sh: Fix unquoted $@/$* if IFS=''.
authorPeter Avalos <pavalos@dragonflybsd.org>
Sun, 21 Aug 2011 19:39:23 +0000 (12:39 -0700)
committerPeter Avalos <pavalos@dragonflybsd.org>
Sun, 21 Aug 2011 20:49:14 +0000 (13:49 -0700)
If IFS is null, unquoted $@/$* should still expand to separate words.
This differs from quoted $@ (which does not depend on IFS) in that
pathname generation is performed and empty words are removed.

Obtained-from:   FreeBSD 222361

bin/sh/expand.c
tools/regression/bin/sh/expansion/ifs4.0 [new file with mode: 0644]

index 6f355b8..fae5a3d 100644 (file)
@@ -36,7 +36,7 @@
  * SUCH DAMAGE.
  *
  * @(#)expand.c        8.5 (Berkeley) 5/15/95
- * $FreeBSD: src/bin/sh/expand.c,v 1.85 2011/05/08 11:32:20 jilles Exp $
+ * $FreeBSD: src/bin/sh/expand.c,v 1.86 2011/05/27 15:56:13 jilles Exp $
  */
 
 #include <sys/types.h>
@@ -760,7 +760,8 @@ again: /* jump here after setting a variable with ${var=text} */
                        break;
 record:
                recordregion(startloc, expdest - stackblock(),
-                            varflags & VSQUOTE);
+                   varflags & VSQUOTE || (ifsset() && ifsval()[0] == '\0' &&
+                   (*var == '@' || *var == '*')));
                break;
 
        case VSPLUS:
@@ -946,7 +947,9 @@ numvar:
                        sep = ' ';
                for (ap = shellparam.p ; (p = *ap++) != NULL ; ) {
                        strtodest(p, flag, subtype, quoted);
-                       if (*ap && sep)
+                       if (!*ap)
+                               break;
+                       if (sep || (flag & EXP_FULL && !quoted && **ap != '\0'))
                                STPUTC(sep, expdest);
                }
                break;
diff --git a/tools/regression/bin/sh/expansion/ifs4.0 b/tools/regression/bin/sh/expansion/ifs4.0
new file mode 100644 (file)
index 0000000..f29cfea
--- /dev/null
@@ -0,0 +1,39 @@
+# $FreeBSD: src/tools/regression/bin/sh/expansion/ifs4.0,v 1.1 2011/05/27 15:56:13 jilles Exp $
+
+c=: e= s=' '
+failures=''
+ok=''
+
+check_result() {
+       if [ "x$2" = "x$3" ]; then
+               ok=x$ok
+       else
+               failures=x$failures
+               echo "For $1, expected $3 actual $2"
+       fi
+}
+
+IFS='  
+'
+set -- a b '' c
+set -- $@
+check_result 'set -- $@' "($#)($1)($2)($3)($4)" "(3)(a)(b)(c)()"
+
+IFS=''
+set -- a b '' c
+set -- $@
+check_result 'set -- $@' "($#)($1)($2)($3)($4)" "(3)(a)(b)(c)()"
+
+set -- a b '' c
+set -- $*
+check_result 'set -- $*' "($#)($1)($2)($3)($4)" "(3)(a)(b)(c)()"
+
+set -- a b '' c
+set -- "$@"
+check_result 'set -- "$@"' "($#)($1)($2)($3)($4)" "(4)(a)(b)()(c)"
+
+set -- a b '' c
+set -- "$*"
+check_result 'set -- "$*"' "($#)($1)($2)($3)($4)" "(1)(abc)()()()"
+
+test "x$failures" = x