wordexp(): Remove wrong IFS usage.

Words in shell script are separated by spaces or tabs independent of the
value of IFS. The value of IFS is only relevant for the result of
substitutions. Therefore, there should be a space between 'wordexp' and the
words to be expanded, not an IFS character.

Paranoia might dictate that the shell ignore IFS from the environment (even
though our sh currently uses it), so do not depend on it in the new test
case.
This commit is contained in:
Jilles Tjoelker 2013-04-01 20:50:07 +00:00
parent 587feafb5a
commit 70d41b5846
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=248987
2 changed files with 20 additions and 4 deletions

View File

@ -114,15 +114,12 @@ we_askshell(const char *words, wordexp_t *we, int flags)
int status; /* Child exit status */
int error; /* Our return value */
int serrno; /* errno to return */
char *ifs; /* IFS env. var. */
char *np, *p; /* Handy pointers */
char *nstrings; /* Temporary for realloc() */
char **nwv; /* Temporary for realloc() */
sigset_t newsigblock, oldsigblock;
serrno = errno;
if ((ifs = getenv("IFS")) == NULL)
ifs = " \t\n";
if (pipe(pdes) < 0)
return (WRDE_NOSPACE); /* XXX */
@ -150,7 +147,7 @@ we_askshell(const char *words, wordexp_t *we, int flags)
if (_dup2(pdes[1], STDOUT_FILENO) < 0)
_exit(1);
_close(pdes[1]);
if (asprintf(&cmd, "wordexp%c%s\n", *ifs, words) < 0)
if (asprintf(&cmd, "wordexp %s\n", words) < 0)
_exit(1);
if ((flags & WRDE_SHOWERR) == 0) {
if ((devnull = _open(_PATH_DEVNULL, O_RDWR, 0666)) < 0)

View File

@ -208,6 +208,25 @@ main(int argc, char *argv[])
assert(strcmp(we.we_wordv[1], "world") == 0);
assert(we.we_wordv[2] == NULL);
wordfree(&we);
sa.sa_handler = SIG_DFL;
r = sigaction(SIGCHLD, &sa, NULL);
assert(r == 0);
/*
* With IFS set to a non-default value (without depending on whether
* IFS is inherited or not).
*/
r = setenv("IFS", ":", 1);
assert(r == 0);
r = wordexp("hello world", &we, 0);
assert(r == 0);
assert(we.we_wordc == 2);
assert(strcmp(we.we_wordv[0], "hello") == 0);
assert(strcmp(we.we_wordv[1], "world") == 0);
assert(we.we_wordv[2] == NULL);
wordfree(&we);
r = unsetenv("IFS");
assert(r == 0);
printf("PASS wordexp()\n");
printf("PASS wordfree()\n");