Use glibc_likely instead __builtin_expect.
[jlayton/glibc.git] / posix / wordexp.c
index 42b22ec8de61695623ea8478326303c10980e68c..b6b65dd993ec7e2ee5e3e88f8800c2e254cb9d5f 100644 (file)
 /* POSIX.2 wordexp implementation.
-   Copyright (C) 1997 Free Software Foundation, Inc.
+   Copyright (C) 1997-2014 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Tim Waugh <tim@cyberelk.demon.co.uk>.
 
    The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Library General Public License as
-   published by the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
 
    The GNU C Library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Library General Public License for more details.
+   Lesser General Public License for more details.
 
-   You should have received a copy of the GNU Library General Public
-   License along with the GNU C Library; see the file COPYING.LIB.  If not,
-   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-   Boston, MA 02111-1307, USA.  */
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
 
-#include <wordexp.h>
+#include <alloca.h>
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <fnmatch.h>
+#include <glob.h>
+#include <libintl.h>
+#include <paths.h>
+#include <pwd.h>
 #include <signal.h>
+#include <stdbool.h>
+#include <stdio.h>
 #include <stdlib.h>
-#include <pwd.h>
-#include <sys/types.h>
 #include <string.h>
-#include <glob.h>
-#include <ctype.h>
+#include <sys/param.h>
+#include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/types.h>
+#include <sys/types.h>
 #include <sys/wait.h>
 #include <unistd.h>
-#include <fcntl.h>
-#include <sys/stat.h>
-#include <paths.h>
-#include <errno.h>
-#include <sys/param.h>
-#include <stdio.h>
-#include <fnmatch.h>
+#include <wchar.h>
+#include <wordexp.h>
+#include <kernel-features.h>
 
-#include <stdio-common/_itoa.h>
+#include <bits/libc-lock.h>
+#include <_itoa.h>
 
 /* Undefine the following line for the production version.  */
 /* #define NDEBUG 1 */
 #include <assert.h>
 
+/* Get some device information.  */
+#include <device-nrs.h>
+
 /*
  * This is a recursive-descent-style word expansion routine.
  */
 
+/* These variables are defined and initialized in the startup code.  */
+extern int __libc_argc attribute_hidden;
+extern char **__libc_argv attribute_hidden;
+
 /* Some forward declarations */
 static int parse_dollars (char **word, size_t *word_length, size_t *max_length,
                          const char *words, size_t *offset, int flags,
-                         wordexp_t *pwordexp)
+                         wordexp_t *pwordexp, const char *ifs,
+                         const char *ifs_white, int quoted)
      internal_function;
 static int parse_backtick (char **word, size_t *word_length,
                           size_t *max_length, const char *words,
-                          size_t *offset, int flags, wordexp_t *pwordexp)
+                          size_t *offset, int flags, wordexp_t *pwordexp,
+                          const char *ifs, const char *ifs_white)
      internal_function;
-static int eval_expr (char *expr, int *result) internal_function;
+static int parse_dquote (char **word, size_t *word_length, size_t *max_length,
+                        const char *words, size_t *offset, int flags,
+                        wordexp_t *pwordexp, const char *ifs,
+                        const char *ifs_white)
+     internal_function;
+static int eval_expr (char *expr, long int *result) internal_function;
 
 /* The w_*() functions manipulate word lists. */
 
 #define W_CHUNK        (100)
 
+/* Result of w_newword will be ignored if it's the last word. */
 static inline char *
+w_newword (size_t *actlen, size_t *maxlen)
+{
+  *actlen = *maxlen = 0;
+  return NULL;
+}
+
+static char *
 w_addchar (char *buffer, size_t *actlen, size_t *maxlen, char ch)
      /* (lengths exclude trailing zero) */
 {
-  /* Add a character to the buffer, allocating room for it if needed.
-   */
+  /* Add a character to the buffer, allocating room for it if needed.  */
 
   if (*actlen == *maxlen)
     {
       char *old_buffer = buffer;
       assert (buffer == NULL || *maxlen != 0);
       *maxlen += W_CHUNK;
-      buffer = realloc (buffer, 1 + *maxlen);
+      buffer = (char *) realloc (buffer, 1 + *maxlen);
 
       if (buffer == NULL)
        free (old_buffer);
@@ -91,16 +117,12 @@ w_addchar (char *buffer, size_t *actlen, size_t *maxlen, char ch)
 }
 
 static char *
-w_addstr (char *buffer, size_t *actlen, size_t *maxlen, const char *str)
-     /* (lengths exclude trailing zero) */
+internal_function
+w_addmem (char *buffer, size_t *actlen, size_t *maxlen, const char *str,
+         size_t len)
 {
   /* Add a string to the buffer, allocating room for it if needed.
    */
-  size_t len;
-
-  assert (str != NULL); /* w_addstr only called from this file */
-  len = strlen (str);
-
   if (*actlen + len > *maxlen)
     {
       char *old_buffer = buffer;
@@ -121,21 +143,55 @@ w_addstr (char *buffer, size_t *actlen, size_t *maxlen, const char *str)
   return buffer;
 }
 
+static char *
+internal_function
+w_addstr (char *buffer, size_t *actlen, size_t *maxlen, const char *str)
+     /* (lengths exclude trailing zero) */
+{
+  /* Add a string to the buffer, allocating room for it if needed.
+   */
+  size_t len;
+
+  assert (str != NULL); /* w_addstr only called from this file */
+  len = strlen (str);
+
+  return w_addmem (buffer, actlen, maxlen, str, len);
+}
+
 static int
+internal_function
 w_addword (wordexp_t *pwordexp, char *word)
 {
   /* Add a word to the wordlist */
   size_t num_p;
+  char **new_wordv;
+  bool allocated = false;
+
+  /* Internally, NULL acts like "".  Convert NULLs to "" before
+   * the caller sees them.
+   */
+  if (word == NULL)
+    {
+      word = __strdup ("");
+      if (word == NULL)
+       goto no_space;
+      allocated = true;
+    }
 
   num_p = 2 + pwordexp->we_wordc + pwordexp->we_offs;
-  pwordexp->we_wordv = realloc (pwordexp->we_wordv, sizeof (char *) * num_p);
-  if (pwordexp->we_wordv != NULL)
+  new_wordv = realloc (pwordexp->we_wordv, sizeof (char *) * num_p);
+  if (new_wordv != NULL)
     {
-      pwordexp->we_wordv[pwordexp->we_wordc++] = word;
-      pwordexp->we_wordv[pwordexp->we_wordc] = NULL;
+      pwordexp->we_wordv = new_wordv;
+      pwordexp->we_wordv[pwordexp->we_offs + pwordexp->we_wordc++] = word;
+      pwordexp->we_wordv[pwordexp->we_offs + pwordexp->we_wordc] = NULL;
       return 0;
     }
 
+  if (allocated)
+    free (word);
+
+no_space:
   return WRDE_NOSPACE;
 }
 
@@ -227,8 +283,8 @@ parse_tilde (char **word, size_t *word_length, size_t *max_length,
     {
       if (!((*word)[*word_length - 1] == '=' && wordc == 0))
        {
-         if (!((*word)[*word_length - 1] == ':' &&
-               strchr (*word, '=') && wordc == 0))
+         if (!((*word)[*word_length - 1] == ':'
+               && strchr (*word, '=') && wordc == 0))
            {
              *word = w_addchar (*word, word_length, max_length, '~');
              return *word ? 0 : WRDE_NOSPACE;
@@ -255,35 +311,48 @@ parse_tilde (char **word, size_t *word_length, size_t *max_length,
       uid_t uid;
       struct passwd pwd, *tpwd;
       int buflen = 1000;
-      char* buffer = __alloca (buflen);
+      char* home;
+      char* buffer;
       int result;
 
-      uid = getuid ();
-
-      while ((result = __getpwuid_r (uid, &pwd, buffer, buflen, &tpwd)) != 0
-            && errno == ERANGE)
-       {
-         buflen += 1000;
-         buffer = __alloca (buflen);
-       }
+      /* POSIX.2 says ~ expands to $HOME and if HOME is unset the
+        results are unspecified.  We do a lookup on the uid if
+        HOME is unset. */
 
-      if (result == 0 && pwd.pw_dir != NULL)
+      home = getenv ("HOME");
+      if (home != NULL)
        {
-         *word = w_addstr (*word, word_length, max_length, pwd.pw_dir);
+         *word = w_addstr (*word, word_length, max_length, home);
          if (*word == NULL)
            return WRDE_NOSPACE;
        }
       else
        {
-         *word = w_addchar (*word, word_length, max_length, '~');
-         if (*word == NULL)
-           return WRDE_NOSPACE;
+         uid = __getuid ();
+         buffer = __alloca (buflen);
+
+         while ((result = __getpwuid_r (uid, &pwd, buffer, buflen, &tpwd)) != 0
+                && errno == ERANGE)
+           buffer = extend_alloca (buffer, buflen, buflen + 1000);
+
+         if (result == 0 && tpwd != NULL && pwd.pw_dir != NULL)
+           {
+             *word = w_addstr (*word, word_length, max_length, pwd.pw_dir);
+             if (*word == NULL)
+               return WRDE_NOSPACE;
+           }
+         else
+           {
+             *word = w_addchar (*word, word_length, max_length, '~');
+             if (*word == NULL)
+               return WRDE_NOSPACE;
+           }
        }
     }
   else
     {
       /* Look up user name in database to get home directory */
-      char *user = __strndup (&words[1 + *offset], i - *offset);
+      char *user = strndupa (&words[1 + *offset], i - (1 + *offset));
       struct passwd pwd, *tpwd;
       int buflen = 1000;
       char* buffer = __alloca (buflen);
@@ -291,12 +360,9 @@ parse_tilde (char **word, size_t *word_length, size_t *max_length,
 
       while ((result = __getpwnam_r (user, &pwd, buffer, buflen, &tpwd)) != 0
             && errno == ERANGE)
-       {
-         buflen += 1000;
-         buffer = __alloca (buflen);
-       }
+       buffer = extend_alloca (buffer, buflen, buflen + 1000);
 
-      if (result == 0 && pwd.pw_dir)
+      if (result == 0 && tpwd != NULL && pwd.pw_dir)
        *word = w_addstr (*word, word_length, max_length, pwd.pw_dir);
       else
        {
@@ -311,67 +377,31 @@ parse_tilde (char **word, size_t *word_length, size_t *max_length,
   return *word ? 0 : WRDE_NOSPACE;
 }
 
+
 static int
 internal_function
-parse_glob (char **word, size_t *word_length, size_t *max_length,
-           const char *words, size_t *offset, int flags,
-           wordexp_t *pwordexp, char *ifs)
+do_parse_glob (const char *glob_word, char **word, size_t *word_length,
+              size_t *max_length, wordexp_t *pwordexp, const char *ifs,
+              const char *ifs_white)
 {
-  /* We are poised just after a '*' or a '{'. */
   int error;
+  unsigned int match;
   glob_t globbuf;
-  int match;
-  char *matching_word;
-
-  for (; words[*offset]; (*offset)++)
-    switch (words[*offset])
-      {
-      case ' ':
-      case '\t':
-       break;
-
-      case '$':
-       error = parse_dollars (word, word_length, max_length, words, offset,
-                              flags, pwordexp);
-       if (error)
-         return error;
-
-       continue;
-
-      default:
-       if (ifs == NULL || strchr (ifs, words[*offset]) == NULL)
-         {
-           *word = w_addchar (*word, word_length, max_length, words[*offset]);
-           if (*word == NULL)
-             return WRDE_NOSPACE;
-
-           continue;
-         }
-
-       break;
-      }
 
-  error = glob (*word, GLOB_NOCHECK, NULL, &globbuf);
+  error = glob (glob_word, GLOB_NOCHECK, NULL, &globbuf);
 
   if (error != 0)
     {
       /* We can only run into memory problems.  */
       assert (error == GLOB_NOSPACE);
-
       return WRDE_NOSPACE;
     }
 
   if (ifs && !*ifs)
     {
-      /* No field splitting allowed */
-      size_t length = strlen (globbuf.gl_pathv[0]);
-      *word = realloc (*word, length + 1);
-      if (*word == NULL)
-       goto no_space;
-
-      memcpy (*word, globbuf.gl_pathv[0], length + 1);
-      *word_length = length;
-
+      /* No field splitting allowed. */
+      assert (globbuf.gl_pathv[0] != NULL);
+      *word = w_addstr (*word, word_length, max_length, globbuf.gl_pathv[0]);
       for (match = 1; match < globbuf.gl_pathc && *word != NULL; ++match)
        {
          *word = w_addchar (*word, word_length, max_length, ' ');
@@ -380,46 +410,129 @@ parse_glob (char **word, size_t *word_length, size_t *max_length,
                              globbuf.gl_pathv[match]);
        }
 
-      /* Re-parse white space on return */
       globfree (&globbuf);
-      --(*offset);
       return *word ? 0 : WRDE_NOSPACE;
     }
 
-  /* here ifs != "" */
-  free (*word);
-  *word = NULL;
-  *word_length = 0;
+  assert (ifs == NULL || *ifs != '\0');
+  if (*word != NULL)
+    {
+      free (*word);
+      *word = w_newword (word_length, max_length);
+    }
+
+  for (match = 0; match < globbuf.gl_pathc; ++match)
+    {
+      char *matching_word = __strdup (globbuf.gl_pathv[match]);
+      if (matching_word == NULL || w_addword (pwordexp, matching_word))
+       {
+         globfree (&globbuf);
+         return WRDE_NOSPACE;
+       }
+    }
 
-  matching_word = __strdup (globbuf.gl_pathv[0]);
-  if (matching_word == NULL)
-    goto no_space;
+  globfree (&globbuf);
+  return 0;
+}
 
-  if (w_addword (pwordexp, matching_word) == WRDE_NOSPACE)
-    goto no_space;
+static int
+internal_function
+parse_glob (char **word, size_t *word_length, size_t *max_length,
+           const char *words, size_t *offset, int flags,
+           wordexp_t *pwordexp, const char *ifs, const char *ifs_white)
+{
+  /* We are poised just after a '*', a '[' or a '?'. */
+  int error = WRDE_NOSPACE;
+  int quoted = 0; /* 1 if singly-quoted, 2 if doubly */
+  size_t i;
+  wordexp_t glob_list; /* List of words to glob */
 
-  for (match = 1; match < globbuf.gl_pathc; ++match)
+  glob_list.we_wordc = 0;
+  glob_list.we_wordv = NULL;
+  glob_list.we_offs = 0;
+  for (; words[*offset] != '\0'; ++*offset)
     {
-      matching_word = __strdup (globbuf.gl_pathv[match]);
-      if (matching_word == NULL)
-       goto no_space;
+      if (strchr (ifs, words[*offset]) != NULL)
+       /* Reached IFS */
+       break;
 
-      if (w_addword (pwordexp, matching_word) == WRDE_NOSPACE)
-       goto no_space;
+      /* Sort out quoting */
+      if (words[*offset] == '\'')
+       {
+         if (quoted == 0)
+           {
+             quoted = 1;
+             continue;
+           }
+         else if (quoted == 1)
+           {
+             quoted = 0;
+             continue;
+           }
+       }
+      else if (words[*offset] == '"')
+       {
+         if (quoted == 0)
+           {
+             quoted = 2;
+             continue;
+           }
+         else if (quoted == 2)
+           {
+             quoted = 0;
+             continue;
+           }
+       }
+
+      /* Sort out other special characters */
+      if (quoted != 1 && words[*offset] == '$')
+       {
+         error = parse_dollars (word, word_length, max_length, words,
+                                offset, flags, &glob_list, ifs, ifs_white,
+                                quoted == 2);
+         if (error)
+           goto tidy_up;
+
+         continue;
+       }
+      else if (words[*offset] == '\\')
+       {
+         if (quoted)
+           error = parse_qtd_backslash (word, word_length, max_length,
+                                        words, offset);
+         else
+           error = parse_backslash (word, word_length, max_length,
+                                    words, offset);
+
+         if (error)
+           goto tidy_up;
+
+         continue;
+       }
+
+      *word = w_addchar (*word, word_length, max_length, words[*offset]);
+      if (*word == NULL)
+       goto tidy_up;
     }
 
-  globfree (&globbuf);
+  /* Don't forget to re-parse the character we stopped at. */
+  --*offset;
 
-  /* Re-parse white space on return */
-  --(*offset);
-  return 0;
+  /* Glob the words */
+  error = w_addword (&glob_list, *word);
+  *word = w_newword (word_length, max_length);
+  for (i = 0; error == 0 && i < glob_list.we_wordc; i++)
+    error = do_parse_glob (glob_list.we_wordv[i], word, word_length,
+                          max_length, pwordexp, ifs, ifs_white);
 
-no_space:
-  globfree (&globbuf);
-  return WRDE_NOSPACE;
+  /* Now tidy up */
+tidy_up:
+  wordfree (&glob_list);
+  return error;
 }
 
 static int
+internal_function
 parse_squote (char **word, size_t *word_length, size_t *max_length,
              const char *words, size_t *offset)
 {
@@ -442,18 +555,15 @@ parse_squote (char **word, size_t *word_length, size_t *max_length,
 /* Functions to evaluate an arithmetic expression */
 static int
 internal_function
-eval_expr_val (char **expr, int *result)
+eval_expr_val (char **expr, long int *result)
 {
-  int sgn = +1;
   char *digit;
 
   /* Skip white space */
   for (digit = *expr; digit && *digit && isspace (*digit); ++digit);
 
-  switch (*digit)
+  if (*digit == '(')
     {
-    case '(':
-
       /* Scan for closing paren */
       for (++digit; **expr && **expr != ')'; ++(*expr));
 
@@ -467,38 +577,25 @@ eval_expr_val (char **expr, int *result)
        return WRDE_SYNTAX;
 
       return 0;
-
-    case '+':  /* Positive value */
-      ++digit;
-      break;
-
-    case '-':  /* Negative value */
-      ++digit;
-      sgn = -1;
-      break;
-
-    default:
-      if (!isdigit (*digit))
-       return WRDE_SYNTAX;
     }
 
-  *result = 0;
-  for (; *digit && isdigit (*digit); ++digit)
-    *result = (*result * 10) + (*digit - '0');
+  /* POSIX requires that decimal, octal, and hexadecimal constants are
+     recognized.  Therefore we pass 0 as the third parameter to strtol.  */
+  *result = strtol (digit, expr, 0);
+  if (digit == *expr)
+    return WRDE_SYNTAX;
 
-  *expr = digit;
-  *result *= sgn;
   return 0;
 }
 
 static int
 internal_function
-eval_expr_multdiv (char **expr, int *result)
+eval_expr_multdiv (char **expr, long int *result)
 {
-  int arg;
+  long int arg;
 
   /* Read a Value */
-  if (eval_expr_val (expr, result))
+  if (eval_expr_val (expr, result) != 0)
     return WRDE_SYNTAX;
 
   while (**expr)
@@ -508,16 +605,16 @@ eval_expr_multdiv (char **expr, int *result)
 
       if (**expr == '*')
        {
-         (*expr)++;
-         if ((eval_expr_val (expr, &arg)) != 0)
+         ++(*expr);
+         if (eval_expr_val (expr, &arg) != 0)
            return WRDE_SYNTAX;
 
          *result *= arg;
        }
       else if (**expr == '/')
        {
-         (*expr)++;
-         if ((eval_expr_val (expr, &arg)) != 0)
+         ++(*expr);
+         if (eval_expr_val (expr, &arg) != 0)
            return WRDE_SYNTAX;
 
          *result /= arg;
@@ -530,12 +627,12 @@ eval_expr_multdiv (char **expr, int *result)
 
 static int
 internal_function
-eval_expr (char *expr, int *result)
+eval_expr (char *expr, long int *result)
 {
-  int arg;
+  long int arg;
 
   /* Read a Multdiv */
-  if ((eval_expr_multdiv (&expr, result)) != 0)
+  if (eval_expr_multdiv (&expr, result) != 0)
     return WRDE_SYNTAX;
 
   while (*expr)
@@ -545,16 +642,16 @@ eval_expr (char *expr, int *result)
 
       if (*expr == '+')
        {
-         expr++;
-         if ((eval_expr_multdiv (&expr, &arg)) != 0)
+         ++expr;
+         if (eval_expr_multdiv (&expr, &arg) != 0)
            return WRDE_SYNTAX;
 
          *result += arg;
        }
       else if (*expr == '-')
        {
-         expr++;
-         if ((eval_expr_multdiv (&expr, &arg)) != 0)
+         ++expr;
+         if (eval_expr_multdiv (&expr, &arg) != 0)
            return WRDE_SYNTAX;
 
          *result -= arg;
@@ -573,18 +670,19 @@ parse_arith (char **word, size_t *word_length, size_t *max_length,
   /* We are poised just after "$((" or "$[" */
   int error;
   int paren_depth = 1;
-  size_t expr_length = 0;
-  size_t expr_maxlen = 0;
-  char *expr = NULL;
+  size_t expr_length;
+  size_t expr_maxlen;
+  char *expr;
 
+  expr = w_newword (&expr_length, &expr_maxlen);
   for (; words[*offset]; ++(*offset))
     {
       switch (words[*offset])
        {
        case '$':
          error = parse_dollars (&expr, &expr_length, &expr_maxlen,
-                                words, offset, flags, NULL);
-         /* The NULL here is to tell parse_dollars not to
+                                words, offset, flags, NULL, NULL, NULL, 1);
+         /* The ``1'' here is to tell parse_dollars not to
           * split the fields.
           */
          if (error)
@@ -597,8 +695,8 @@ parse_arith (char **word, size_t *word_length, size_t *max_length,
        case '`':
          (*offset)++;
          error = parse_backtick (&expr, &expr_length, &expr_maxlen,
-                                 words, offset, flags, NULL);
-         /* The NULL here is to tell parse_backtick not to
+                                 words, offset, flags, NULL, NULL, NULL);
+         /* The first NULL here is to tell parse_backtick not to
           * split the fields.
           */
          if (error)
@@ -626,21 +724,40 @@ parse_arith (char **word, size_t *word_length, size_t *max_length,
          if (--paren_depth == 0)
            {
              char result[21];  /* 21 = ceil(log10(2^64)) + 1 */
-             int numresult = 0;
+             long int numresult = 0;
+             long long int convertme;
 
              if (bracket || words[1 + *offset] != ')')
-               return WRDE_SYNTAX;
+               {
+                 free (expr);
+                 return WRDE_SYNTAX;
+               }
 
              ++(*offset);
 
              /* Go - evaluate. */
-             if (*expr &&
-                 eval_expr (expr, &numresult) != 0)
-               return WRDE_SYNTAX;
+             if (*expr && eval_expr (expr, &numresult) != 0)
+               {
+                 free (expr);
+                 return WRDE_SYNTAX;
+               }
+
+             if (numresult < 0)
+               {
+                 convertme = -numresult;
+                 *word = w_addchar (*word, word_length, max_length, '-');
+                 if (!*word)
+                   {
+                     free (expr);
+                     return WRDE_NOSPACE;
+                   }
+               }
+             else
+               convertme = numresult;
 
              result[20] = '\0';
              *word = w_addstr (*word, word_length, max_length,
-                               _itoa_word (numresult, &result[20], 10, 0));
+                               _itoa (convertme, &result[20], 10, 0));
              free (expr);
              return *word ? 0 : WRDE_NOSPACE;
            }
@@ -654,11 +771,14 @@ parse_arith (char **word, size_t *word_length, size_t *max_length,
          if (bracket && paren_depth == 1)
            {
              char result[21];  /* 21 = ceil(log10(2^64)) + 1 */
-             int numresult = 0;
+             long int numresult = 0;
 
              /* Go - evaluate. */
              if (*expr && eval_expr (expr, &numresult) != 0)
-               return WRDE_SYNTAX;
+               {
+                 free (expr);
+                 return WRDE_SYNTAX;
+               }
 
              result[20] = '\0';
              *word = w_addstr (*word, word_length, max_length,
@@ -691,458 +811,924 @@ parse_arith (char **word, size_t *word_length, size_t *max_length,
   return WRDE_SYNTAX;
 }
 
+/* Function called by child process in exec_comm() */
+static inline void
+internal_function __attribute__ ((always_inline))
+exec_comm_child (char *comm, int *fildes, int showerr, int noexec)
+{
+  const char *args[4] = { _PATH_BSHELL, "-c", comm, NULL };
+
+  /* Execute the command, or just check syntax? */
+  if (noexec)
+    args[1] = "-nc";
+
+  /* Redirect output.  */
+  if (__glibc_likely (fildes[1] != STDOUT_FILENO))
+    {
+      __dup2 (fildes[1], STDOUT_FILENO);
+      __close (fildes[1]);
+    }
+  else
+    {
+#ifdef O_CLOEXEC
+      /* Reset the close-on-exec flag (if necessary).  */
+# ifndef __ASSUME_PIPE2
+      if (__have_pipe2 > 0)
+# endif
+       __fcntl (fildes[1], F_SETFD, 0);
+#endif
+    }
+
+  /* Redirect stderr to /dev/null if we have to.  */
+  if (showerr == 0)
+    {
+      struct stat64 st;
+      int fd;
+      __close (STDERR_FILENO);
+      fd = __open (_PATH_DEVNULL, O_WRONLY);
+      if (fd >= 0 && fd != STDERR_FILENO)
+       {
+         __dup2 (fd, STDERR_FILENO);
+         __close (fd);
+       }
+      /* Be paranoid.  Check that we actually opened the /dev/null
+        device.  */
+      if (__builtin_expect (__fxstat64 (_STAT_VER, STDERR_FILENO, &st), 0) != 0
+         || __builtin_expect (S_ISCHR (st.st_mode), 1) == 0
+#if defined DEV_NULL_MAJOR && defined DEV_NULL_MINOR
+         || st.st_rdev != makedev (DEV_NULL_MAJOR, DEV_NULL_MINOR)
+#endif
+         )
+       /* It's not the /dev/null device.  Stop right here.  The
+          problem is: how do we stop?  We use _exit() with an
+          hopefully unusual exit code.  */
+       _exit (90);
+    }
+
+  /* Make sure the subshell doesn't field-split on our behalf. */
+  __unsetenv ("IFS");
+
+  __close (fildes[0]);
+  __execve (_PATH_BSHELL, (char *const *) args, __environ);
+
+  /* Bad.  What now?  */
+  abort ();
+}
+
 /* Function to execute a command and retrieve the results */
 /* pwordexp contains NULL if field-splitting is forbidden */
 static int
 internal_function
 exec_comm (char *comm, char **word, size_t *word_length, size_t *max_length,
-          int flags, wordexp_t *pwordexp)
+          int flags, wordexp_t *pwordexp, const char *ifs,
+          const char *ifs_white)
 {
   int fildes[2];
-  int bufsize = 128;
+#define bufsize 128
   int buflen;
-  int state = 0;
   int i;
-  char *buffer;
+  int status = 0;
+  size_t maxnewlines = 0;
+  char buffer[bufsize];
   pid_t pid;
-  /* 'state' is:
-   *  0 until first non-(whitespace-ifs)
-   *  1 after a non-ifs
-   *  2 after non-(whitespace-ifs)
-   */
+  int noexec = 0;
 
   /* Don't fork() unless necessary */
   if (!comm || !*comm)
     return 0;
 
-  if (pipe (fildes))
-    /* Bad */
-    return WRDE_NOSPACE;
+#ifdef O_CLOEXEC
+# ifndef __ASSUME_PIPE2
+  if (__have_pipe2 >= 0)
+# endif
+    {
+      int r = __pipe2 (fildes, O_CLOEXEC);
+# ifndef __ASSUME_PIPE2
+      if (__have_pipe2 == 0)
+       __have_pipe2 = r != -1 || errno != ENOSYS ? 1 : -1;
+
+      if (__have_pipe2 > 0)
+# endif
+       if (r < 0)
+         /* Bad */
+         return WRDE_NOSPACE;
+    }
+#endif
+#ifndef __ASSUME_PIPE2
+# ifdef O_CLOEXEC
+  if (__have_pipe2 < 0)
+# endif
+    if (__pipe (fildes) < 0)
+      /* Bad */
+      return WRDE_NOSPACE;
+#endif
 
-  if ((pid = fork ()) < 0)
+ again:
+  if ((pid = __fork ()) < 0)
     {
       /* Bad */
+      __close (fildes[0]);
+      __close (fildes[1]);
       return WRDE_NOSPACE;
     }
 
   if (pid == 0)
-    {
-      /* Child */
-      const char *args[4] = { _PATH_BSHELL, "-c", comm, NULL };
-
-      /* Redirect input and output */
-      dup2 (fildes[1], 1);
-
-      /* Close stderr if we have to */
-      if ((flags & WRDE_SHOWERR) == 0)
-       close (2);
-
-      __execve (_PATH_BSHELL, (char *const *) args, __environ);
-
-      /* Bad.  What now?  */
-      abort ();
-    }
+    exec_comm_child (comm, fildes, noexec ? 0 : flags & WRDE_SHOWERR, noexec);
 
   /* Parent */
 
-  close (fildes[1]);
-  buffer = __alloca (bufsize);
+  /* If we are just testing the syntax, only wait.  */
+  if (noexec)
+    return (TEMP_FAILURE_RETRY (__waitpid (pid, &status, 0)) == pid
+           && status != 0) ? WRDE_SYNTAX : 0;
 
-  if (!pwordexp)
-    { /* Quoted - no field splitting */
+  __close (fildes[1]);
+  fildes[1] = -1;
 
+  if (!pwordexp)
+    /* Quoted - no field splitting */
+    {
       while (1)
        {
-         if ((buflen = read (fildes[0], buffer, bufsize)) < 1)
+         if ((buflen = TEMP_FAILURE_RETRY (__read (fildes[0], buffer,
+                                                   bufsize))) < 1)
            {
-             if (__waitpid (pid, NULL, WNOHANG) == 0)
+             /* If read returned 0 then the process has closed its
+                stdout.  Don't use WNOHANG in that case to avoid busy
+                looping until the process eventually exits.  */
+             if (TEMP_FAILURE_RETRY (__waitpid (pid, &status,
+                                                buflen == 0 ? 0 : WNOHANG))
+                 == 0)
                continue;
-             if ((buflen = read (fildes[0], buffer, bufsize)) < 1)
+             if ((buflen = TEMP_FAILURE_RETRY (__read (fildes[0], buffer,
+                                                       bufsize))) < 1)
                break;
            }
 
-         for (i = 0; i < buflen; ++i)
-           {
-             *word = w_addchar (*word, word_length, max_length, buffer[i]);
-             if (*word == NULL)
-               {
-                 close (fildes[0]);
-                 return WRDE_NOSPACE;
-               }
-           }
-       }
+         maxnewlines += buflen;
 
-      close (fildes[0]);
-      return 0;
+         *word = w_addmem (*word, word_length, max_length, buffer, buflen);
+         if (*word == NULL)
+           goto no_space;
+       }
     }
-
-  /* Not quoted - split fields.
-   * NB. This isn't done properly yet.
-   */
-  while (1)
+  else
+    /* Not quoted - split fields */
     {
-      if ((buflen = read (fildes[0], buffer, bufsize)) < 1)
-       {
-         if (__waitpid (pid, NULL, WNOHANG) == 0)
-           continue;
-         if ((read (fildes[0], buffer, bufsize)) < 1)
-           break;
-       }
+      int copying = 0;
+      /* 'copying' is:
+       *  0 when searching for first character in a field not IFS white space
+       *  1 when copying the text of a field
+       *  2 when searching for possible non-whitespace IFS
+       *  3 when searching for non-newline after copying field
+       */
 
-      for (i = 0; i < buflen; ++i)
+      while (1)
        {
-         /* What if these aren't field separators? FIX */
-         if (buffer[i] == ' ' || buffer[i] == '\t' || buffer[i] == '\n')
+         if ((buflen = TEMP_FAILURE_RETRY (__read (fildes[0], buffer,
+                                                   bufsize))) < 1)
            {
-             if (state != 0)
-               state = 2;
-             continue;
+             /* If read returned 0 then the process has closed its
+                stdout.  Don't use WNOHANG in that case to avoid busy
+                looping until the process eventually exits.  */
+             if (TEMP_FAILURE_RETRY (__waitpid (pid, &status,
+                                                buflen == 0 ? 0 : WNOHANG))
+                 == 0)
+               continue;
+             if ((buflen = TEMP_FAILURE_RETRY (__read (fildes[0], buffer,
+                                                       bufsize))) < 1)
+               break;
            }
 
-         if (state == 2)
+         for (i = 0; i < buflen; ++i)
            {
-             /* End of word */
-             if (w_addword (pwordexp, *word) == WRDE_NOSPACE)
+             if (strchr (ifs, buffer[i]) != NULL)
                {
-                 close (fildes[0]);
-                 return WRDE_NOSPACE;
-               }
+                 /* Current character is IFS */
+                 if (strchr (ifs_white, buffer[i]) == NULL)
+                   {
+                     /* Current character is IFS but not whitespace */
+                     if (copying == 2)
+                       {
+                         /*            current character
+                          *                   |
+                          *                   V
+                          * eg: text<space><comma><space>moretext
+                          *
+                          * So, strip whitespace IFS (like at the start)
+                          */
+                         copying = 0;
+                         continue;
+                       }
+
+                     copying = 0;
+                     /* fall through and delimit field.. */
+                   }
+                 else
+                   {
+                     if (buffer[i] == '\n')
+                       {
+                         /* Current character is (IFS) newline */
+
+                         /* If copying a field, this is the end of it,
+                            but maybe all that's left is trailing newlines.
+                            So start searching for a non-newline. */
+                         if (copying == 1)
+                           copying = 3;
+
+                         continue;
+                       }
+                     else
+                       {
+                         /* Current character is IFS white space, but
+                            not a newline */
+
+                         /* If not either copying a field or searching
+                            for non-newline after a field, ignore it */
+                         if (copying != 1 && copying != 3)
+                           continue;
+
+                         /* End of field (search for non-ws IFS afterwards) */
+                         copying = 2;
+                       }
+                   }
 
-             *word = NULL;
-             *word_length = 0;
-           }
+                 /* First IFS white space (non-newline), or IFS non-whitespace.
+                  * Delimit the field.  Nulls are converted by w_addword. */
+                 if (w_addword (pwordexp, *word) == WRDE_NOSPACE)
+                   goto no_space;
 
-         state = 1;
-         *word = w_addchar (*word, word_length, max_length, buffer[i]);
-         if (*word == NULL)
-           {
-             close (fildes[0]);
-             return WRDE_NOSPACE;
+                 *word = w_newword (word_length, max_length);
+
+                 maxnewlines = 0;
+                 /* fall back round the loop.. */
+               }
+             else
+               {
+                 /* Not IFS character */
+
+                 if (copying == 3)
+                   {
+                     /* Nothing but (IFS) newlines since the last field,
+                        so delimit it here before starting new word */
+                     if (w_addword (pwordexp, *word) == WRDE_NOSPACE)
+                       goto no_space;
+
+                     *word = w_newword (word_length, max_length);
+                   }
+
+                 copying = 1;
+
+                 if (buffer[i] == '\n') /* happens if newline not in IFS */
+                   maxnewlines++;
+                 else
+                   maxnewlines = 0;
+
+                 *word = w_addchar (*word, word_length, max_length,
+                                    buffer[i]);
+                 if (*word == NULL)
+                   goto no_space;
+               }
            }
        }
     }
 
-  close (fildes[0]);
+  /* Chop off trailing newlines (required by POSIX.2)  */
+  /* Ensure we don't go back further than the beginning of the
+     substitution (i.e. remove maxnewlines bytes at most) */
+  while (maxnewlines-- != 0 &&
+        *word_length > 0 && (*word)[*word_length - 1] == '\n')
+    {
+      (*word)[--*word_length] = '\0';
+
+      /* If the last word was entirely newlines, turn it into a new word
+       * which can be ignored if there's nothing following it. */
+      if (*word_length == 0)
+       {
+         free (*word);
+         *word = w_newword (word_length, max_length);
+         break;
+       }
+    }
+
+  __close (fildes[0]);
+  fildes[0] = -1;
+
+  /* Check for syntax error (re-execute but with "-n" flag) */
+  if (buflen < 1 && status != 0)
+    {
+      noexec = 1;
+      goto again;
+    }
+
   return 0;
+
+no_space:
+  __kill (pid, SIGKILL);
+  TEMP_FAILURE_RETRY (__waitpid (pid, NULL, 0));
+  __close (fildes[0]);
+  return WRDE_NOSPACE;
 }
 
 static int
+internal_function
 parse_comm (char **word, size_t *word_length, size_t *max_length,
-           const char *words, size_t *offset, int flags, wordexp_t *pwordexp)
+           const char *words, size_t *offset, int flags, wordexp_t *pwordexp,
+           const char *ifs, const char *ifs_white)
 {
   /* We are poised just after "$(" */
   int paren_depth = 1;
-  int error;
-  size_t comm_length = 0;
-  size_t comm_maxlen = 0;
-  char *comm = NULL;
+  int error = 0;
+  int quoted = 0; /* 1 for singly-quoted, 2 for doubly-quoted */
+  size_t comm_length;
+  size_t comm_maxlen;
+  char *comm = w_newword (&comm_length, &comm_maxlen);
 
   for (; words[*offset]; ++(*offset))
     {
       switch (words[*offset])
        {
+       case '\'':
+         if (quoted == 0)
+           quoted = 1;
+         else if (quoted == 1)
+           quoted = 0;
+
+         break;
+
+       case '"':
+         if (quoted == 0)
+           quoted = 2;
+         else if (quoted == 2)
+           quoted = 0;
+
+         break;
+
        case ')':
-         if (--paren_depth == 0)
+         if (!quoted && --paren_depth == 0)
            {
              /* Go -- give script to the shell */
-             error = exec_comm (comm, word, word_length, max_length, flags,
-                                pwordexp);
-             free (comm);
+             if (comm)
+               {
+#ifdef __libc_ptf_call
+                 /* We do not want the exec_comm call to be cut short
+                    by a thread cancellation since cleanup is very
+                    ugly.  Therefore disable cancellation for
+                    now.  */
+                 // XXX Ideally we do want the thread being cancelable.
+                 // XXX If demand is there we'll change it.
+                 int state = PTHREAD_CANCEL_ENABLE;
+                 __libc_ptf_call (pthread_setcancelstate,
+                                  (PTHREAD_CANCEL_DISABLE, &state), 0);
+#endif
+
+                 error = exec_comm (comm, word, word_length, max_length,
+                                    flags, pwordexp, ifs, ifs_white);
+
+#ifdef __libc_ptf_call
+                 __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
+#endif
+
+                 free (comm);
+               }
+
              return error;
            }
 
          /* This is just part of the script */
-         comm = w_addchar (comm, &comm_length, &comm_maxlen, words[*offset]);
-         if (comm == NULL)
-           return WRDE_NOSPACE;
-
          break;
 
        case '(':
-         ++paren_depth;
-       default:
-         comm = w_addchar (comm, &comm_length, &comm_maxlen, words[*offset]);
-         if (comm == NULL)
-           return WRDE_NOSPACE;
-
-         break;
+         if (!quoted)
+           ++paren_depth;
        }
+
+      comm = w_addchar (comm, &comm_length, &comm_maxlen, words[*offset]);
+      if (comm == NULL)
+       return WRDE_NOSPACE;
     }
 
-  /* Premature end */
+  /* Premature end */
   free (comm);
+
   return WRDE_SYNTAX;
 }
 
 static int
 internal_function
 parse_param (char **word, size_t *word_length, size_t *max_length,
-            const char *words, size_t *offset, int flags, wordexp_t *pwordexp)
+            const char *words, size_t *offset, int flags, wordexp_t *pwordexp,
+            const char *ifs, const char *ifs_white, int quoted)
 {
   /* We are poised just after "$" */
-  enum remove_pattern_enum
+  enum action
   {
-    RP_NONE = 0,
-    RP_SHORT_LEFT,
-    RP_LONG_LEFT,
-    RP_SHORT_RIGHT,
-    RP_LONG_RIGHT
+    ACT_NONE,
+    ACT_RP_SHORT_LEFT = '#',
+    ACT_RP_LONG_LEFT = 'L',
+    ACT_RP_SHORT_RIGHT = '%',
+    ACT_RP_LONG_RIGHT = 'R',
+    ACT_NULL_ERROR = '?',
+    ACT_NULL_SUBST = '-',
+    ACT_NONNULL_SUBST = '+',
+    ACT_NULL_ASSIGN = '='
   };
+  size_t env_length;
+  size_t env_maxlen;
+  size_t pat_length;
+  size_t pat_maxlen;
   size_t start = *offset;
-  size_t env_length = 0;
-  size_t env_maxlen = 0;
-  size_t pat_length = 0;
-  size_t pat_maxlen = 0;
-  char *env = NULL;
-  char *pattern = NULL;
-  char *value;
-  char action = '\0';
-  enum remove_pattern_enum remove = RP_NONE;
-  int colon_seen = 0;
+  char *env;
+  char *pattern;
+  char *value = NULL;
+  enum action action = ACT_NONE;
   int depth = 0;
+  int colon_seen = 0;
+  int seen_hash = 0;
+  int free_value = 0;
+  int pattern_is_quoted = 0; /* 1 for singly-quoted, 2 for doubly-quoted */
   int error;
+  int special = 0;
+  char buffer[21];
+  int brace = words[*offset] == '{';
 
-  for (; words[*offset]; ++(*offset))
-    {
-      switch (words[*offset])
-       {
-       case '{':
-         ++depth;
+  env = w_newword (&env_length, &env_maxlen);
+  pattern = w_newword (&pat_length, &pat_maxlen);
 
-         if (action != '\0' || remove != RP_NONE)
-           {
-             pattern = w_addchar (pattern, &pat_length, &pat_maxlen,
-                                  words[*offset]);
-             if (pattern == NULL)
-               goto no_space;
+  if (brace)
+    ++*offset;
 
-             break;
-           }
+  /* First collect the parameter name. */
 
-         if (*offset == start)
-           break;
+  if (words[*offset] == '#')
+    {
+      seen_hash = 1;
+      if (!brace)
+       goto envsubst;
+      ++*offset;
+    }
 
-         /* Otherwise evaluate */
-         /* (and re-parse this character) */
-         --(*offset);
-         goto envsubst;
+  if (isalpha (words[*offset]) || words[*offset] == '_')
+    {
+      /* Normal parameter name. */
+      do
+       {
+         env = w_addchar (env, &env_length, &env_maxlen,
+                          words[*offset]);
+         if (env == NULL)
+           goto no_space;
+       }
+      while (isalnum (words[++*offset]) || words[*offset] == '_');
+    }
+  else if (isdigit (words[*offset]))
+    {
+      /* Numeric parameter name. */
+      special = 1;
+      do
+       {
+         env = w_addchar (env, &env_length, &env_maxlen,
+                          words[*offset]);
+         if (env == NULL)
+           goto no_space;
+         if (!brace)
+           goto envsubst;
+       }
+      while (isdigit(words[++*offset]));
+    }
+  else if (strchr ("*@$", words[*offset]) != NULL)
+    {
+      /* Special parameter. */
+      special = 1;
+      env = w_addchar (env, &env_length, &env_maxlen,
+                      words[*offset]);
+      if (env == NULL)
+       goto no_space;
+      ++*offset;
+    }
+  else
+    {
+      if (brace)
+       goto syntax;
+    }
 
+  if (brace)
+    {
+      /* Check for special action to be applied to the value. */
+      switch (words[*offset])
+       {
        case '}':
-         if (words[start] != '{')
-             --(*offset);
+         /* Evaluate. */
+         goto envsubst;
 
-         if (action != '\0' || remove != RP_NONE)
+       case '#':
+         action = ACT_RP_SHORT_LEFT;
+         if (words[1 + *offset] == '#')
            {
-             if (--depth)
-               {
-                 pattern = w_addchar (pattern, &pat_length, &pat_maxlen,
-                                      words[*offset]);
-                 if (pattern == NULL)
-                   goto no_space;
+             ++*offset;
+             action = ACT_RP_LONG_LEFT;
+           }
+         break;
 
-                 break;
-               }
+       case '%':
+         action = ACT_RP_SHORT_RIGHT;
+         if (words[1 + *offset] == '%')
+           {
+             ++*offset;
+             action = ACT_RP_LONG_RIGHT;
            }
+         break;
 
-         /* Evaluate */
-         goto envsubst;
+       case ':':
+         if (strchr ("-=?+", words[1 + *offset]) == NULL)
+           goto syntax;
 
-       case '#':
-         /* At the start?  (ie. 'string length') */
-         if (*offset == start + 1)
-           /* FIXME: This isn't written yet! */
-           break;
+         colon_seen = 1;
+         action = words[++*offset];
+         break;
 
-         if (words[start] != '{')
-           {
-             /* Evaluate */
-             /* (and re-parse this character) */
-             --(*offset);
-             goto envsubst;
-           }
+       case '-':
+       case '=':
+       case '?':
+       case '+':
+         action = words[*offset];
+         break;
 
-         /* Separating variable name from prefix pattern? */
+       default:
+         goto syntax;
+       }
 
-         if (remove == RP_NONE)
+      /* Now collect the pattern, but don't expand it yet. */
+      ++*offset;
+      for (; words[*offset]; ++(*offset))
+       {
+         switch (words[*offset])
            {
-             remove = RP_SHORT_LEFT;
+           case '{':
+             if (!pattern_is_quoted)
+               ++depth;
              break;
-           }
-         else if (remove == RP_SHORT_LEFT)
-           {
-             remove = RP_LONG_LEFT;
+
+           case '}':
+             if (!pattern_is_quoted)
+               {
+                 if (depth == 0)
+                   goto envsubst;
+                 --depth;
+               }
              break;
-           }
 
-         /* Must be part of prefix/suffix pattern. */
-         pattern = w_addchar (pattern, &pat_length, &pat_maxlen,
-                              words[*offset]);
-         if (pattern == NULL)
-           goto no_space;
+           case '\\':
+             if (pattern_is_quoted)
+               /* Quoted; treat as normal character. */
+               break;
 
-         break;
+             /* Otherwise, it's an escape: next character is literal. */
+             if (words[++*offset] == '\0')
+               goto syntax;
 
-       case '%':
-         if (!*env)
-           goto syntax;
+             pattern = w_addchar (pattern, &pat_length, &pat_maxlen, '\\');
+             if (pattern == NULL)
+               goto no_space;
 
-         /* Separating variable name from suffix pattern? */
-         if (remove == RP_NONE)
-           {
-             remove = RP_SHORT_RIGHT;
              break;
-           }
-         else if (remove == RP_SHORT_RIGHT)
-           {
-             remove = RP_LONG_RIGHT;
+
+           case '\'':
+             if (pattern_is_quoted == 0)
+               pattern_is_quoted = 1;
+             else if (pattern_is_quoted == 1)
+               pattern_is_quoted = 0;
+
+             break;
+
+           case '"':
+             if (pattern_is_quoted == 0)
+               pattern_is_quoted = 2;
+             else if (pattern_is_quoted == 2)
+               pattern_is_quoted = 0;
+
              break;
            }
 
-         /* Must be part of prefix/suffix pattern. */
          pattern = w_addchar (pattern, &pat_length, &pat_maxlen,
                               words[*offset]);
          if (pattern == NULL)
            goto no_space;
+       }
+    }
 
-         break;
-
-       case ':':
-         if (!*env)
-           goto syntax;
+  /* End of input string -- remember to reparse the character that we
+   * stopped at.  */
+  --(*offset);
 
-         if (action != '\0' || remove != RP_NONE)
-           {
-             pattern = w_addchar (pattern, &pat_length, &pat_maxlen,
-                                  words[*offset]);
-             if (pattern == NULL)
-               goto no_space;
+envsubst:
+  if (words[start] == '{' && words[*offset] != '}')
+    goto syntax;
 
-             break;
-           }
+  if (env == NULL)
+    {
+      if (seen_hash)
+       {
+         /* $# expands to the number of positional parameters */
+         buffer[20] = '\0';
+         value = _itoa_word (__libc_argc - 1, &buffer[20], 10, 0);
+         seen_hash = 0;
+       }
+      else
+       {
+         /* Just $ on its own */
+         *offset = start - 1;
+         *word = w_addchar (*word, word_length, max_length, '$');
+         return *word ? 0 : WRDE_NOSPACE;
+       }
+    }
+  /* Is it a numeric parameter? */
+  else if (isdigit (env[0]))
+    {
+      int n = atoi (env);
 
-         if ((words[1 + *offset] == '-') || (words[1 + *offset] == '=') ||
-             (words[1 + *offset] == '?') || (words[1 + *offset] == '+'))
+      if (n >= __libc_argc)
+       /* Substitute NULL. */
+       value = NULL;
+      else
+       /* Replace with appropriate positional parameter. */
+       value = __libc_argv[n];
+    }
+  /* Is it a special parameter? */
+  else if (special)
+    {
+      /* Is it `$$'? */
+      if (*env == '$')
+       {
+         buffer[20] = '\0';
+         value = _itoa_word (__getpid (), &buffer[20], 10, 0);
+       }
+      /* Is it `${#*}' or `${#@}'? */
+      else if ((*env == '*' || *env == '@') && seen_hash)
+       {
+         buffer[20] = '\0';
+         value = _itoa_word (__libc_argc > 0 ? __libc_argc - 1 : 0,
+                             &buffer[20], 10, 0);
+         *word = w_addstr (*word, word_length, max_length, value);
+         free (env);
+         free (pattern);
+         return *word ? 0 : WRDE_NOSPACE;
+       }
+      /* Is it `$*' or `$@' (unquoted) ? */
+      else if (*env == '*' || (*env == '@' && !quoted))
+       {
+         size_t plist_len = 0;
+         int p;
+         char *end;
+
+         /* Build up value parameter by parameter (copy them) */
+         for (p = 1; __libc_argv[p]; ++p)
+           plist_len += strlen (__libc_argv[p]) + 1; /* for space */
+         value = malloc (plist_len);
+         if (value == NULL)
+           goto no_space;
+         end = value;
+         *end = 0;
+         for (p = 1; __libc_argv[p]; ++p)
            {
-             colon_seen = 1;
-             break;
+             if (p > 1)
+               *end++ = ' ';
+             end = __stpcpy (end, __libc_argv[p]);
            }
 
-         goto syntax;
-
-       case '-':
-       case '=':
-       case '?':
-       case '+':
-         if (!*env)
-           goto syntax;
+         free_value = 1;
+       }
+      else
+       {
+         /* Must be a quoted `$@' */
+         assert (*env == '@' && quoted);
 
-         if (action != '\0' || remove != RP_NONE)
+         /* Each parameter is a separate word ("$@") */
+         if (__libc_argc == 2)
+           value = __libc_argv[1];
+         else if (__libc_argc > 2)
            {
-             pattern = w_addchar (pattern, &pat_length, &pat_maxlen,
-                                  words[*offset]);
-             if (pattern == NULL)
-               goto no_space;
+             int p;
 
-             break;
-           }
+             /* Append first parameter to current word. */
+             value = w_addstr (*word, word_length, max_length,
+                               __libc_argv[1]);
+             if (value == NULL || w_addword (pwordexp, value))
+               goto no_space;
 
-         action = words[*offset];
-         break;
+             for (p = 2; __libc_argv[p + 1]; p++)
+               {
+                 char *newword = __strdup (__libc_argv[p]);
+                 if (newword == NULL || w_addword (pwordexp, newword))
+                   goto no_space;
+               }
 
-       case '\\':
-         if (action != '\0' || remove != RP_NONE)
-           {
-             /* Um. Is this right? */
-             error = parse_qtd_backslash (word, word_length, max_length,
-                                          words, offset);
-             if (error == 0)
-               break;
+             /* Start a new word with the last parameter. */
+             *word = w_newword (word_length, max_length);
+             value = __libc_argv[p];
            }
          else
            {
-             error = WRDE_SYNTAX;
+             free (env);
+             free (pattern);
+             return 0;
            }
+       }
+    }
+  else
+    value = getenv (env);
 
-         if (env)
-           free (env);
+  if (value == NULL && (flags & WRDE_UNDEF))
+    {
+      /* Variable not defined. */
+      error = WRDE_BADVAL;
+      goto do_error;
+    }
 
-         if (pattern != NULL)
-           free (pattern);
+  if (action != ACT_NONE)
+    {
+      int expand_pattern = 0;
 
-         return error;
+      /* First, find out if we need to expand pattern (i.e. if we will
+       * use it). */
+      switch (action)
+       {
+       case ACT_RP_SHORT_LEFT:
+       case ACT_RP_LONG_LEFT:
+       case ACT_RP_SHORT_RIGHT:
+       case ACT_RP_LONG_RIGHT:
+         /* Always expand for these. */
+         expand_pattern = 1;
+         break;
+
+       case ACT_NULL_ERROR:
+       case ACT_NULL_SUBST:
+       case ACT_NULL_ASSIGN:
+         if (!value || (!*value && colon_seen))
+           /* If param is unset, or set but null and a colon has been seen,
+              the expansion of the pattern will be needed. */
+           expand_pattern = 1;
+
+         break;
+
+       case ACT_NONNULL_SUBST:
+         /* Expansion of word will be needed if parameter is set and not null,
+            or set null but no colon has been seen. */
+         if (value && (*value || !colon_seen))
+           expand_pattern = 1;
+
+         break;
 
        default:
-         if (action != '\0' || remove != RP_NONE)
+         assert (! "Unrecognised action!");
+       }
+
+      if (expand_pattern)
+       {
+         /* We need to perform tilde expansion, parameter expansion,
+            command substitution, and arithmetic expansion.  We also
+            have to be a bit careful with wildcard characters, as
+            pattern might be given to fnmatch soon.  To do this, we
+            convert quotes to escapes. */
+
+         char *expanded;
+         size_t exp_len;
+         size_t exp_maxl;
+         char *p;
+         int quoted = 0; /* 1: single quotes; 2: double */
+
+         expanded = w_newword (&exp_len, &exp_maxl);
+         for (p = pattern; p && *p; p++)
            {
-             pattern = w_addchar (pattern, &pat_length, &pat_maxlen,
-                                  words[*offset]);
-             if (pattern == NULL)
-               goto no_space;
+             size_t offset;
 
-             break;
-           }
+             switch (*p)
+               {
+               case '"':
+                 if (quoted == 2)
+                   quoted = 0;
+                 else if (quoted == 0)
+                   quoted = 2;
+                 else break;
+
+                 continue;
+
+               case '\'':
+                 if (quoted == 1)
+                   quoted = 0;
+                 else if (quoted == 0)
+                   quoted = 1;
+                 else break;
+
+                 continue;
+
+               case '*':
+               case '?':
+                 if (quoted)
+                   {
+                     /* Convert quoted wildchar to escaped wildchar. */
+                     expanded = w_addchar (expanded, &exp_len,
+                                           &exp_maxl, '\\');
 
-         if ((words[start] == '{') || isalpha (words[*offset]))
-           {
-             env = w_addchar (env, &env_length, &env_maxlen, words[*offset]);
-             if (env == NULL)
-               goto no_space;
+                     if (expanded == NULL)
+                       goto no_space;
+                   }
+                 break;
 
-             break;
-           }
+               case '$':
+                 offset = 0;
+                 error = parse_dollars (&expanded, &exp_len, &exp_maxl, p,
+                                        &offset, flags, NULL, NULL, NULL, 1);
+                 if (error)
+                   {
+                     if (free_value)
+                       free (value);
 
-         --(*offset);
-         goto envsubst;
-       }
-    }
+                     free (expanded);
 
-  /* End of input string */
-  --(*offset);
+                     goto do_error;
+                   }
 
-envsubst:
-  if (words[start] == '{' && words[*offset] != '}')
-    goto syntax;
+                 p += offset;
+                 continue;
 
-  if (!env || !*env)
-    {
-      *offset = start - 1;
-      *word = w_addchar (*word, word_length, max_length, '$');
-      free (env);
-      free (pattern);
-      return *word ? 0 : WRDE_NOSPACE;
-    }
+               case '~':
+                 if (quoted || exp_len)
+                   break;
 
-  value = getenv (env);
+                 offset = 0;
+                 error = parse_tilde (&expanded, &exp_len, &exp_maxl, p,
+                                      &offset, 0);
+                 if (error)
+                   {
+                     if (free_value)
+                       free (value);
+
+                     free (expanded);
+
+                     goto do_error;
+                   }
+
+                 p += offset;
+                 continue;
+
+               case '\\':
+                 expanded = w_addchar (expanded, &exp_len, &exp_maxl, '\\');
+                 ++p;
+                 assert (*p); /* checked when extracted initially */
+                 if (expanded == NULL)
+                   goto no_space;
+               }
+
+             expanded = w_addchar (expanded, &exp_len, &exp_maxl, *p);
+
+             if (expanded == NULL)
+               goto no_space;
+           }
+
+         free (pattern);
+
+         pattern = expanded;
+       }
 
-  if (action != '\0' || remove != RP_NONE)
-    {
       switch (action)
        {
-       case 0:
+       case ACT_RP_SHORT_LEFT:
+       case ACT_RP_LONG_LEFT:
+       case ACT_RP_SHORT_RIGHT:
+       case ACT_RP_LONG_RIGHT:
          {
            char *p;
            char c;
            char *end;
 
-           if (!pattern || !*pattern)
+           if (value == NULL || pattern == NULL || *pattern == '\0')
              break;
 
            end = value + strlen (value);
 
-           if (value == NULL)
-             break;
-
-           switch (remove)
+           switch (action)
              {
-             case RP_SHORT_LEFT:
-               for (p = value; p <= end; p++)
+             case ACT_RP_SHORT_LEFT:
+               for (p = value; p <= end; ++p)
                  {
                    c = *p;
                    *p = '\0';
                    if (fnmatch (pattern, value, 0) != FNM_NOMATCH)
                      {
                        *p = c;
-                       value = p;
+                       if (free_value)
+                         {
+                           char *newval = __strdup (p);
+                           if (newval == NULL)
+                             {
+                               free (value);
+                               goto no_space;
+                             }
+                           free (value);
+                           value = newval;
+                         }
+                       else
+                         value = p;
                        break;
                      }
                    *p = c;
@@ -1150,15 +1736,27 @@ envsubst:
 
                break;
 
-             case RP_LONG_LEFT:
-               for (p = end; p >= value; p--)
+             case ACT_RP_LONG_LEFT:
+               for (p = end; p >= value; --p)
                  {
                    c = *p;
                    *p = '\0';
                    if (fnmatch (pattern, value, 0) != FNM_NOMATCH)
                      {
                        *p = c;
-                       value = p;
+                       if (free_value)
+                         {
+                           char *newval = __strdup (p);
+                           if (newval == NULL)
+                             {
+                               free (value);
+                               goto no_space;
+                             }
+                           free (value);
+                           value = newval;
+                         }
+                       else
+                         value = p;
                        break;
                      }
                    *p = c;
@@ -1166,24 +1764,52 @@ envsubst:
 
                break;
 
-             case RP_SHORT_RIGHT:
-               for (p = end; p >= value; p--)
+             case ACT_RP_SHORT_RIGHT:
+               for (p = end; p >= value; --p)
                  {
                    if (fnmatch (pattern, p, 0) != FNM_NOMATCH)
                      {
-                       *p = '\0';
+                       char *newval;
+                       newval = malloc (p - value + 1);
+
+                       if (newval == NULL)
+                         {
+                           if (free_value)
+                             free (value);
+                           goto no_space;
+                         }
+
+                       *(char *) __mempcpy (newval, value, p - value) = '\0';
+                       if (free_value)
+                         free (value);
+                       value = newval;
+                       free_value = 1;
                        break;
                      }
                  }
 
                break;
 
-             case RP_LONG_RIGHT:
-               for (p = value; p <= end; p++)
+             case ACT_RP_LONG_RIGHT:
+               for (p = value; p <= end; ++p)
                  {
                    if (fnmatch (pattern, p, 0) != FNM_NOMATCH)
                      {
-                       *p = '\0';
+                       char *newval;
+                       newval = malloc (p - value + 1);
+
+                       if (newval == NULL)
+                         {
+                           if (free_value)
+                             free (value);
+                           goto no_space;
+                         }
+
+                       *(char *) __mempcpy (newval, value, p - value) = '\0';
+                       if (free_value)
+                         free (value);
+                       value = newval;
+                       free_value = 1;
                        break;
                      }
                  }
@@ -1191,108 +1817,236 @@ envsubst:
                break;
 
              default:
-               assert (! "Unexpected `remove' value\n");
+               break;
              }
 
            break;
          }
 
-       case '?':
+       case ACT_NULL_ERROR:
          if (value && *value)
+           /* Substitute parameter */
            break;
 
+         error = 0;
          if (!colon_seen && value)
+           /* Substitute NULL */
+           ;
+         else
            {
-             /* Substitute NULL */
-             free (env);
-             free (pattern);
-             return 0;
+             const char *str = pattern;
+
+             if (str[0] == '\0')
+               str = _("parameter null or not set");
+
+             __fxprintf (NULL, "%s: %s\n", env, str);
            }
 
-         /* Error - exit */
-         fprintf (stderr, "%s: ", env);
+         if (free_value)
+           free (value);
+         goto do_error;
+
+       case ACT_NULL_SUBST:
+         if (value && *value)
+           /* Substitute parameter */
+           break;
+
+         if (free_value)
+           free (value);
+
+         if (!colon_seen && value)
+           /* Substitute NULL */
+           goto success;
+
+         value = pattern ? __strdup (pattern) : pattern;
+         free_value = 1;
+
+         if (pattern && !value)
+           goto no_space;
+
+         break;
 
-         if (*pattern)
+       case ACT_NONNULL_SUBST:
+         if (value && (*value || !colon_seen))
            {
-             /* Expand 'pattern' and write it to stderr */
-             wordexp_t we;
+             if (free_value)
+               free (value);
 
-             error = wordexp (pattern, &we, flags);
+             value = pattern ? __strdup (pattern) : pattern;
+             free_value = 1;
 
-             if (error == 0)
-               {
-                 int i;
+             if (pattern && !value)
+               goto no_space;
 
-                 for (i = 0; i < we.we_wordc; ++i)
-                   {
-                     fprintf (stderr, "%s%s", i ? " " : "", we.we_wordv[i]);
-                   }
+             break;
+           }
 
-                 fprintf (stderr, "\n");
-                 error = WRDE_BADVAL;
-               }
+         /* Substitute NULL */
+         if (free_value)
+           free (value);
+         goto success;
 
-             wordfree (&we);
-             free (env);
-             free (pattern);
-             return error;
+       case ACT_NULL_ASSIGN:
+         if (value && *value)
+           /* Substitute parameter */
+           break;
+
+         if (!colon_seen && value)
+           {
+             /* Substitute NULL */
+             if (free_value)
+               free (value);
+             goto success;
            }
 
-         fprintf (stderr, "parameter null or not set\n");
-         free (env);
-         free (pattern);
-         return WRDE_BADVAL;
+         if (free_value)
+           free (value);
+
+         value = pattern ? __strdup (pattern) : pattern;
+         free_value = 1;
+
+         if (pattern && !value)
+           goto no_space;
+
+         __setenv (env, value, 1);
+         break;
 
        default:
-         printf ("warning: parameter substitution does not yet support \"%s%c\"\n", colon_seen?":":"", action);
+         assert (! "Unrecognised action!");
        }
     }
 
   free (env);
+  env = NULL;
   free (pattern);
+  pattern = NULL;
 
-  if (value == NULL)
+  if (seen_hash)
     {
-      /* Variable not defined */
-      if (flags & WRDE_UNDEF)
-       return WRDE_SYNTAX;
+      char param_length[21];
+      param_length[20] = '\0';
+      *word = w_addstr (*word, word_length, max_length,
+                       _itoa_word (value ? strlen (value) : 0,
+                                   &param_length[20], 10, 0));
+      if (free_value)
+       {
+         assert (value != NULL);
+         free (value);
+       }
 
-      return 0;
+      return *word ? 0 : WRDE_NOSPACE;
     }
 
-  if (pwordexp == NULL)
-    /* Quoted - no field split */
-    *word = w_addstr (*word, word_length, max_length, value);
+  if (value == NULL)
+    return 0;
+
+  if (quoted || !pwordexp)
+    {
+      /* Quoted - no field split */
+      *word = w_addstr (*word, word_length, max_length, value);
+      if (free_value)
+       free (value);
+
+      return *word ? 0 : WRDE_NOSPACE;
+    }
   else
-    /* Should field-split here - FIX */
-    *word = w_addstr (*word, word_length, max_length, value);
+    {
+      /* Need to field-split */
+      char *value_copy = __strdup (value); /* Don't modify value */
+      char *field_begin = value_copy;
+      int seen_nonws_ifs = 0;
 
-  return *word ? 0 : WRDE_NOSPACE;
+      if (free_value)
+       free (value);
 
-no_space:
-  if (env)
-    free (env);
+      if (value_copy == NULL)
+       goto no_space;
 
-  if (pattern)
-    free (pattern);
+      do
+       {
+         char *field_end = field_begin;
+         char *next_field;
 
-  return WRDE_NOSPACE;
+         /* If this isn't the first field, start a new word */
+         if (field_begin != value_copy)
+           {
+             if (w_addword (pwordexp, *word) == WRDE_NOSPACE)
+               {
+                 free (value_copy);
+                 goto no_space;
+               }
+
+             *word = w_newword (word_length, max_length);
+           }
+
+         /* Skip IFS whitespace before the field */
+         field_begin += strspn (field_begin, ifs_white);
+
+         if (!seen_nonws_ifs && *field_begin == 0)
+           /* Nothing but whitespace */
+           break;
+
+         /* Search for the end of the field */
+         field_end = field_begin + strcspn (field_begin, ifs);
+
+         /* Set up pointer to the character after end of field and
+            skip whitespace IFS after it. */
+         next_field = field_end + strspn (field_end, ifs_white);
+
+         /* Skip at most one non-whitespace IFS character after the field */
+         seen_nonws_ifs = 0;
+         if (*next_field && strchr (ifs, *next_field))
+           {
+             seen_nonws_ifs = 1;
+             next_field++;
+           }
+
+         /* Null-terminate it */
+         *field_end = 0;
+
+         /* Tag a copy onto the current word */
+         *word = w_addstr (*word, word_length, max_length, field_begin);
+
+         if (*word == NULL && *field_begin != '\0')
+           {
+             free (value_copy);
+             goto no_space;
+           }
+
+         field_begin = next_field;
+       }
+      while (seen_nonws_ifs || *field_begin);
+
+      free (value_copy);
+    }
+
+  return 0;
+
+success:
+  error = 0;
+  goto do_error;
+
+no_space:
+  error = WRDE_NOSPACE;
+  goto do_error;
 
 syntax:
-  if (env)
-    free (env);
+  error = WRDE_SYNTAX;
 
-  if (pattern)
-    free (pattern);
+do_error:
+  free (env);
 
-  return WRDE_SYNTAX;
+  free (pattern);
+
+  return error;
 }
 
 static int
 internal_function
 parse_dollars (char **word, size_t *word_length, size_t *max_length,
               const char *words, size_t *offset, int flags,
-              wordexp_t *pwordexp)
+              wordexp_t *pwordexp, const char *ifs, const char *ifs_white,
+              int quoted)
 {
   /* We are poised _at_ "$" */
   switch (words[1 + *offset])
@@ -1306,10 +2060,26 @@ parse_dollars (char **word, size_t *word_length, size_t *max_length,
     case '(':
       if (words[2 + *offset] == '(')
        {
-         (*offset) += 3;
-         /* Call parse_arith -- 0 is for "no brackets" */
-         return parse_arith (word, word_length, max_length, words, offset,
-                             flags, 0);
+         /* Differentiate between $((1+3)) and $((echo);(ls)) */
+         int i = 3 + *offset;
+         int depth = 0;
+         while (words[i] && !(depth == 0 && words[i] == ')'))
+           {
+             if (words[i] == '(')
+               ++depth;
+             else if (words[i] == ')')
+               --depth;
+
+             ++i;
+           }
+
+         if (words[i] == ')' && words[i + 1] == ')')
+           {
+             (*offset) += 3;
+             /* Call parse_arith -- 0 is for "no brackets" */
+             return parse_arith (word, word_length, max_length, words, offset,
+                                 flags, 0);
+           }
        }
 
       if (flags & WRDE_NOCMD)
@@ -1317,7 +2087,7 @@ parse_dollars (char **word, size_t *word_length, size_t *max_length,
 
       (*offset) += 2;
       return parse_comm (word, word_length, max_length, words, offset, flags,
-                        pwordexp);
+                        quoted? NULL : pwordexp, ifs, ifs_white);
 
     case '[':
       (*offset) += 2;
@@ -1329,21 +2099,22 @@ parse_dollars (char **word, size_t *word_length, size_t *max_length,
     default:
       ++(*offset);     /* parse_param needs to know if "{" is there */
       return parse_param (word, word_length, max_length, words, offset, flags,
-                         pwordexp);
+                          pwordexp, ifs, ifs_white, quoted);
     }
 }
 
 static int
+internal_function
 parse_backtick (char **word, size_t *word_length, size_t *max_length,
                const char *words, size_t *offset, int flags,
-               wordexp_t *pwordexp)
+               wordexp_t *pwordexp, const char *ifs, const char *ifs_white)
 {
   /* We are poised just after "`" */
   int error;
-  size_t comm_length = 0;
-  size_t comm_maxlen = 0;
-  char *comm = NULL;
   int squoting = 0;
+  size_t comm_length;
+  size_t comm_maxlen;
+  char *comm = w_newword (&comm_length, &comm_maxlen);
 
   for (; words[*offset]; ++(*offset))
     {
@@ -1352,7 +2123,7 @@ parse_backtick (char **word, size_t *word_length, size_t *max_length,
        case '`':
          /* Go -- give the script to the shell */
          error = exec_comm (comm, word, word_length, max_length, flags,
-                            pwordexp);
+                            pwordexp, ifs, ifs_white);
          free (comm);
          return error;
 
@@ -1400,7 +2171,8 @@ parse_backtick (char **word, size_t *word_length, size_t *max_length,
 static int
 internal_function
 parse_dquote (char **word, size_t *word_length, size_t *max_length,
-             const char *words, size_t *offset, int flags)
+             const char *words, size_t *offset, int flags,
+             wordexp_t *pwordexp, const char * ifs, const char * ifs_white)
 {
   /* We are poised just after a double-quote */
   int error;
@@ -1414,9 +2186,9 @@ parse_dquote (char **word, size_t *word_length, size_t *max_length,
 
        case '$':
          error = parse_dollars (word, word_length, max_length, words, offset,
-                                flags, NULL);
-         /* The NULL here is to tell parse_dollars not to
-          * split the fields.
+                                flags, pwordexp, ifs, ifs_white, 1);
+         /* The ``1'' here is to tell parse_dollars not to
+          * split the fields.  It may need to, however ("$@").
           */
          if (error)
            return error;
@@ -1429,8 +2201,8 @@ parse_dquote (char **word, size_t *word_length, size_t *max_length,
 
          ++(*offset);
          error = parse_backtick (word, word_length, max_length, words,
-                                 offset, flags, NULL);
-         /* The NULL here is to tell parse_backtick not to
+                                 offset, flags, NULL, NULL, NULL);
+         /* The first NULL here is to tell parse_backtick not to
           * split the fields.
           */
          if (error)
@@ -1478,6 +2250,7 @@ wordfree (wordexp_t *pwordexp)
       pwordexp->we_wordv = NULL;
     }
 }
+libc_hidden_def (wordfree)
 
 /*
  * wordexp()
@@ -1486,47 +2259,55 @@ wordfree (wordexp_t *pwordexp)
 int
 wordexp (const char *words, wordexp_t *pwordexp, int flags)
 {
-  size_t wordv_offset;
   size_t words_offset;
-  size_t word_length = 0;
-  size_t max_length = 0;
-  char *word = NULL;
+  size_t word_length;
+  size_t max_length;
+  char *word = w_newword (&word_length, &max_length);
   int error;
   char *ifs;
   char ifs_white[4];
-  char **old_wordv = pwordexp->we_wordv;
-  size_t old_wordc = pwordexp->we_wordc;
+  wordexp_t old_word = *pwordexp;
 
   if (flags & WRDE_REUSE)
-    /* Minimal implementation of WRDE_REUSE for now */
-    wordfree (pwordexp);
-
-  if (flags & WRDE_DOOFFS)
-    {
-      pwordexp->we_wordv = calloc (1 + pwordexp->we_offs, sizeof (char *));
-      if (pwordexp->we_wordv == NULL)
-       return WRDE_NOSPACE;
-    }
-  else
     {
-      pwordexp->we_wordv = calloc (1, sizeof (char *));
-      if (pwordexp->we_wordv == NULL)
-       return WRDE_NOSPACE;
-
-      pwordexp->we_offs = 0;
+      /* Minimal implementation of WRDE_REUSE for now */
+      wordfree (pwordexp);
+      old_word.we_wordv = NULL;
     }
 
   if ((flags & WRDE_APPEND) == 0)
-    pwordexp->we_wordc = 0;
+    {
+      pwordexp->we_wordc = 0;
 
-  wordv_offset = pwordexp->we_offs + pwordexp->we_wordc;
+      if (flags & WRDE_DOOFFS)
+       {
+         pwordexp->we_wordv = calloc (1 + pwordexp->we_offs, sizeof (char *));
+         if (pwordexp->we_wordv == NULL)
+           {
+             error = WRDE_NOSPACE;
+             goto do_error;
+           }
+       }
+      else
+       {
+         pwordexp->we_wordv = calloc (1, sizeof (char *));
+         if (pwordexp->we_wordv == NULL)
+           {
+             error = WRDE_NOSPACE;
+             goto do_error;
+           }
+
+         pwordexp->we_offs = 0;
+       }
+    }
 
   /* Find out what the field separators are.
    * There are two types: whitespace and non-whitespace.
    */
   ifs = getenv ("IFS");
 
-  if (!ifs)
+  if (ifs == NULL)
+    /* IFS unset - use <space><tab><newline>. */
     ifs = strcpy (ifs_white, " \t\n");
   else
     {
@@ -1534,39 +2315,28 @@ wordexp (const char *words, wordexp_t *pwordexp, int flags)
       char *whch = ifs_white;
 
       while (*ifsch != '\0')
-       if ((*ifsch == ' ') || (*ifsch == '\t') || (*ifsch == '\n'))
-         {
-           /* White space IFS.  See first whether it is already in our
-              collection.  */
-           char *runp = ifs_white;
+       {
+         if (*ifsch == ' ' || *ifsch == '\t' || *ifsch == '\n')
+           {
+             /* Whitespace IFS.  See first whether it is already in our
+                collection.  */
+             char *runp = ifs_white;
 
-           while (runp < whch && *runp != '\0' && *runp != *ifsch)
-             ++runp;
+             while (runp < whch && *runp != *ifsch)
+               ++runp;
 
-           if (runp == whch)
-             *whch++ = *ifsch;
-         }
+             if (runp == whch)
+               *whch++ = *ifsch;
+           }
+
+         ++ifsch;
+       }
       *whch = '\0';
     }
 
   for (words_offset = 0 ; words[words_offset] ; ++words_offset)
     switch (words[words_offset])
       {
-      case '\n':
-      case '|':
-      case '&':
-      case ';':
-      case '<':
-      case '>':
-      case '(':
-      case ')':
-      case '}':
-       /* Fail */
-       wordfree (pwordexp);
-       pwordexp->we_wordc = 0;
-       pwordexp->we_wordv = old_wordv;
-       return WRDE_BADCHAR;
-
       case '\\':
        error = parse_backslash (&word, &word_length, &max_length, words,
                                 &words_offset);
@@ -1578,7 +2348,8 @@ wordexp (const char *words, wordexp_t *pwordexp, int flags)
 
       case '$':
        error = parse_dollars (&word, &word_length, &max_length, words,
-                              &words_offset, flags, pwordexp);
+                              &words_offset, flags, pwordexp, ifs, ifs_white,
+                              0);
 
        if (error)
          goto do_error;
@@ -1587,11 +2358,15 @@ wordexp (const char *words, wordexp_t *pwordexp, int flags)
 
       case '`':
        if (flags & WRDE_NOCMD)
-         return WRDE_CMDSUB;
+         {
+           error = WRDE_CMDSUB;
+           goto do_error;
+         }
 
        ++words_offset;
        error = parse_backtick (&word, &word_length, &max_length, words,
-                               &words_offset, flags, pwordexp);
+                               &words_offset, flags, pwordexp, ifs,
+                               ifs_white);
 
        if (error)
          goto do_error;
@@ -1601,11 +2376,19 @@ wordexp (const char *words, wordexp_t *pwordexp, int flags)
       case '"':
        ++words_offset;
        error = parse_dquote (&word, &word_length, &max_length, words,
-                             &words_offset, flags);
+                             &words_offset, flags, pwordexp, ifs, ifs_white);
 
        if (error)
          goto do_error;
 
+       if (!word_length)
+         {
+           error = w_addword (pwordexp, NULL);
+
+           if (error)
+             return error;
+         }
+
        break;
 
       case '\'':
@@ -1616,6 +2399,14 @@ wordexp (const char *words, wordexp_t *pwordexp, int flags)
        if (error)
          goto do_error;
 
+       if (!word_length)
+         {
+           error = w_addword (pwordexp, NULL);
+
+           if (error)
+             return error;
+         }
+
        break;
 
       case '~':
@@ -1628,9 +2419,10 @@ wordexp (const char *words, wordexp_t *pwordexp, int flags)
        break;
 
       case '*':
-      case '{':
+      case '[':
+      case '?':
        error = parse_glob (&word, &word_length, &max_length, words,
-                           &words_offset, flags, pwordexp, ifs);
+                           &words_offset, flags, pwordexp, ifs, ifs_white);
 
        if (error)
          goto do_error;
@@ -1638,64 +2430,46 @@ wordexp (const char *words, wordexp_t *pwordexp, int flags)
        break;
 
       default:
-       /* Is it a field separator? */
-       if (strchr (ifs, words[words_offset]) == NULL)
+       /* Is it a word separator? */
+       if (strchr (" \t", words[words_offset]) == NULL)
          {
-           /* "Ordinary" character -- add it to word */
+           char ch = words[words_offset];
 
-           word = w_addchar (word, &word_length, &max_length,
-                             words[words_offset]);
-           if (word == NULL)
+           /* Not a word separator -- but is it a valid word char? */
+           if (strchr ("\n|&;<>(){}", ch))
              {
-               error = WRDE_NOSPACE;
+               /* Fail */
+               error = WRDE_BADCHAR;
                goto do_error;
              }
 
-           break;
-         }
-
-       /* Field separator */
-       if (strchr (ifs_white, words[words_offset]))
-         {
-           /* It's a whitespace IFS char.  Ignore it at the beginning
-              of a line and ignore multiple instances.  */
-           if (!word || !*word)
-             break;
-
-           if (w_addword (pwordexp, word) == WRDE_NOSPACE)
+           /* "Ordinary" character -- add it to word */
+           word = w_addchar (word, &word_length, &max_length,
+                             ch);
+           if (word == NULL)
              {
                error = WRDE_NOSPACE;
                goto do_error;
              }
 
-           word = NULL;
-           word_length = 0;
            break;
          }
 
-       /* It's a non-whitespace IFS char */
-
-       /* Multiple non-whitespace IFS chars are treated as one;
-        * IS THIS CORRECT?
-        */
+       /* If a word has been delimited, add it to the list. */
        if (word != NULL)
          {
-           if (w_addword (pwordexp, word) == WRDE_NOSPACE)
-             {
-               error = WRDE_NOSPACE;
-               goto do_error;
-             }
+           error = w_addword (pwordexp, word);
+           if (error)
+             goto do_error;
          }
 
-       word = NULL;
-       word_length = 0;
-       max_length = 0;
+       word = w_newword (&word_length, &max_length);
       }
 
   /* End of string */
 
-  /* There was a field separator at the end */
-  if (word == NULL)
+  /* There was a word separator at the end */
+  if (word == NULL) /* i.e. w_newword */
     return 0;
 
   /* There was no field separator at the end */
@@ -1703,13 +2477,18 @@ wordexp (const char *words, wordexp_t *pwordexp, int flags)
 
 do_error:
   /* Error:
-       free memory used, set we_wordc and wd_wordv back to what they were.
+   *   free memory used (unless error is WRDE_NOSPACE), and
+   *   set pwordexp members back to what they were.
    */
-  if (word != NULL)
-    free (word);
 
-  wordfree (pwordexp);
-  pwordexp->we_wordv = old_wordv;
-  pwordexp->we_wordc = old_wordc;
+  free (word);
+
+  if (error == WRDE_NOSPACE)
+    return WRDE_NOSPACE;
+
+  if ((flags & WRDE_APPEND) == 0)
+    wordfree (pwordexp);
+
+  *pwordexp = old_word;
   return error;
 }