OSDN Git Service

Upgrade to mksh 50.
[android-x86/external-mksh.git] / src / misc.c
index 75a4de1..82d47d6 100644 (file)
@@ -1,8 +1,9 @@
-/*     $OpenBSD: misc.c,v 1.37 2009/04/19 20:34:05 sthen Exp $ */
+/*     $OpenBSD: misc.c,v 1.38 2013/11/28 10:33:37 sobrado Exp $       */
 /*     $OpenBSD: path.c,v 1.12 2005/03/30 17:16:37 deraadt Exp $       */
 
 /*-
- * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
+ * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
+ *              2011, 2012, 2013, 2014
  *     Thorsten Glaser <tg@mirbsd.org>
  *
  * Provided that these terms and disclaimer and all copyright notices
 #include <grp.h>
 #endif
 
-__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.141 2010/07/17 22:09:36 tg Exp $");
+__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.219 2014/01/05 21:57:27 tg Exp $");
 
-unsigned char chtypes[UCHAR_MAX + 1];  /* type bits for unsigned char */
-
-#if !HAVE_SETRESUGID
-uid_t kshuid;
-gid_t kshgid, kshegid;
+#define KSH_CHVT_FLAG
+#ifdef MKSH_SMALL
+#undef KSH_CHVT_FLAG
+#endif
+#ifdef TIOCSCTTY
+#define KSH_CHVT_CODE
+#define KSH_CHVT_FLAG
+#endif
+#ifdef MKSH_LEGACY_MODE
+#undef KSH_CHVT_CODE
+#undef KSH_CHVT_FLAG
 #endif
 
+/* type bits for unsigned char */
+unsigned char chtypes[UCHAR_MAX + 1];
+
+static const unsigned char *pat_scan(const unsigned char *,
+    const unsigned char *, bool) MKSH_A_PURE;
 static int do_gmatch(const unsigned char *, const unsigned char *,
-    const unsigned char *, const unsigned char *);
-static const unsigned char *cclass(const unsigned char *, int);
-#ifdef TIOCSCTTY
-static void chvt(const char *);
+    const unsigned char *, const unsigned char *) MKSH_A_PURE;
+static const unsigned char *cclass(const unsigned char *, unsigned char)
+    MKSH_A_PURE;
+#ifdef KSH_CHVT_CODE
+static void chvt(const Getopt *);
+#endif
+
+/*XXX this should go away */
+static int make_path(const char *, const char *, char **, XString *, int *);
+
+#ifdef SETUID_CAN_FAIL_WITH_EAGAIN
+/* we don't need to check for other codes, EPERM won't happen */
+#define DO_SETUID(func, argvec) do {                                   \
+       if ((func argvec) && errno == EAGAIN)                           \
+               errorf("%s failed with EAGAIN, probably due to a"       \
+                   " too low process limit; aborting", #func);         \
+} while (/* CONSTCOND */ 0)
+#else
+#define DO_SETUID(func, argvec) func argvec
 #endif
 
 /*
@@ -56,7 +83,8 @@ setctypes(const char *s, int t)
        if (t & C_IFS) {
                for (i = 0; i < UCHAR_MAX + 1; i++)
                        chtypes[i] &= ~C_IFS;
-               chtypes[0] |= C_IFS; /* include \0 in C_IFS */
+               /* include \0 in C_IFS */
+               chtypes[0] |= C_IFS;
        }
        while (*s != 0)
                chtypes[(unsigned char)*s++] |= t;
@@ -73,31 +101,40 @@ initctypes(void)
                chtypes[c] |= C_ALPHA;
        chtypes['_'] |= C_ALPHA;
        setctypes("0123456789", C_DIGIT);
-       setctypes(" \t\n|&;<>()", C_LEX1); /* \0 added automatically */
+       /* \0 added automatically */
+       setctypes(TC_LEX1, C_LEX1);
        setctypes("*@#!$-?", C_VAR1);
-       setctypes(" \t\n", C_IFSWS);
+       setctypes(TC_IFSWS, C_IFSWS);
        setctypes("=-+?", C_SUBOP1);
        setctypes("\t\n \"#$&'()*;<=>?[\\]`|", C_QUOTE);
 }
 
 /* called from XcheckN() to grow buffer */
 char *
-Xcheck_grow_(XString *xsp, const char *xp, unsigned int more)
+Xcheck_grow(XString *xsp, const char *xp, size_t more)
 {
        const char *old_beg = xsp->beg;
 
-       xsp->len += more > xsp->len ? more : xsp->len;
-       xsp->beg = aresize(xsp->beg, xsp->len + 8, xsp->areap);
+       if (more < xsp->len)
+               more = xsp->len;
+       /* (xsp->len + X_EXTRA) never overflows */
+       checkoktoadd(more, xsp->len + X_EXTRA);
+       xsp->beg = aresize(xsp->beg, (xsp->len += more) + X_EXTRA, xsp->areap);
        xsp->end = xsp->beg + xsp->len;
        return (xsp->beg + (xp - old_beg));
 }
 
+
 #define SHFLAGS_DEFNS
-#include "sh_flags.h"
+#include "sh_flags.gen"
+
+#define OFC(i) (options[i][-2])
+#define OFF(i) (((const unsigned char *)options[i])[-1])
+#define OFN(i) (options[i])
 
-const struct shoption options[] = {
+const char * const options[] = {
 #define SHFLAGS_ITEMS
-#include "sh_flags.h"
+#include "sh_flags.gen"
 };
 
 /*
@@ -106,15 +143,20 @@ const struct shoption options[] = {
 size_t
 option(const char *n)
 {
-       size_t i;
+       size_t i = 0;
 
-       if ((n[0] == '-' || n[0] == '+') && n[1] && !n[2]) {
-               for (i = 0; i < NELEM(options); i++)
-                       if (options[i].c == n[1])
+       if ((n[0] == '-' || n[0] == '+') && n[1] && !n[2])
+               while (i < NELEM(options)) {
+                       if (OFC(i) == n[1])
+                               return (i);
+                       ++i;
+               }
+       else
+               while (i < NELEM(options)) {
+                       if (!strcmp(OFN(i), n))
                                return (i);
-       } else for (i = 0; i < NELEM(options); i++)
-               if (options[i].name && strcmp(options[i].name, n) == 0)
-                       return (i);
+                       ++i;
+               }
 
        return ((size_t)-1);
 }
@@ -124,17 +166,17 @@ struct options_info {
        int opts[NELEM(options)];
 };
 
-static char *options_fmt_entry(char *, int, int, const void *);
+static char *options_fmt_entry(char *, size_t, unsigned int, const void *);
 static void printoptions(bool);
 
 /* format a single select menu item */
 static char *
-options_fmt_entry(char *buf, int buflen, int i, const void *arg)
+options_fmt_entry(char *buf, size_t buflen, unsigned int i, const void *arg)
 {
        const struct options_info *oi = (const struct options_info *)arg;
 
        shf_snprintf(buf, buflen, "%-*s %s",
-           oi->opt_width, options[oi->opts[i]].name,
+           oi->opt_width, OFN(oi->opts[i]),
            Flag(oi->opts[i]) ? "on" : "off");
        return (buf);
 }
@@ -142,36 +184,35 @@ options_fmt_entry(char *buf, int buflen, int i, const void *arg)
 static void
 printoptions(bool verbose)
 {
-       int i = 0;
+       size_t i = 0;
 
        if (verbose) {
-               int n = 0, len, octs = 0;
+               size_t n = 0, len, octs = 0;
                struct options_info oi;
 
                /* verbose version */
                shf_puts("Current option settings\n", shl_stdout);
 
                oi.opt_width = 0;
-               while (i < (int)NELEM(options)) {
-                       if (options[i].name) {
+               while (i < NELEM(options)) {
+                       if ((len = strlen(OFN(i)))) {
                                oi.opts[n++] = i;
-                               len = strlen(options[i].name);
                                if (len > octs)
                                        octs = len;
-                               len = utf_mbswidth(options[i].name);
-                               if (len > oi.opt_width)
-                                       oi.opt_width = len;
+                               len = utf_mbswidth(OFN(i));
+                               if ((int)len > oi.opt_width)
+                                       oi.opt_width = (int)len;
                        }
                        ++i;
                }
                print_columns(shl_stdout, n, options_fmt_entry, &oi,
                    octs + 4, oi.opt_width + 4, true);
        } else {
-               /* short version รก la AT&T ksh93 */
-               shf_puts("set", shl_stdout);
-               while (i < (int)NELEM(options)) {
-                       if (Flag(i) && options[i].name)
-                               shprintf(" -o %s", options[i].name);
+               /* short version like AT&T ksh93 */
+               shf_puts(Tset, shl_stdout);
+               while (i < NELEM(options)) {
+                       if (Flag(i) && OFN(i)[0])
+                               shprintf(" -o %s", OFN(i));
                        ++i;
                }
                shf_putc('\n', shl_stdout);
@@ -181,31 +222,39 @@ printoptions(bool verbose)
 char *
 getoptions(void)
 {
-       unsigned int i;
-       char m[(int) FNFLAGS + 1];
+       size_t i = 0;
+       char c, m[(int)FNFLAGS + 1];
        char *cp = m;
 
-       for (i = 0; i < NELEM(options); i++)
-               if (options[i].c && Flag(i))
-                       *cp++ = options[i].c;
+       while (i < NELEM(options)) {
+               if ((c = OFC(i)) && Flag(i))
+                       *cp++ = c;
+               ++i;
+       }
        strndupx(cp, m, cp - m, ATEMP);
        return (cp);
 }
 
 /* change a Flag(*) value; takes care of special actions */
 void
-change_flag(enum sh_flag f, int what, unsigned int newval)
+change_flag(enum sh_flag f, int what, bool newset)
 {
        unsigned char oldval;
+       unsigned char newval = (newset ? 1 : 0);
 
+       if (f == FXTRACE) {
+               change_xtrace(newval, true);
+               return;
+       }
        oldval = Flag(f);
-       Flag(f) = newval ? 1 : 0;       /* needed for tristates */
+       Flag(f) = newval = (newset ? 1 : 0);
 #ifndef MKSH_UNEMPLOYED
        if (f == FMONITOR) {
                if (what != OF_CMDLINE && newval != oldval)
                        j_change();
        } else
 #endif
+#ifndef MKSH_NO_CMDLINE_EDITING
          if ((
 #if !MKSH_S_NOVI
            f == FVI ||
@@ -215,83 +264,109 @@ change_flag(enum sh_flag f, int what, unsigned int newval)
                Flag(FVI) =
 #endif
                    Flag(FEMACS) = Flag(FGMACS) = 0;
-               Flag(f) = (unsigned char)newval;
-       } else if (f == FPRIVILEGED && oldval && !newval) {
+               Flag(f) = newval;
+       } else
+#endif
+         if (f == FPRIVILEGED && oldval && !newval) {
                /* Turning off -p? */
-#if HAVE_SETRESUGID
-               gid_t kshegid = getgid();
 
-               setresgid(kshegid, kshegid, kshegid);
+               /*XXX this can probably be optimised */
+               kshegid = kshgid = getgid();
+               ksheuid = kshuid = getuid();
+#if HAVE_SETRESUGID
+               DO_SETUID(setresgid, (kshegid, kshegid, kshegid));
 #if HAVE_SETGROUPS
+               /* setgroups doesn't EAGAIN on Linux */
                setgroups(1, &kshegid);
 #endif
-               setresuid(ksheuid, ksheuid, ksheuid);
-#else
-               seteuid(ksheuid = kshuid = getuid());
-               setuid(ksheuid);
-               setegid(kshegid = kshgid = getgid());
-               setgid(kshegid);
+               DO_SETUID(setresuid, (ksheuid, ksheuid, ksheuid));
+#else /* !HAVE_SETRESUGID */
+               /* seteuid, setegid, setgid don't EAGAIN on Linux */
+#ifndef MKSH__NO_SETEUGID
+               seteuid(ksheuid);
+#endif
+               DO_SETUID(setuid, (ksheuid));
+#ifndef MKSH__NO_SETEUGID
+               setegid(kshegid);
 #endif
+               setgid(kshegid);
+#endif /* !HAVE_SETRESUGID */
        } else if ((f == FPOSIX || f == FSH) && newval) {
-               Flag(FPOSIX) = Flag(FSH) = Flag(FBRACEEXPAND) = 0;
-               Flag(f) = (unsigned char)newval;
-       }
-       /* Changing interactive flag? */
-       if (f == FTALKING) {
+               /* Turning on -o posix or -o sh? */
+               Flag(FBRACEEXPAND) = 0;
+       } else if (f == FTALKING) {
+               /* Changing interactive flag? */
                if ((what == OF_CMDLINE || what == OF_SET) && procpid == kshpid)
-                       Flag(FTALKING_I) = (unsigned char)newval;
+                       Flag(FTALKING_I) = newval;
+       }
+}
+
+void
+change_xtrace(unsigned char newval, bool dosnapshot)
+{
+       if (!dosnapshot && newval == Flag(FXTRACE))
+               return;
+
+       if (Flag(FXTRACE) == 2) {
+               shf_putc('\n', shl_xtrace);
+               Flag(FXTRACE) = 1;
+               shf_flush(shl_xtrace);
        }
+
+       if (!dosnapshot && Flag(FXTRACE) == 1)
+               switch (newval) {
+               case 1:
+                       return;
+               case 2:
+                       goto changed_xtrace;
+               }
+
+       shf_flush(shl_xtrace);
+       if (shl_xtrace->fd != 2)
+               close(shl_xtrace->fd);
+       if (!newval || (shl_xtrace->fd = savefd(2)) == -1)
+               shl_xtrace->fd = 2;
+
+ changed_xtrace:
+       if ((Flag(FXTRACE) = newval) == 2)
+               shf_puts(substitute(str_val(global("PS4")), 0), shl_xtrace);
 }
 
-/* Parse command line & set command arguments. Returns the index of
+/*
+ * Parse command line and set command arguments. Returns the index of
  * non-option arguments, -1 if there is an error.
  */
 int
 parse_args(const char **argv,
-    int what,                  /* OF_CMDLINE or OF_SET */
+    /* OF_CMDLINE or OF_SET */
+    int what,
     bool *setargsp)
 {
-       static char cmd_opts[NELEM(options) + 5]; /* o:T:\0 */
-       static char set_opts[NELEM(options) + 6]; /* A:o;s\0 */
-       char set, *opts;
+       static const char cmd_opts[] =
+#define SHFLAGS_NOT_SET
+#define SHFLAGS_OPTCS
+#include "sh_flags.gen"
+#undef SHFLAGS_NOT_SET
+           ;
+       static const char set_opts[] =
+#define SHFLAGS_NOT_CMD
+#define SHFLAGS_OPTCS
+#include "sh_flags.gen"
+#undef SHFLAGS_NOT_CMD
+           ;
+       bool set;
+       const char *opts;
        const char *array = NULL;
        Getopt go;
        size_t i;
-       int optc, sortargs = 0, arrayset = 0;
-
-       /* First call? Build option strings... */
-       if (cmd_opts[0] == '\0') {
-               char *p = cmd_opts, *q = set_opts;
-
-               /* see cmd_opts[] declaration */
-               *p++ = 'o';
-               *p++ = ':';
-#if !defined(MKSH_SMALL) || defined(TIOCSCTTY)
-               *p++ = 'T';
-               *p++ = ':';
-#endif
-               /* see set_opts[] declaration */
-               *q++ = 'A';
-               *q++ = ':';
-               *q++ = 'o';
-               *q++ = ';';
-               *q++ = 's';
-
-               for (i = 0; i < NELEM(options); i++) {
-                       if (options[i].c) {
-                               if (options[i].flags & OF_CMDLINE)
-                                       *p++ = options[i].c;
-                               if (options[i].flags & OF_SET)
-                                       *q++ = options[i].c;
-                       }
-               }
-               *p = '\0';
-               *q = '\0';
-       }
+       int optc, arrayset = 0;
+       bool sortargs = false;
+       bool fcompatseen = false;
 
        if (what == OF_CMDLINE) {
                const char *p = argv[0], *q;
-               /* Set FLOGIN before parsing options so user can clear
+               /*
+                * Set FLOGIN before parsing options so user can clear
                 * flag using +l.
                 */
                if (*p != '-')
@@ -306,7 +381,7 @@ parse_args(const char **argv,
                opts = set_opts;
        ksh_getopt_reset(&go, GF_ERROR|GF_PLUSOPT);
        while ((optc = ksh_getopt(argv, &go, opts)) != -1) {
-               set = (go.info & GI_PLUS) ? 0 : 1;
+               set = tobool(!(go.info & GI_PLUS));
                switch (optc) {
                case 'A':
                        if (what == OF_FIRSTTIME)
@@ -319,7 +394,8 @@ parse_args(const char **argv,
                        if (what == OF_FIRSTTIME)
                                break;
                        if (go.optarg == NULL) {
-                               /* lone -o: print options
+                               /*
+                                * lone -o: print options
                                 *
                                 * Note that on the command line, -o requires
                                 * an option (ie, can't get here if what is
@@ -329,36 +405,42 @@ parse_args(const char **argv,
                                break;
                        }
                        i = option(go.optarg);
-                       if ((enum sh_flag)i == FARC4RANDOM) {
-                               warningf(true, "Do not use set ยฑo arc4random,"
-                                   " it will be removed in the next version"
-                                   " of mksh!");
-                               return (0);
+                       if ((i == FPOSIX || i == FSH) && set && !fcompatseen) {
+                               /*
+                                * If running 'set -o posix' or
+                                * 'set -o sh', turn off the other;
+                                * if running 'set -o posix -o sh'
+                                * allow both to be set though.
+                                */
+                               Flag(FPOSIX) = 0;
+                               Flag(FSH) = 0;
+                               fcompatseen = true;
                        }
-                       if ((i != (size_t)-1) && set == Flag(i))
-                               /* Don't check the context if the flag
+                       if ((i != (size_t)-1) && (set ? 1U : 0U) == Flag(i))
+                               /*
+                                * Don't check the context if the flag
                                 * isn't changing - makes "set -o interactive"
                                 * work if you're already interactive. Needed
                                 * if the output of "set +o" is to be used.
                                 */
                                ;
-                       else if ((i != (size_t)-1) && (options[i].flags & what))
+                       else if ((i != (size_t)-1) && (OFF(i) & what))
                                change_flag((enum sh_flag)i, what, set);
                        else {
-                               bi_errorf("%s: bad option", go.optarg);
+                               bi_errorf("%s: %s", go.optarg, "bad option");
                                return (-1);
                        }
                        break;
 
-#if !defined(MKSH_SMALL) || defined(TIOCSCTTY)
+#ifdef KSH_CHVT_FLAG
                case 'T':
                        if (what != OF_FIRSTTIME)
                                break;
-#ifndef TIOCSCTTY
+#ifndef KSH_CHVT_CODE
                        errorf("no TIOCSCTTY ioctl");
 #else
-                       change_flag(FTALKING, OF_CMDLINE, 1);
-                       chvt(go.optarg);
+                       change_flag(FTALKING, OF_CMDLINE, true);
+                       chvt(&go);
                        break;
 #endif
 #endif
@@ -371,12 +453,12 @@ parse_args(const char **argv,
                                break;
                        /* -s: sort positional params (AT&T ksh stupidity) */
                        if (what == OF_SET && optc == 's') {
-                               sortargs = 1;
+                               sortargs = true;
                                break;
                        }
                        for (i = 0; i < NELEM(options); i++)
-                               if (optc == options[i].c &&
-                                   (what & options[i].flags)) {
+                               if (optc == OFC(i) &&
+                                   (what & OFF(i))) {
                                        change_flag((enum sh_flag)i, what, set);
                                        break;
                                }
@@ -388,8 +470,10 @@ parse_args(const char **argv,
            (argv[go.optind][0] == '-' || argv[go.optind][0] == '+') &&
            argv[go.optind][1] == '\0') {
                /* lone - clears -v and -x flags */
-               if (argv[go.optind][0] == '-')
-                       Flag(FVERBOSE) = Flag(FXTRACE) = 0;
+               if (argv[go.optind][0] == '-') {
+                       Flag(FVERBOSE) = 0;
+                       change_xtrace(0, false);
+               }
                /* set skips lone - or + option */
                go.optind++;
        }
@@ -398,9 +482,16 @@ parse_args(const char **argv,
                *setargsp = !arrayset && ((go.info & GI_MINUSMINUS) ||
                    argv[go.optind]);
 
-       if (arrayset && (!*array || *skip_varname(array, false))) {
-               bi_errorf("%s: is not an identifier", array);
-               return (-1);
+       if (arrayset) {
+               const char *ccp = NULL;
+
+               mkssert(array != NULL);
+               if (*array)
+                       ccp = skip_varname(array, false);
+               if (!ccp || !(!ccp[0] || (ccp[0] == '+' && !ccp[1]))) {
+                       bi_errorf("%s: %s", array, "is not an identifier");
+                       return (-1);
+               }
        }
        if (sortargs) {
                for (i = go.optind; argv[i]; i++)
@@ -409,7 +500,7 @@ parse_args(const char **argv,
                    xstrcmp);
        }
        if (arrayset)
-               go.optind += set_array(array, arrayset > 0 ? true : false,
+               go.optind += set_array(array, tobool(arrayset > 0),
                    argv + go.optind);
 
        return (go.optind);
@@ -419,45 +510,117 @@ parse_args(const char **argv,
 int
 getn(const char *s, int *ai)
 {
-       int i, c, rv = 0;
+       char c;
+       mksh_ari_u num;
        bool neg = false;
 
+       num.u = 0;
+
        do {
                c = *s++;
        } while (ksh_isspace(c));
-       if (c == '-') {
+
+       switch (c) {
+       case '-':
                neg = true;
+               /* FALLTHROUGH */
+       case '+':
                c = *s++;
-       } else if (c == '+')
-               c = *s++;
-       *ai = i = 0;
+               break;
+       }
+
        do {
                if (!ksh_isdigit(c))
-                       goto getn_out;
-               i *= 10;
-               if (i < *ai)
-                       /* overflow */
-                       goto getn_out;
-               i += c - '0';
-               *ai = i;
+                       /* not numeric */
+                       return (0);
+               if (num.u > 214748364U)
+                       /* overflow on multiplication */
+                       return (0);
+               num.u = num.u * 10U + (unsigned int)(c - '0');
+               /* now: num.u <= 2147483649U */
        } while ((c = *s++));
-       rv = 1;
 
- getn_out:
+       if (num.u > (neg ? 2147483648U : 2147483647U))
+               /* overflow for signed 32-bit int */
+               return (0);
+
        if (neg)
-               *ai = -*ai;
-       return (rv);
+               num.u = -num.u;
+       *ai = num.i;
+       return (1);
 }
 
-/* getn() that prints error */
-int
-bi_getn(const char *as, int *ai)
+/**
+ * pattern simplifications:
+ * - @(x) -> x (not @(x|y) though)
+ * - ** -> *
+ */
+static void *
+simplify_gmatch_pattern(const unsigned char *sp)
 {
-       int rv;
+       uint8_t c;
+       unsigned char *cp, *dp;
+       const unsigned char *ps, *se;
+
+       cp = alloc(strlen((const void *)sp) + 1, ATEMP);
+       goto simplify_gmatch_pat1a;
+
+       /* foo@(b@(a)r)b@(a|a)z -> foobarb@(a|a)z */
+ simplify_gmatch_pat1:
+       sp = cp;
+ simplify_gmatch_pat1a:
+       dp = cp;
+       se = sp + strlen((const void *)sp);
+       while ((c = *sp++)) {
+               if (!ISMAGIC(c)) {
+                       *dp++ = c;
+                       continue;
+               }
+               switch ((c = *sp++)) {
+               case 0x80|'@':
+               /* simile for @ */
+               case 0x80|' ':
+                       /* check whether it has only one clause */
+                       ps = pat_scan(sp, se, true);
+                       if (!ps || ps[-1] != /*(*/ ')')
+                               /* nope */
+                               break;
+                       /* copy inner clause until matching close */
+                       ps -= 2;
+                       while ((const unsigned char *)sp < ps)
+                               *dp++ = *sp++;
+                       /* skip MAGIC and closing parenthesis */
+                       sp += 2;
+                       /* copy the rest of the pattern */
+                       memmove(dp, sp, strlen((const void *)sp) + 1);
+                       /* redo from start */
+                       goto simplify_gmatch_pat1;
+               }
+               *dp++ = MAGIC;
+               *dp++ = c;
+       }
+       *dp = '\0';
 
-       if (!(rv = getn(as, ai)))
-               bi_errorf("%s: bad number", as);
-       return (rv);
+       /* collapse adjacent asterisk wildcards */
+       sp = dp = cp;
+       while ((c = *sp++)) {
+               if (!ISMAGIC(c)) {
+                       *dp++ = c;
+                       continue;
+               }
+               switch ((c = *sp++)) {
+               case '*':
+                       while (ISMAGIC(sp[0]) && sp[1] == c)
+                               sp += 2;
+                       break;
+               }
+               *dp++ = MAGIC;
+               *dp++ = c;
+       }
+       *dp = '\0';
+
+       /* return the result, allocated from ATEMP */
+       return (cp);
 }
 
 /* -------- gmatch.c -------- */
@@ -469,18 +632,20 @@ bi_getn(const char *as, int *ai)
  * Match a pattern as in sh(1).
  * pattern character are prefixed with MAGIC by expand.
  */
-
 int
 gmatchx(const char *s, const char *p, bool isfile)
 {
        const char *se, *pe;
+       char *pnew;
+       int rv;
 
        if (s == NULL || p == NULL)
                return (0);
 
        se = s + strlen(s);
        pe = p + strlen(p);
-       /* isfile is false iff no syntax check has been done on
+       /*
+        * isfile is false iff no syntax check has been done on
         * the pattern. If check fails, just to a strcmp().
         */
        if (!isfile && !has_globbing(p, pe)) {
@@ -490,11 +655,22 @@ gmatchx(const char *s, const char *p, bool isfile)
                debunk(t, p, len);
                return (!strcmp(t, s));
        }
-       return (do_gmatch((const unsigned char *) s, (const unsigned char *) se,
-           (const unsigned char *) p, (const unsigned char *) pe));
+
+       /*
+        * since the do_gmatch() engine sucks so much, we must do some
+        * pattern simplifications
+        */
+       pnew = simplify_gmatch_pattern((const unsigned char *)p);
+       pe = pnew + strlen(pnew);
+
+       rv = do_gmatch((const unsigned char *)s, (const unsigned char *)se,
+           (const unsigned char *)pnew, (const unsigned char *)pe);
+       afree(pnew, ATEMP);
+       return (rv);
 }
 
-/* Returns if p is a syntacticly correct globbing pattern, false
+/**
+ * Returns if p is a syntacticly correct globbing pattern, false
  * if it contains no pattern characters or if there is a syntax error.
  * Syntax errors are:
  *     - [ with no closing ]
@@ -502,14 +678,14 @@ gmatchx(const char *s, const char *p, bool isfile)
  *     - [...] and *(...) not nested (eg, [a$(b|]c), *(a[b|c]d))
  */
 /*XXX
-- if no magic,
      if dest given, copy to dst
      return ?
-- if magic && (no globbing || syntax error)
      debunk to dst
      return ?
-- return ?
-*/
+ * - if no magic,
*     if dest given, copy to dst
*     return ?
+ * - if magic && (no globbing || syntax error)
*     debunk to dst
*     return ?
+ * - return ?
+ */
 int
 has_globbing(const char *xp, const char *xpe)
 {
@@ -517,42 +693,46 @@ has_globbing(const char *xp, const char *xpe)
        const unsigned char *pe = (const unsigned char *) xpe;
        int c;
        int nest = 0, bnest = 0;
-       int saw_glob = 0;
-       int in_bracket = 0; /* inside [...] */
+       bool saw_glob = false;
+       /* inside [...] */
+       bool in_bracket = false;
 
        for (; p < pe; p++) {
                if (!ISMAGIC(*p))
                        continue;
                if ((c = *++p) == '*' || c == '?')
-                       saw_glob = 1;
+                       saw_glob = true;
                else if (c == '[') {
                        if (!in_bracket) {
-                               saw_glob = 1;
-                               in_bracket = 1;
-                               if (ISMAGIC(p[1]) && p[2] == NOT)
+                               saw_glob = true;
+                               in_bracket = true;
+                               if (ISMAGIC(p[1]) && p[2] == '!')
                                        p += 2;
                                if (ISMAGIC(p[1]) && p[2] == ']')
                                        p += 2;
                        }
-                       /* XXX Do we need to check ranges here? POSIX Q */
+                       /*XXX Do we need to check ranges here? POSIX Q */
                } else if (c == ']') {
                        if (in_bracket) {
-                               if (bnest)              /* [a*(b]) */
+                               if (bnest)
+                                       /* [a*(b]) */
                                        return (0);
-                               in_bracket = 0;
+                               in_bracket = false;
                        }
                } else if ((c & 0x80) && vstrchr("*+?@! ", c & 0x7f)) {
-                       saw_glob = 1;
+                       saw_glob = true;
                        if (in_bracket)
                                bnest++;
                        else
                                nest++;
                } else if (c == '|') {
-                       if (in_bracket && !bnest)       /* *(a[foo|bar]) */
+                       if (in_bracket && !bnest)
+                               /* *(a[foo|bar]) */
                                return (0);
                } else if (c == /*(*/ ')') {
                        if (in_bracket) {
-                               if (!bnest--)           /* *(a[b)c] */
+                               if (!bnest--)
+                                       /* *(a[b)c] */
                                        return (0);
                        } else if (nest)
                                nest--;
@@ -570,7 +750,7 @@ static int
 do_gmatch(const unsigned char *s, const unsigned char *se,
     const unsigned char *p, const unsigned char *pe)
 {
-       int sc, pc;
+       unsigned char sc, pc;
        const unsigned char *prest, *psub, *pnext;
        const unsigned char *srest;
 
@@ -614,9 +794,12 @@ do_gmatch(const unsigned char *s, const unsigned char *se,
                 * [*+?@!](pattern|pattern|..)
                 * This is also needed for ${..%..}, etc.
                 */
-               case 0x80|'+': /* matches one or more times */
-               case 0x80|'*': /* matches zero or more times */
-                       if (!(prest = pat_scan(p, pe, 0)))
+
+               /* matches one or more times */
+               case 0x80|'+':
+               /* matches zero or more times */
+               case 0x80|'*':
+                       if (!(prest = pat_scan(p, pe, false)))
                                return (0);
                        s--;
                        /* take care of zero matches */
@@ -624,7 +807,7 @@ do_gmatch(const unsigned char *s, const unsigned char *se,
                            do_gmatch(s, se, prest, pe))
                                return (1);
                        for (psub = p; ; psub = pnext) {
-                               pnext = pat_scan(psub, pe, 1);
+                               pnext = pat_scan(psub, pe, true);
                                for (srest = s; srest <= se; srest++) {
                                        if (do_gmatch(s, srest, psub, pnext - 2) &&
                                            (do_gmatch(srest, se, prest, pe) ||
@@ -637,10 +820,13 @@ do_gmatch(const unsigned char *s, const unsigned char *se,
                        }
                        return (0);
 
-               case 0x80|'?': /* matches zero or once */
-               case 0x80|'@': /* matches one of the patterns */
-               case 0x80|' ': /* simile for @ */
-                       if (!(prest = pat_scan(p, pe, 0)))
+               /* matches zero or once */
+               case 0x80|'?':
+               /* matches one of the patterns */
+               case 0x80|'@':
+               /* simile for @ */
+               case 0x80|' ':
+                       if (!(prest = pat_scan(p, pe, false)))
                                return (0);
                        s--;
                        /* Take care of zero matches */
@@ -648,7 +834,7 @@ do_gmatch(const unsigned char *s, const unsigned char *se,
                            do_gmatch(s, se, prest, pe))
                                return (1);
                        for (psub = p; ; psub = pnext) {
-                               pnext = pat_scan(psub, pe, 1);
+                               pnext = pat_scan(psub, pe, true);
                                srest = prest == pe ? se : s;
                                for (; srest <= se; srest++) {
                                        if (do_gmatch(s, srest, psub, pnext - 2) &&
@@ -660,15 +846,16 @@ do_gmatch(const unsigned char *s, const unsigned char *se,
                        }
                        return (0);
 
-               case 0x80|'!': /* matches none of the patterns */
-                       if (!(prest = pat_scan(p, pe, 0)))
+               /* matches none of the patterns */
+               case 0x80|'!':
+                       if (!(prest = pat_scan(p, pe, false)))
                                return (0);
                        s--;
                        for (srest = s; srest <= se; srest++) {
                                int matched = 0;
 
                                for (psub = p; ; psub = pnext) {
-                                       pnext = pat_scan(psub, pe, 1);
+                                       pnext = pat_scan(psub, pe, true);
                                        if (do_gmatch(s, srest, psub,
                                            pnext - 2)) {
                                                matched = 1;
@@ -693,21 +880,24 @@ do_gmatch(const unsigned char *s, const unsigned char *se,
 }
 
 static const unsigned char *
-cclass(const unsigned char *p, int sub)
+cclass(const unsigned char *p, unsigned char sub)
 {
-       int c, d, notp, found = 0;
+       unsigned char c, d;
+       bool notp, found = false;
        const unsigned char *orig_p = p;
 
-       if ((notp = (ISMAGIC(*p) && *++p == NOT)))
+       if ((notp = tobool(ISMAGIC(*p) && *++p == '!')))
                p++;
        do {
                c = *p++;
                if (ISMAGIC(c)) {
                        c = *p++;
                        if ((c & 0x80) && !ISMAGIC(c)) {
-                               c &= 0x7f;/* extended pattern matching: *+?@! */
+                               /* extended pattern matching: *+?@! */
+                               c &= 0x7F;
                                /* XXX the ( char isn't handled as part of [] */
-                               if (c == ' ') /* simile for @: plain (..) */
+                               if (c == ' ')
+                                       /* simile for @: plain (..) */
                                        c = '(' /*)*/;
                        }
                }
@@ -716,7 +906,8 @@ cclass(const unsigned char *p, int sub)
                        return (sub == '[' ? orig_p : NULL);
                if (ISMAGIC(p[0]) && p[1] == '-' &&
                    (!ISMAGIC(p[2]) || p[3] != ']')) {
-                       p += 2; /* MAGIC- */
+                       /* MAGIC- */
+                       p += 2;
                        d = *p++;
                        if (ISMAGIC(d)) {
                                d = *p++;
@@ -729,15 +920,15 @@ cclass(const unsigned char *p, int sub)
                } else
                        d = c;
                if (c == sub || (c <= sub && sub <= d))
-                       found = 1;
+                       found = true;
        } while (!(ISMAGIC(p[0]) && p[1] == ']'));
 
        return ((found != notp) ? p+2 : NULL);
 }
 
 /* Look for next ) or | (if match_sep) in *(foo|bar) pattern */
-const unsigned char *
-pat_scan(const unsigned char *p, const unsigned char *pe, int match_sep)
+static const unsigned char *
+pat_scan(const unsigned char *p, const unsigned char *pe, bool match_sep)
 {
        int nest = 0;
 
@@ -772,7 +963,8 @@ ksh_getopt_reset(Getopt *go, int flags)
 }
 
 
-/* getopt() used for shell built-in commands, the getopts command, and
+/**
+ * getopt() used for shell built-in commands, the getopts command, and
  * command line options.
  * A leading ':' in options means don't print errors, instead return '?'
  * or ':' and set go->optarg to the offending option character.
@@ -813,7 +1005,8 @@ ksh_getopt(const char **argv, Getopt *go, const char *optionsp)
                        return (-1);
                }
                if (arg == NULL ||
-                   ((flag != '-' ) && /* neither a - nor a + (if + allowed) */
+                   ((flag != '-' ) &&
+                   /* neither a - nor a + (if + allowed) */
                    (!(go->flags & GF_PLUSOPT) || flag != '+')) ||
                    (c = arg[1]) == '\0') {
                        go->p = 0;
@@ -830,15 +1023,17 @@ ksh_getopt(const char **argv, Getopt *go, const char *optionsp)
                        go->buf[0] = c;
                        go->optarg = go->buf;
                } else {
-                       warningf(true, "%s%s-%c: unknown option",
+                       warningf(true, "%s%s-%c: %s",
                            (go->flags & GF_NONAME) ? "" : argv[0],
-                           (go->flags & GF_NONAME) ? "" : ": ", c);
+                           (go->flags & GF_NONAME) ? "" : ": ", c,
+                           "unknown option");
                        if (go->flags & GF_ERROR)
                                bi_errorfz();
                }
                return ('?');
        }
-       /* : means argument must be present, may be part of option argument
+       /**
+        * : means argument must be present, may be part of option argument
         *   or the next argument
         * ; same as : but argument may be missing
         * , means argument is part of option argument, and may be null.
@@ -856,9 +1051,10 @@ ksh_getopt(const char **argv, Getopt *go, const char *optionsp)
                                go->optarg = go->buf;
                                return (':');
                        }
-                       warningf(true, "%s%s-'%c' requires argument",
+                       warningf(true, "%s%s-%c: %s",
                            (go->flags & GF_NONAME) ? "" : argv[0],
-                           (go->flags & GF_NONAME) ? "" : ": ", c);
+                           (go->flags & GF_NONAME) ? "" : ": ", c,
+                           "requires an argument");
                        if (go->flags & GF_ERROR)
                                bi_errorfz();
                        return ('?');
@@ -869,7 +1065,8 @@ ksh_getopt(const char **argv, Getopt *go, const char *optionsp)
                go->optarg = argv[go->optind - 1] + go->p;
                go->p = 0;
        } else if (*o == '#') {
-               /* argument is optional and may be attached or unattached
+               /*
+                * argument is optional and may be attached or unattached
                 * but must start with a digit. optarg is set to 0 if the
                 * argument is missing.
                 */
@@ -890,38 +1087,122 @@ ksh_getopt(const char **argv, Getopt *go, const char *optionsp)
        return (c);
 }
 
-/* print variable/alias value using necessary quotes
+/*
+ * print variable/alias value using necessary quotes
  * (POSIX says they should be suitable for re-entry...)
  * No trailing newline is printed.
  */
 void
-print_value_quoted(const char *s)
+print_value_quoted(struct shf *shf, const char *s)
 {
-       const char *p;
-       int inquote = 0;
+       unsigned char c;
+       const unsigned char *p = (const unsigned char *)s;
+       bool inquote = true;
+
+       /* first, check whether any quotes are needed */
+       while ((c = *p++) >= 32)
+               if (ctype(c, C_QUOTE))
+                       inquote = false;
+
+       p = (const unsigned char *)s;
+       if (c == 0) {
+               if (inquote) {
+                       /* nope, use the shortcut */
+                       shf_puts(s, shf);
+                       return;
+               }
 
-       /* Test if any quotes are needed */
-       for (p = s; *p; p++)
-               if (ctype(*p, C_QUOTE))
-                       break;
-       if (!*p) {
-               shf_puts(s, shl_stdout);
-               return;
-       }
-       for (p = s; *p; p++) {
-               if (*p == '\'') {
-                       if (inquote)
-                               shf_putc('\'', shl_stdout);
-                       shf_putc('\\', shl_stdout);
-                       inquote = 0;
-               } else if (!inquote) {
-                       shf_putc('\'', shl_stdout);
-                       inquote = 1;
+               /* otherwise, quote nicely via state machine */
+               while ((c = *p++) != 0) {
+                       if (c == '\'') {
+                               /*
+                                * multiple single quotes or any of them
+                                * at the beginning of a string look nicer
+                                * this way than when simply substituting
+                                */
+                               if (inquote) {
+                                       shf_putc('\'', shf);
+                                       inquote = false;
+                               }
+                               shf_putc('\\', shf);
+                       } else if (!inquote) {
+                               shf_putc('\'', shf);
+                               inquote = true;
+                       }
+                       shf_putc(c, shf);
+               }
+       } else {
+               unsigned int wc;
+               size_t n;
+
+               /* use $'...' quote format */
+               shf_putc('$', shf);
+               shf_putc('\'', shf);
+               while ((c = *p) != 0) {
+                       if (c >= 0xC2) {
+                               n = utf_mbtowc(&wc, (const char *)p);
+                               if (n != (size_t)-1) {
+                                       p += n;
+                                       shf_fprintf(shf, "\\u%04X", wc);
+                                       continue;
+                               }
+                       }
+                       ++p;
+                       switch (c) {
+                       /* see unbksl() in this file for comments */
+                       case 7:
+                               c = 'a';
+                               if (0)
+                                       /* FALLTHROUGH */
+                       case '\b':
+                                 c = 'b';
+                               if (0)
+                                       /* FALLTHROUGH */
+                       case '\f':
+                                 c = 'f';
+                               if (0)
+                                       /* FALLTHROUGH */
+                       case '\n':
+                                 c = 'n';
+                               if (0)
+                                       /* FALLTHROUGH */
+                       case '\r':
+                                 c = 'r';
+                               if (0)
+                                       /* FALLTHROUGH */
+                       case '\t':
+                                 c = 't';
+                               if (0)
+                                       /* FALLTHROUGH */
+                       case 11:
+                                 c = 'v';
+                               if (0)
+                                       /* FALLTHROUGH */
+                       case '\033':
+                               /* take E not e because \e is \ in *roff */
+                                 c = 'E';
+                               /* FALLTHROUGH */
+                       case '\\':
+                               shf_putc('\\', shf);
+
+                               if (0)
+                                       /* FALLTHROUGH */
+                       default:
+                                 if (c < 32 || c > 0x7E) {
+                                       /* FALLTHROUGH */
+                       case '\'':
+                                       shf_fprintf(shf, "\\%03o", c);
+                                       break;
+                               }
+
+                               shf_putc(c, shf);
+                               break;
+                       }
                }
-               shf_putc(*p, shl_stdout);
+               inquote = true;
        }
        if (inquote)
-               shf_putc('\'', shl_stdout);
+               shf_putc('\'', shf);
 }
 
 /*
@@ -929,27 +1210,35 @@ print_value_quoted(const char *s)
  * the i-th element
  */
 void
-print_columns(struct shf *shf, int n,
-    char *(*func)(char *, int, int, const void *),
-    const void *arg, int max_oct, int max_col, bool prefcol)
+print_columns(struct shf *shf, unsigned int n,
+    char *(*func)(char *, size_t, unsigned int, const void *),
+    const void *arg, size_t max_oct, size_t max_colz, bool prefcol)
 {
-       int i, r, c, rows, cols, nspace;
+       unsigned int i, r, c, rows, cols, nspace, max_col;
        char *str;
 
-       if (n <= 0) {
+       if (!n)
+               return;
+
+       if (max_colz > 2147483646) {
 #ifndef MKSH_SMALL
-               internal_warningf("print_columns called with n=%d <= 0", n);
+               internal_warningf("print_columns called with %s=%zu >= INT_MAX",
+                   "max_col", max_colz);
 #endif
                return;
        }
+       max_col = (unsigned int)max_colz;
 
+       if (max_oct > 2147483646) {
+#ifndef MKSH_SMALL
+               internal_warningf("print_columns called with %s=%zu >= INT_MAX",
+                   "max_oct", max_oct);
+#endif
+               return;
+       }
        ++max_oct;
        str = alloc(max_oct, ATEMP);
 
-       /* ensure x_cols is valid first */
-       if (x_cols < MIN_COLS)
-               change_winsz();
-
        /*
         * We use (max_col + 1) to consider the space separator.
         * Note that no space is printed after the last column
@@ -960,20 +1249,19 @@ print_columns(struct shf *shf, int n,
        /* if we can only print one column anyway, skip the goo */
        if (cols < 2) {
                for (i = 0; i < n; ++i)
-                       shf_fprintf(shf, "%s \n",
+                       shf_fprintf(shf, "%s\n",
                            (*func)(str, max_oct, i, arg));
                goto out;
        }
 
        rows = (n + cols - 1) / cols;
        if (prefcol && cols > rows) {
-               i = rows;
-               rows = cols > n ? n : cols;
-               cols = i;
+               cols = rows;
+               rows = (n + cols - 1) / cols;
        }
 
+       nspace = (x_cols - max_col * cols) / cols;
        max_col = -max_col;
-       nspace = (x_cols + max_col * cols) / cols;
        if (nspace <= 0)
                nspace = 1;
        for (r = 0; r < rows; r++) {
@@ -998,8 +1286,9 @@ strip_nuls(char *buf, int nbytes)
 {
        char *dst;
 
-       /* nbytes check because some systems (older FreeBSDs) have a buggy
-        * memchr()
+       /*
+        * nbytes check because some systems (older FreeBSDs) have a
+        * buggy memchr()
         */
        if (nbytes && (dst = memchr(buf, '\0', nbytes))) {
                char *end = buf + nbytes;
@@ -1019,19 +1308,20 @@ strip_nuls(char *buf, int nbytes)
        }
 }
 
-/* Like read(2), but if read fails due to non-blocking flag, resets flag
- * and restarts read.
+/*
+ * Like read(2), but if read fails due to non-blocking flag,
+ * resets flag and restarts read.
  */
-int
-blocking_read(int fd, char *buf, int nbytes)
+ssize_t
+blocking_read(int fd, char *buf, size_t nbytes)
 {
-       int ret;
-       int tried_reset = 0;
+       ssize_t ret;
+       bool tried_reset = false;
 
        while ((ret = read(fd, buf, nbytes)) < 0) {
                if (!tried_reset && errno == EAGAIN) {
                        if (reset_nonblock(fd) > 0) {
-                               tried_reset = 1;
+                               tried_reset = true;
                                continue;
                        }
                        errno = EAGAIN;
@@ -1041,7 +1331,8 @@ blocking_read(int fd, char *buf, int nbytes)
        return (ret);
 }
 
-/* Reset the non-blocking flag on the specified file descriptor.
+/*
+ * Reset the non-blocking flag on the specified file descriptor.
  * Returns -1 if there was an error, 0 if non-blocking wasn't set,
  * 1 if it was.
  */
@@ -1060,34 +1351,232 @@ reset_nonblock(int fd)
        return (1);
 }
 
-
-/* Like getcwd(), except bsize is ignored if buf is 0 (PATH_MAX is used) */
+/* getcwd(3) equivalent, allocates from ATEMP but doesn't resize */
 char *
-ksh_get_wd(size_t *dlen)
+ksh_get_wd(void)
 {
-       char *ret, *b;
-       size_t len = 1;
-
-#ifdef NO_PATH_MAX
-       if ((b = get_current_dir_name())) {
-               len = strlen(b) + 1;
-               strndupx(ret, b, len - 1, ATEMP);
-               free(b);
+#ifdef MKSH__NO_PATH_MAX
+       char *rv, *cp;
+
+       if ((cp = get_current_dir_name())) {
+               strdupx(rv, cp, ATEMP);
+               free_gnu_gcdn(cp);
        } else
-               ret = NULL;
+               rv = NULL;
 #else
-       if ((ret = getcwd((b = alloc(PATH_MAX + 1, ATEMP)), PATH_MAX)))
-               ret = aresize(b, len = (strlen(b) + 1), ATEMP);
-       else
-               afree(b, ATEMP);
+       char *rv;
+
+       if (!getcwd((rv = alloc(PATH_MAX + 1, ATEMP)), PATH_MAX)) {
+               afree(rv, ATEMP);
+               rv = NULL;
+       }
 #endif
 
-       if (dlen)
-               *dlen = len;
-       return (ret);
+       return (rv);
 }
 
-/*
+#ifndef ELOOP
+#define ELOOP          E2BIG
+#endif
+
+char *
+do_realpath(const char *upath)
+{
+       char *xp, *ip, *tp, *ipath, *ldest = NULL;
+       XString xs;
+       ptrdiff_t pos;
+       size_t len;
+       int llen;
+       struct stat sb;
+#ifdef MKSH__NO_PATH_MAX
+       size_t ldestlen = 0;
+#define pathlen sb.st_size
+#define pathcnd (ldestlen < (pathlen + 1))
+#else
+#define pathlen PATH_MAX
+#define pathcnd (!ldest)
+#endif
+       /* max. recursion depth */
+       int symlinks = 32;
+
+       if (upath[0] == '/') {
+               /* upath is an absolute pathname */
+               strdupx(ipath, upath, ATEMP);
+       } else {
+               /* upath is a relative pathname, prepend cwd */
+               if ((tp = ksh_get_wd()) == NULL || tp[0] != '/')
+                       return (NULL);
+               ipath = shf_smprintf("%s%s%s", tp, "/", upath);
+               afree(tp, ATEMP);
+       }
+
+       /* ipath and upath are in memory at the same time -> unchecked */
+       Xinit(xs, xp, strlen(ip = ipath) + 1, ATEMP);
+
+       /* now jump into the deep of the loop */
+       goto beginning_of_a_pathname;
+
+       while (*ip) {
+               /* skip slashes in input */
+               while (*ip == '/')
+                       ++ip;
+               if (!*ip)
+                       break;
+
+               /* get next pathname component from input */
+               tp = ip;
+               while (*ip && *ip != '/')
+                       ++ip;
+               len = ip - tp;
+
+               /* check input for "." and ".." */
+               if (tp[0] == '.') {
+                       if (len == 1)
+                               /* just continue with the next one */
+                               continue;
+                       else if (len == 2 && tp[1] == '.') {
+                               /* strip off last pathname component */
+                               while (xp > Xstring(xs, xp))
+                                       if (*--xp == '/')
+                                               break;
+                               /* then continue with the next one */
+                               continue;
+                       }
+               }
+
+               /* store output position away, then append slash to output */
+               pos = Xsavepos(xs, xp);
+               /* 1 for the '/' and len + 1 for tp and the NUL from below */
+               XcheckN(xs, xp, 1 + len + 1);
+               Xput(xs, xp, '/');
+
+               /* append next pathname component to output */
+               memcpy(xp, tp, len);
+               xp += len;
+               *xp = '\0';
+
+               /* lstat the current output, see if it's a symlink */
+               if (mksh_lstat(Xstring(xs, xp), &sb)) {
+                       /* lstat failed */
+                       if (errno == ENOENT) {
+                               /* because the pathname does not exist */
+                               while (*ip == '/')
+                                       /* skip any trailing slashes */
+                                       ++ip;
+                               /* no more components left? */
+                               if (!*ip)
+                                       /* we can still return successfully */
+                                       break;
+                               /* more components left? fall through */
+                       }
+                       /* not ENOENT or not at the end of ipath */
+                       goto notfound;
+               }
+
+               /* check if we encountered a symlink? */
+               if (S_ISLNK(sb.st_mode)) {
+#ifndef MKSH__NO_SYMLINK
+                       /* reached maximum recursion depth? */
+                       if (!symlinks--) {
+                               /* yep, prevent infinite loops */
+                               errno = ELOOP;
+                               goto notfound;
+                       }
+
+                       /* get symlink(7) target */
+                       if (pathcnd) {
+#ifdef MKSH__NO_PATH_MAX
+                               if (notoktoadd(pathlen, 1)) {
+                                       errno = ENAMETOOLONG;
+                                       goto notfound;
+                               }
+#endif
+                               ldest = aresize(ldest, pathlen + 1, ATEMP);
+                       }
+                       llen = readlink(Xstring(xs, xp), ldest, pathlen);
+                       if (llen < 0)
+                               /* oops... */
+                               goto notfound;
+                       ldest[llen] = '\0';
+
+                       /*
+                        * restart if symlink target is an absolute path,
+                        * otherwise continue with currently resolved prefix
+                        */
+                       /* append rest of current input path to link target */
+                       tp = shf_smprintf("%s%s%s", ldest, *ip ? "/" : "", ip);
+                       afree(ipath, ATEMP);
+                       ip = ipath = tp;
+                       if (ldest[0] != '/') {
+                               /* symlink target is a relative path */
+                               xp = Xrestpos(xs, xp, pos);
+                       } else
+#endif
+                         {
+                               /* symlink target is an absolute path */
+                               xp = Xstring(xs, xp);
+ beginning_of_a_pathname:
+                               /* assert: (ip == ipath)[0] == '/' */
+                               /* assert: xp == xs.beg => start of path */
+
+                               /* exactly two leading slashes? (SUSv4 3.266) */
+                               if (ip[1] == '/' && ip[2] != '/') {
+                                       /* keep them, e.g. for UNC pathnames */
+                                       Xput(xs, xp, '/');
+                               }
+                       }
+               }
+               /* otherwise (no symlink) merely go on */
+       }
+
+       /*
+        * either found the target and successfully resolved it,
+        * or found its parent directory and may create it
+        */
+       if (Xlength(xs, xp) == 0)
+               /*
+                * if the resolved pathname is "", make it "/",
+                * otherwise do not add a trailing slash
+                */
+               Xput(xs, xp, '/');
+       Xput(xs, xp, '\0');
+
+       /*
+        * if source path had a trailing slash, check if target path
+        * is not a non-directory existing file
+        */
+       if (ip > ipath && ip[-1] == '/') {
+               if (stat(Xstring(xs, xp), &sb)) {
+                       if (errno != ENOENT)
+                               goto notfound;
+               } else if (!S_ISDIR(sb.st_mode)) {
+                       errno = ENOTDIR;
+                       goto notfound;
+               }
+               /* target now either does not exist or is a directory */
+       }
+
+       /* return target path */
+       if (ldest != NULL)
+               afree(ldest, ATEMP);
+       afree(ipath, ATEMP);
+       return (Xclose(xs, xp));
+
+ notfound:
+       /* save; freeing memory might trash it */
+       llen = errno;
+       if (ldest != NULL)
+               afree(ldest, ATEMP);
+       afree(ipath, ATEMP);
+       Xfree(xs, xp);
+       errno = llen;
+       return (NULL);
+
+#undef pathlen
+#undef pathcnd
+}
+
+/**
  *     Makes a filename into result using the following algorithm.
  *     - make result NULL
  *     - if file starts with '/', append file to result & set cdpathp to NULL
@@ -1102,16 +1591,17 @@ ksh_get_wd(size_t *dlen)
  *     The return value indicates whether a non-null element from cdpathp
  *     was appended to result.
  */
-int
+static int
 make_path(const char *cwd, const char *file,
-    char **cdpathp,            /* & of : separated list */
+    /* pointer to colon-separated list */
+    char **cdpathp,
     XString *xsp,
     int *phys_pathp)
 {
        int rval = 0;
        bool use_cdpath = true;
        char *plist;
-       int len, plen = 0;
+       size_t len, plen = 0;
        char *xp = Xstring(*xsp, xp);
 
        if (!file)
@@ -1172,170 +1662,375 @@ make_path(const char *cwd, const char *file,
        return (rval);
 }
 
-/*
+/*-
  * Simplify pathnames containing "." and ".." entries.
- * ie, simplify_path("/a/b/c/./../d/..") returns "/a/b"
+ *
+ * simplify_path(this)                 = that
+ * /a/b/c/./../d/..                    /a/b
+ * //./C/foo/bar/../baz                        //C/foo/baz
+ * /foo/                               /foo
+ * /foo/../../bar                      /bar
+ * /foo/./blah/..                      /foo
+ * .                                   .
+ * ..                                  ..
+ * ./foo                               foo
+ * foo/../../../bar                    ../../bar
  */
 void
-simplify_path(char *pathl)
+simplify_path(char *p)
 {
-       char *cur, *t;
-       bool isrooted;
-       char *very_start = pathl, *start;
+       char *dp, *ip, *sp, *tp;
+       size_t len;
+       bool needslash;
 
-       if (!*pathl)
+       switch (*p) {
+       case 0:
                return;
+       case '/':
+               /* exactly two leading slashes? (SUSv4 3.266) */
+               if (p[1] == '/' && p[2] != '/')
+                       /* keep them, e.g. for UNC pathnames */
+                       ++p;
+               needslash = true;
+               break;
+       default:
+               needslash = false;
+       }
+       dp = ip = sp = p;
 
-       if ((isrooted = pathl[0] == '/'))
-               very_start++;
-
-       /* Before                       After
-        * /foo/                        /foo
-        * /foo/../../bar               /bar
-        * /foo/./blah/..               /foo
-        * .                            .
-        * ..                           ..
-        * ./foo                        foo
-        * foo/../../../bar             ../../bar
-        */
-
-       for (cur = t = start = very_start; ; ) {
-               /* treat multiple '/'s as one '/' */
-               while (*t == '/')
-                       t++;
-
-               if (*t == '\0') {
-                       if (cur == pathl)
-                               /* convert empty path to dot */
-                               *cur++ = '.';
-                       *cur = '\0';
+       while (*ip) {
+               /* skip slashes in input */
+               while (*ip == '/')
+                       ++ip;
+               if (!*ip)
                        break;
-               }
 
-               if (t[0] == '.') {
-                       if (!t[1] || t[1] == '/') {
-                               t += 1;
+               /* get next pathname component from input */
+               tp = ip;
+               while (*ip && *ip != '/')
+                       ++ip;
+               len = ip - tp;
+
+               /* check input for "." and ".." */
+               if (tp[0] == '.') {
+                       if (len == 1)
+                               /* just continue with the next one */
                                continue;
-                       } else if (t[1] == '.' && (!t[2] || t[2] == '/')) {
-                               if (!isrooted && cur == start) {
-                                       if (cur != very_start)
-                                               *cur++ = '/';
-                                       *cur++ = '.';
-                                       *cur++ = '.';
-                                       start = cur;
-                               } else if (cur != start)
-                                       while (--cur > start && *cur != '/')
-                                               ;
-                               t += 2;
+                       else if (len == 2 && tp[1] == '.') {
+                               /* parent level, but how? */
+                               if (*p == '/')
+                                       /* absolute path, only one way */
+                                       goto strip_last_component;
+                               else if (dp > sp) {
+                                       /* relative path, with subpaths */
+                                       needslash = false;
+ strip_last_component:
+                                       /* strip off last pathname component */
+                                       while (dp > sp)
+                                               if (*--dp == '/')
+                                                       break;
+                               } else {
+                                       /* relative path, at its beginning */
+                                       if (needslash)
+                                               /* or already dotdot-slash'd */
+                                               *dp++ = '/';
+                                       /* keep dotdot-slash if not absolute */
+                                       *dp++ = '.';
+                                       *dp++ = '.';
+                                       needslash = true;
+                                       sp = dp;
+                               }
+                               /* then continue with the next one */
                                continue;
                        }
                }
 
-               if (cur != very_start)
-                       *cur++ = '/';
+               if (needslash)
+                       *dp++ = '/';
+
+               /* append next pathname component to output */
+               memmove(dp, tp, len);
+               dp += len;
 
-               /* find/copy next component of pathname */
-               while (*t && *t != '/')
-                       *cur++ = *t++;
+               /* append slash if we continue */
+               needslash = true;
+               /* try next component */
        }
+       if (dp == p)
+               /* empty path -> dot */
+               *dp++ = needslash ? '/' : '.';
+       *dp = '\0';
 }
 
-
 void
-set_current_wd(char *pathl)
+set_current_wd(const char *nwd)
 {
-       size_t len = 1;
-       char *p = pathl;
+       char *allocd = NULL;
 
-       if (p == NULL) {
-               if ((p = ksh_get_wd(&len)) == NULL)
-                       p = null;
-       } else
-               len = strlen(p) + 1;
+       if (nwd == NULL) {
+               allocd = ksh_get_wd();
+               nwd = allocd ? allocd : null;
+       }
 
-       if (len > current_wd_size) {
-               afree(current_wd, APERM);
-               current_wd = alloc(current_wd_size = len, APERM);
+       afree(current_wd, APERM);
+       strdupx(current_wd, nwd, APERM);
+
+       afree(allocd, ATEMP);
+}
+
+int
+c_cd(const char **wp)
+{
+       int optc, rv, phys_path;
+       bool physical = tobool(Flag(FPHYSICAL));
+       /* was a node from cdpath added in? */
+       int cdnode;
+       /* show where we went?, error for $PWD */
+       bool printpath = false, eflag = false;
+       struct tbl *pwd_s, *oldpwd_s;
+       XString xs;
+       char *dir, *allocd = NULL, *tryp, *pwd, *cdpath;
+
+       while ((optc = ksh_getopt(wp, &builtin_opt, "eLP")) != -1)
+               switch (optc) {
+               case 'e':
+                       eflag = true;
+                       break;
+               case 'L':
+                       physical = false;
+                       break;
+               case 'P':
+                       physical = true;
+                       break;
+               case '?':
+                       return (2);
+               }
+       wp += builtin_opt.optind;
+
+       if (Flag(FRESTRICTED)) {
+               bi_errorf("restricted shell - can't cd");
+               return (2);
+       }
+
+       pwd_s = global("PWD");
+       oldpwd_s = global("OLDPWD");
+
+       if (!wp[0]) {
+               /* No arguments - go home */
+               if ((dir = str_val(global("HOME"))) == null) {
+                       bi_errorf("no home directory (HOME not set)");
+                       return (2);
+               }
+       } else if (!wp[1]) {
+               /* One argument: - or dir */
+               strdupx(allocd, wp[0], ATEMP);
+               if (ksh_isdash((dir = allocd))) {
+                       afree(allocd, ATEMP);
+                       allocd = NULL;
+                       dir = str_val(oldpwd_s);
+                       if (dir == null) {
+                               bi_errorf("no OLDPWD");
+                               return (2);
+                       }
+                       printpath = true;
+               }
+       } else if (!wp[2]) {
+               /* Two arguments - substitute arg1 in PWD for arg2 */
+               size_t ilen, olen, nlen, elen;
+               char *cp;
+
+               if (!current_wd[0]) {
+                       bi_errorf("can't determine current directory");
+                       return (2);
+               }
+               /*
+                * substitute arg1 for arg2 in current path.
+                * if the first substitution fails because the cd fails
+                * we could try to find another substitution. For now
+                * we don't
+                */
+               if ((cp = strstr(current_wd, wp[0])) == NULL) {
+                       bi_errorf("bad substitution");
+                       return (2);
+               }
+               /*-
+                * ilen = part of current_wd before wp[0]
+                * elen = part of current_wd after wp[0]
+                * because current_wd and wp[1] need to be in memory at the
+                * same time beforehand the addition can stay unchecked
+                */
+               ilen = cp - current_wd;
+               olen = strlen(wp[0]);
+               nlen = strlen(wp[1]);
+               elen = strlen(current_wd + ilen + olen) + 1;
+               dir = allocd = alloc(ilen + nlen + elen, ATEMP);
+               memcpy(dir, current_wd, ilen);
+               memcpy(dir + ilen, wp[1], nlen);
+               memcpy(dir + ilen + nlen, current_wd + ilen + olen, elen);
+               printpath = true;
+       } else {
+               bi_errorf("too many arguments");
+               return (2);
        }
-       memcpy(current_wd, p, len);
-       if (p != pathl && p != null)
-               afree(p, ATEMP);
+
+#ifdef MKSH__NO_PATH_MAX
+       /* only a first guess; make_path will enlarge xs if necessary */
+       XinitN(xs, 1024, ATEMP);
+#else
+       XinitN(xs, PATH_MAX, ATEMP);
+#endif
+
+       cdpath = str_val(global("CDPATH"));
+       do {
+               cdnode = make_path(current_wd, dir, &cdpath, &xs, &phys_path);
+               if (physical)
+                       rv = chdir(tryp = Xstring(xs, xp) + phys_path);
+               else {
+                       simplify_path(Xstring(xs, xp));
+                       rv = chdir(tryp = Xstring(xs, xp));
+               }
+       } while (rv < 0 && cdpath != NULL);
+
+       if (rv < 0) {
+               if (cdnode)
+                       bi_errorf("%s: %s", dir, "bad directory");
+               else
+                       bi_errorf("%s: %s", tryp, cstrerror(errno));
+               afree(allocd, ATEMP);
+               Xfree(xs, xp);
+               return (2);
+       }
+
+       rv = 0;
+
+       /* allocd (above) => dir, which is no longer used */
+       afree(allocd, ATEMP);
+       allocd = NULL;
+
+       /* Clear out tracked aliases with relative paths */
+       flushcom(false);
+
+       /*
+        * Set OLDPWD (note: unsetting OLDPWD does not disable this
+        * setting in AT&T ksh)
+        */
+       if (current_wd[0])
+               /* Ignore failure (happens if readonly or integer) */
+               setstr(oldpwd_s, current_wd, KSH_RETURN_ERROR);
+
+       if (Xstring(xs, xp)[0] != '/') {
+               pwd = NULL;
+       } else if (!physical) {
+               goto norealpath_PWD;
+       } else if ((pwd = allocd = do_realpath(Xstring(xs, xp))) == NULL) {
+               if (eflag)
+                       rv = 1;
+ norealpath_PWD:
+               pwd = Xstring(xs, xp);
+       }
+
+       /* Set PWD */
+       if (pwd) {
+               char *ptmp = pwd;
+
+               set_current_wd(ptmp);
+               /* Ignore failure (happens if readonly or integer) */
+               setstr(pwd_s, ptmp, KSH_RETURN_ERROR);
+       } else {
+               set_current_wd(null);
+               pwd = Xstring(xs, xp);
+               /* XXX unset $PWD? */
+               if (eflag)
+                       rv = 1;
+       }
+       if (printpath || cdnode)
+               shprintf("%s\n", pwd);
+
+       afree(allocd, ATEMP);
+       Xfree(xs, xp);
+       return (rv);
 }
 
-#ifdef TIOCSCTTY
+
+#ifdef KSH_CHVT_CODE
 extern void chvt_reinit(void);
 
 static void
-chvt(const char *fn)
+chvt(const Getopt *go)
 {
-       char dv[20];
-       struct stat sb;
+       const char *dv = go->optarg;
+       char *cp = NULL;
        int fd;
 
-       /* for entropy */
-       kshstate_f.h = evilhash(fn);
+       switch (*dv) {
+       case '-':
+               dv = "/dev/null";
+               break;
+       case '!':
+               ++dv;
+               /* FALLTHROUGH */
+       default: {
+               struct stat sb;
 
-       if (*fn == '-') {
-               memcpy(dv, "-/dev/null", sizeof("-/dev/null"));
-               fn = dv + 1;
-       } else {
-               if (stat(fn, &sb)) {
-                       memcpy(dv, "/dev/ttyC", 9);
-                       strlcpy(dv + 9, fn, sizeof(dv) - 9);
+               if (stat(dv, &sb)) {
+                       cp = shf_smprintf("/dev/ttyC%s", dv);
+                       dv = cp;
                        if (stat(dv, &sb)) {
-                               strlcpy(dv + 8, fn, sizeof(dv) - 8);
-                               if (stat(dv, &sb))
-                                       errorf("chvt: can't find tty %s", fn);
+                               memmove(cp + 1, cp, /* /dev/tty */ 8);
+                               dv = cp + 1;
+                               if (stat(dv, &sb)) {
+                                       errorf("%s: %s: %s", "chvt",
+                                           "can't find tty", go->optarg);
+                               }
                        }
-                       fn = dv;
                }
                if (!(sb.st_mode & S_IFCHR))
-                       errorf("chvt: not a char device: %s", fn);
-               if ((sb.st_uid != 0) && chown(fn, 0, 0))
-                       warningf(false, "chvt: cannot chown root %s", fn);
-               if (((sb.st_mode & 07777) != 0600) && chmod(fn, (mode_t)0600))
-                       warningf(false, "chvt: cannot chmod 0600 %s", fn);
+                       errorf("%s: %s: %s", "chvt", "not a char device", dv);
+#ifndef MKSH_DISABLE_REVOKE_WARNING
 #if HAVE_REVOKE
-               if (revoke(fn))
+               if (revoke(dv))
+#endif
+                       warningf(false, "%s: %s %s", "chvt",
+                           "new shell is potentially insecure, can't revoke",
+                           dv);
 #endif
-                       warningf(false, "chvt: cannot revoke %s, new shell is"
-                           " potentially insecure", fn);
+           }
        }
-       if ((fd = open(fn, O_RDWR)) == -1) {
+       if ((fd = open(dv, O_RDWR | O_BINARY)) < 0) {
                sleep(1);
-               if ((fd = open(fn, O_RDWR)) == -1)
-                       errorf("chvt: cannot open %s", fn);
+               if ((fd = open(dv, O_RDWR | O_BINARY)) < 0) {
+                       errorf("%s: %s %s", "chvt", "can't open", dv);
+               }
        }
-       switch (fork()) {
-       case -1:
-               errorf("chvt: %s failed", "fork");
-       case 0:
-               break;
-       default:
-               exit(0);
+       if (go->optarg[0] != '!') {
+               switch (fork()) {
+               case -1:
+                       errorf("%s: %s %s", "chvt", "fork", "failed");
+               case 0:
+                       break;
+               default:
+                       exit(0);
+               }
        }
        if (setsid() == -1)
-               errorf("chvt: %s failed", "setsid");
-       if (fn != dv + 1) {
+               errorf("%s: %s %s", "chvt", "setsid", "failed");
+       if (go->optarg[0] != '-') {
                if (ioctl(fd, TIOCSCTTY, NULL) == -1)
-                       errorf("chvt: %s failed", "TIOCSCTTY");
+                       errorf("%s: %s %s", "chvt", "TIOCSCTTY", "failed");
                if (tcflush(fd, TCIOFLUSH))
-                       errorf("chvt: %s failed", "TCIOFLUSH");
+                       errorf("%s: %s %s", "chvt", "TCIOFLUSH", "failed");
        }
        ksh_dup2(fd, 0, false);
        ksh_dup2(fd, 1, false);
        ksh_dup2(fd, 2, false);
        if (fd > 2)
                close(fd);
+       rndset((unsigned long)chvt_rndsetup(go, sizeof(Getopt)));
        chvt_reinit();
 }
 #endif
 
 #ifdef DEBUG
-char longsizes_are_okay[sizeof(long) == sizeof(unsigned long) ? 1 : -1];
-char arisize_is_okay[sizeof(mksh_ari_t) == 4 ? 1 : -1];
-char uarisize_is_okay[sizeof(mksh_uari_t) == 4 ? 1 : -1];
-
 char *
 strchr(char *p, int ch)
 {
@@ -1367,31 +2062,9 @@ strstr(char *b, const char *l)
 }
 #endif
 
-#ifndef MKSH_ASSUME_UTF8
-#if !HAVE_STRCASESTR
-const char *
-stristr(const char *b, const char *l)
-{
-       char first, c;
-       size_t n;
-
-       if ((first = *l++), ((first = ksh_tolower(first)) == '\0'))
-               return (b);
-       n = strlen(l);
- stristr_look:
-       while ((c = *b++), ((c = ksh_tolower(c)) != first))
-               if (c == '\0')
-                       return (NULL);
-       if (strncasecmp(b, l, n))
-               goto stristr_look;
-       return (b - 1);
-}
-#endif
-#endif
-
-#ifdef MKSH_SMALL
+#if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST)
 char *
-strndup_(const char *src, size_t len, Area *ap)
+strndup_i(const char *src, size_t len, Area *ap)
 {
        char *dst = NULL;
 
@@ -1404,9 +2077,9 @@ strndup_(const char *src, size_t len, Area *ap)
 }
 
 char *
-strdup_(const char *src, Area *ap)
+strdup_i(const char *src, Area *ap)
 {
-       return (src == NULL ? NULL : strndup_(src, strlen(src), ap));
+       return (src == NULL ? NULL : strndup_i(src, strlen(src), ap));
 }
 #endif
 
@@ -1527,15 +2200,15 @@ unbksl(bool cstyle, int (*fg)(void), void (*fp)(int))
                break;
        case 'U':
                i = 8;
-               if (0)
+               if (/* CONSTCOND */ 0)
                /* FALLTHROUGH */
        case 'u':
                i = 4;
-               if (0)
+               if (/* CONSTCOND */ 0)
                /* FALLTHROUGH */
        case 'x':
                i = cstyle ? -1 : 2;
-               /*
+               /**
                 * x:   look for a hexadecimal number with up to
                 *      two (C style: arbitrary) digits; convert
                 *      to raw octet (C style: Unicode if >0xFF)