From 715a0dd5564694171f758c3c633da5f492fcbf5b Mon Sep 17 00:00:00 2001 From: Jilles Tjoelker Date: Fri, 27 May 2011 15:56:13 +0000 Subject: [PATCH] sh: Fix unquoted $@/$* if IFS=''. 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. --- bin/sh/expand.c | 7 +++-- tools/regression/bin/sh/expansion/ifs4.0 | 39 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 tools/regression/bin/sh/expansion/ifs4.0 diff --git a/bin/sh/expand.c b/bin/sh/expand.c index b3c4962161f7..108a77c4be68 100644 --- a/bin/sh/expand.c +++ b/bin/sh/expand.c @@ -761,7 +761,8 @@ evalvar(char *p, int flag) break; record: recordregion(startloc, expdest - stackblock(), - varflags & VSQUOTE); + varflags & VSQUOTE || (ifsset() && ifsval()[0] == '\0' && + (*var == '@' || *var == '*'))); break; case VSPLUS: @@ -947,7 +948,9 @@ varvalue(char *name, int quoted, int subtype, int flag) 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 index 000000000000..5b896a25413b --- /dev/null +++ b/tools/regression/bin/sh/expansion/ifs4.0 @@ -0,0 +1,39 @@ +# $FreeBSD$ + +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