Index: head/lib/libc/gen/wordexp.3 =================================================================== --- head/lib/libc/gen/wordexp.3 (revision 286940) +++ head/lib/libc/gen/wordexp.3 (revision 286941) @@ -1,205 +1,208 @@ .\" .\" Copyright (c) 2002 Tim J. Robbins .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" $FreeBSD$ .\" .Dd August 18, 2015 .Dt WORDEXP 3 .Os .Sh NAME .Nm wordexp .Nd "perform shell-style word expansions" .Sh LIBRARY .Lb libc .Sh SYNOPSIS .In wordexp.h .Ft int .Fn wordexp "const char * restrict words" "wordexp_t * restrict we" "int flags" .Ft void .Fn wordfree "wordexp_t *we" .Sh DESCRIPTION The .Fn wordexp function performs shell-style word expansion on .Fa words and places the list of words into the .Va we_wordv member of .Fa we , and the number of words into .Va we_wordc . .Pp The .Fa flags argument is the bitwise inclusive OR of any of the following constants: .Bl -tag -width ".Dv WRDE_SHOWERR" .It Dv WRDE_APPEND Append the words to those generated by a previous call to .Fn wordexp . .It Dv WRDE_DOOFFS As many .Dv NULL pointers as are specified by the .Va we_offs member of .Fa we are added to the front of .Va we_wordv . .It Dv WRDE_NOCMD Disallow command substitution in .Fa words . See the note in .Sx BUGS before using this. .It Dv WRDE_REUSE The .Fa we argument was passed to a previous successful call to .Fn wordexp but has not been passed to .Fn wordfree . The implementation may reuse the space allocated to it. .It Dv WRDE_SHOWERR Do not redirect shell error messages to .Pa /dev/null . .It Dv WRDE_UNDEF Report error on an attempt to expand an undefined shell variable. .El .Pp The .Vt wordexp_t structure is defined in .In wordexp.h as: .Bd -literal -offset indent typedef struct { size_t we_wordc; /* count of words matched */ char **we_wordv; /* pointer to list of words */ size_t we_offs; /* slots to reserve in we_wordv */ } wordexp_t; .Ed .Pp The .Fn wordfree function frees the memory allocated by .Fn wordexp . .Sh IMPLEMENTATION NOTES The .Fn wordexp function is implemented by executing .Xr sh 1 . .Sh RETURN VALUES The .Fn wordexp function returns zero if successful, otherwise it returns one of the following error codes: .Bl -tag -width ".Dv WRDE_NOSPACE" .It Dv WRDE_BADCHAR The .Fa words argument contains one of the following unquoted characters: .Aq newline , .Ql | , .Ql & , .Ql \&; , .Ql < , .Ql > , .Ql \&( , .Ql \&) , .Ql { , .Ql } . .It Dv WRDE_BADVAL -An attempt was made to expand an undefined shell variable and +An error after successful parsing, +such as an attempt to expand an undefined shell variable with .Dv WRDE_UNDEF -is set in +set in .Fa flags . .It Dv WRDE_CMDSUB An attempt was made to use command substitution and .Dv WRDE_NOCMD is set in .Fa flags . .It Dv WRDE_NOSPACE -Not enough memory to store the result. +Not enough memory to store the result or +an error during +.Xr fork 2 . .It Dv WRDE_SYNTAX Shell syntax error in .Fa words . .El .Pp The .Fn wordfree function returns no value. .Sh ENVIRONMENT .Bl -tag -width ".Ev IFS" .It Ev IFS Field separator. .El .Sh EXAMPLES Invoke the editor on all .Pa .c files in the current directory and .Pa /etc/motd (error checking omitted): .Bd -literal -offset indent wordexp_t we; wordexp("${EDITOR:-vi} *.c /etc/motd", &we, 0); execvp(we.we_wordv[0], we.we_wordv); .Ed .Sh DIAGNOSTICS Diagnostic messages from the shell are written to the standard error output if .Dv WRDE_SHOWERR is set in .Fa flags . .Sh SEE ALSO .Xr sh 1 , .Xr fnmatch 3 , .Xr glob 3 , .Xr popen 3 , .Xr system 3 .Sh STANDARDS The .Fn wordexp and .Fn wordfree functions conform to .St -p1003.1-2001 . .Sh BUGS Do not pass untrusted user data to .Fn wordexp , regardless of whether the .Dv WRDE_NOCMD flag is set. The .Fn wordexp function attempts to detect input that would cause commands to be executed before passing it to the shell but it does not use the same parser so it may be fooled. .Pp The current .Fn wordexp implementation does not recognize multibyte characters other than UTF-8, since the shell (which it invokes to perform expansions) does not. Index: head/lib/libc/gen/wordexp.c =================================================================== --- head/lib/libc/gen/wordexp.c (revision 286940) +++ head/lib/libc/gen/wordexp.c (revision 286941) @@ -1,346 +1,357 @@ /*- * Copyright (c) 2002 Tim J. Robbins. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include "namespace.h" #include #include #include #include #include #include #include #include #include #include #include #include #include "un-namespace.h" __FBSDID("$FreeBSD$"); static int we_askshell(const char *, wordexp_t *, int); static int we_check(const char *, int); /* * wordexp -- * Perform shell word expansion on `words' and place the resulting list * of words in `we'. See wordexp(3). * * Specified by IEEE Std. 1003.1-2001. */ int wordexp(const char * __restrict words, wordexp_t * __restrict we, int flags) { int error; if (flags & WRDE_REUSE) wordfree(we); if ((flags & WRDE_APPEND) == 0) { we->we_wordc = 0; we->we_wordv = NULL; we->we_strings = NULL; we->we_nbytes = 0; } if ((error = we_check(words, flags)) != 0) { wordfree(we); return (error); } if ((error = we_askshell(words, we, flags)) != 0) { wordfree(we); return (error); } return (0); } static size_t we_read_fully(int fd, char *buffer, size_t len) { size_t done; ssize_t nread; done = 0; do { nread = _read(fd, buffer + done, len - done); if (nread == -1 && errno == EINTR) continue; if (nread <= 0) break; done += nread; } while (done != len); return done; } /* * we_askshell -- * Use the `wordexp' /bin/sh builtin function to do most of the work * in expanding the word string. This function is complicated by * memory management. */ static int we_askshell(const char *words, wordexp_t *we, int flags) { int pdes[2]; /* Pipe to child */ - char bbuf[9]; /* Buffer for byte count */ - char wbuf[9]; /* Buffer for word count */ + char buf[18]; /* Buffer for byte and word count */ long nwords, nbytes; /* Number of words, bytes from child */ long i; /* Handy integer */ size_t sofs; /* Offset into we->we_strings */ size_t vofs; /* Offset into we->we_wordv */ pid_t pid; /* Process ID of child */ pid_t wpid; /* waitpid return value */ int status; /* Child exit status */ int error; /* Our return value */ int serrno; /* errno to return */ char *np, *p; /* Handy pointers */ char *nstrings; /* Temporary for realloc() */ char **nwv; /* Temporary for realloc() */ sigset_t newsigblock, oldsigblock; const char *ifs; + char save; serrno = errno; ifs = getenv("IFS"); if (pipe2(pdes, O_CLOEXEC) < 0) return (WRDE_NOSPACE); /* XXX */ (void)sigemptyset(&newsigblock); (void)sigaddset(&newsigblock, SIGCHLD); (void)_sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock); if ((pid = fork()) < 0) { serrno = errno; _close(pdes[0]); _close(pdes[1]); (void)_sigprocmask(SIG_SETMASK, &oldsigblock, NULL); errno = serrno; return (WRDE_NOSPACE); /* XXX */ } else if (pid == 0) { /* * We are the child; make /bin/sh expand `words'. */ (void)_sigprocmask(SIG_SETMASK, &oldsigblock, NULL); if ((pdes[1] != STDOUT_FILENO ? _dup2(pdes[1], STDOUT_FILENO) : _fcntl(pdes[1], F_SETFD, 0)) < 0) _exit(1); execl(_PATH_BSHELL, "sh", flags & WRDE_UNDEF ? "-u" : "+u", - "-c", "IFS=$1;eval \"$2\";eval \"set -- $3\";IFS=;a=\"$*\";" - "printf '%08x' \"$#\" \"${#a}\";printf '%s\\0' \"$@\"", + "-c", "IFS=$1;eval \"$2\";eval \"echo;set -- $3\";" + "IFS=;a=\"$*\";printf '%08x' \"$#\" \"${#a}\";" + "printf '%s\\0' \"$@\"", "", ifs != NULL ? ifs : " \t\n", flags & WRDE_SHOWERR ? "" : "exec 2>/dev/null", words, (char *)NULL); _exit(1); } /* * We are the parent; read the output of the shell wordexp function, - * which is a 32-bit hexadecimal word count, a 32-bit hexadecimal - * byte count (not including terminating null bytes), followed by - * the expanded words separated by nulls. + * which is a byte indicating that the words were parsed successfully, + * a 32-bit hexadecimal word count, a 32-bit hexadecimal byte count + * (not including terminating null bytes), followed by the expanded + * words separated by nulls. */ _close(pdes[1]); - if (we_read_fully(pdes[0], wbuf, 8) != 8 || - we_read_fully(pdes[0], bbuf, 8) != 8) { - error = flags & WRDE_UNDEF ? WRDE_BADVAL : WRDE_SYNTAX; + switch (we_read_fully(pdes[0], buf, 17)) { + case 1: + error = WRDE_BADVAL; serrno = errno; goto cleanup; + case 17: + break; + default: + error = WRDE_SYNTAX; + serrno = errno; + goto cleanup; } - wbuf[8] = bbuf[8] = '\0'; - nwords = strtol(wbuf, NULL, 16); - nbytes = strtol(bbuf, NULL, 16) + nwords; + save = buf[9]; + buf[9] = '\0'; + nwords = strtol(buf + 1, NULL, 16); + buf[9] = save; + buf[17] = '\0'; + nbytes = strtol(buf + 9, NULL, 16) + nwords; /* * Allocate or reallocate (when flags & WRDE_APPEND) the word vector * and string storage buffers for the expanded words we're about to * read from the child. */ sofs = we->we_nbytes; vofs = we->we_wordc; if ((flags & (WRDE_DOOFFS|WRDE_APPEND)) == (WRDE_DOOFFS|WRDE_APPEND)) vofs += we->we_offs; we->we_wordc += nwords; we->we_nbytes += nbytes; if ((nwv = realloc(we->we_wordv, (we->we_wordc + 1 + (flags & WRDE_DOOFFS ? we->we_offs : 0)) * sizeof(char *))) == NULL) { error = WRDE_NOSPACE; goto cleanup; } we->we_wordv = nwv; if ((nstrings = realloc(we->we_strings, we->we_nbytes)) == NULL) { error = WRDE_NOSPACE; goto cleanup; } for (i = 0; i < vofs; i++) if (we->we_wordv[i] != NULL) we->we_wordv[i] += nstrings - we->we_strings; we->we_strings = nstrings; if (we_read_fully(pdes[0], we->we_strings + sofs, nbytes) != nbytes) { - error = flags & WRDE_UNDEF ? WRDE_BADVAL : WRDE_SYNTAX; + error = WRDE_NOSPACE; /* abort for unknown reason */ serrno = errno; goto cleanup; } error = 0; cleanup: _close(pdes[0]); do wpid = _waitpid(pid, &status, 0); while (wpid < 0 && errno == EINTR); (void)_sigprocmask(SIG_SETMASK, &oldsigblock, NULL); if (error != 0) { errno = serrno; return (error); } if (wpid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) - return (flags & WRDE_UNDEF ? WRDE_BADVAL : WRDE_SYNTAX); + return (WRDE_NOSPACE); /* abort for unknown reason */ /* * Break the null-terminated expanded word strings out into * the vector. */ if (vofs == 0 && flags & WRDE_DOOFFS) while (vofs < we->we_offs) we->we_wordv[vofs++] = NULL; p = we->we_strings + sofs; while (nwords-- != 0) { we->we_wordv[vofs++] = p; if ((np = memchr(p, '\0', nbytes)) == NULL) return (WRDE_NOSPACE); /* XXX */ nbytes -= np - p + 1; p = np + 1; } we->we_wordv[vofs] = NULL; return (0); } /* * we_check -- * Check that the string contains none of the following unquoted * special characters: |&;<>(){} * or command substitutions when WRDE_NOCMD is set in flags. */ static int we_check(const char *words, int flags) { char c; int dquote, level, quote, squote; quote = squote = dquote = 0; while ((c = *words++) != '\0') { switch (c) { case '\\': if (squote == 0) quote ^= 1; continue; case '\'': if (quote + dquote == 0) squote ^= 1; break; case '"': if (quote + squote == 0) dquote ^= 1; break; case '`': if (quote + squote == 0 && flags & WRDE_NOCMD) return (WRDE_CMDSUB); while ((c = *words++) != '\0' && c != '`') if (c == '\\' && (c = *words++) == '\0') break; if (c == '\0') return (WRDE_SYNTAX); break; case '|': case '&': case ';': case '<': case '>': case '{': case '}': case '(': case ')': case '\n': if (quote + squote + dquote == 0) return (WRDE_BADCHAR); break; case '$': if ((c = *words++) == '\0') break; else if (quote + squote == 0 && c == '(') { if (flags & WRDE_NOCMD && *words != '(') return (WRDE_CMDSUB); level = 1; while ((c = *words++) != '\0') { if (c == '\\') { if ((c = *words++) == '\0') break; } else if (c == '(') level++; else if (c == ')' && --level == 0) break; } if (c == '\0' || level != 0) return (WRDE_SYNTAX); } else if (quote + squote == 0 && c == '{') { level = 1; while ((c = *words++) != '\0') { if (c == '\\') { if ((c = *words++) == '\0') break; } else if (c == '{') level++; else if (c == '}' && --level == 0) break; } if (c == '\0' || level != 0) return (WRDE_SYNTAX); } else --words; break; default: break; } quote = 0; } if (quote + squote + dquote != 0) return (WRDE_SYNTAX); return (0); } /* * wordfree -- * Free the result of wordexp(). See wordexp(3). * * Specified by IEEE Std. 1003.1-2001. */ void wordfree(wordexp_t *we) { if (we == NULL) return; free(we->we_wordv); free(we->we_strings); we->we_wordv = NULL; we->we_strings = NULL; we->we_nbytes = 0; we->we_wordc = 0; } Index: head/tools/regression/lib/libc/gen/test-wordexp.c =================================================================== --- head/tools/regression/lib/libc/gen/test-wordexp.c (revision 286940) +++ head/tools/regression/lib/libc/gen/test-wordexp.c (revision 286941) @@ -1,262 +1,271 @@ /*- * Copyright (c) 2003 Tim J. Robbins * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * Test program for wordexp() and wordfree() as specified by * IEEE Std. 1003.1-2001. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include static void chld_handler(int x) { int status, serrno; (void)x; serrno = errno; while (waitpid(-1, &status, WNOHANG) > 0) ; errno = serrno; } int main(int argc, char *argv[]) { struct sigaction sa; wordexp_t we; int r; int i; char longdata[6 * 10000 + 1]; /* Test that the macros are there. */ (void)(WRDE_APPEND + WRDE_DOOFFS + WRDE_NOCMD + WRDE_REUSE + WRDE_SHOWERR + WRDE_UNDEF); (void)(WRDE_BADCHAR + WRDE_BADVAL + WRDE_CMDSUB + WRDE_NOSPACE + WRDE_SYNTAX); /* Simple test. */ 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); /* Long output. */ for (i = 0; i < 10000; i++) snprintf(longdata + 6 * i, 7, "%05d ", i); r = wordexp(longdata, &we, 0); assert(r == 0); assert(we.we_wordc == 10000); assert(we.we_wordv[10000] == NULL); wordfree(&we); /* WRDE_DOOFFS */ we.we_offs = 3; r = wordexp("hello world", &we, WRDE_DOOFFS); assert(r == 0); assert(we.we_wordc == 2); assert(we.we_wordv[0] == NULL); assert(we.we_wordv[1] == NULL); assert(we.we_wordv[2] == NULL); assert(strcmp(we.we_wordv[3], "hello") == 0); assert(strcmp(we.we_wordv[4], "world") == 0); assert(we.we_wordv[5] == NULL); wordfree(&we); /* WRDE_REUSE */ r = wordexp("hello world", &we, 0); r = wordexp("hello world", &we, WRDE_REUSE); 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); /* WRDE_APPEND */ r = wordexp("this is", &we, 0); assert(r == 0); r = wordexp("a test", &we, WRDE_APPEND); assert(r == 0); assert(we.we_wordc == 4); assert(strcmp(we.we_wordv[0], "this") == 0); assert(strcmp(we.we_wordv[1], "is") == 0); assert(strcmp(we.we_wordv[2], "a") == 0); assert(strcmp(we.we_wordv[3], "test") == 0); assert(we.we_wordv[4] == NULL); wordfree(&we); /* WRDE_DOOFFS + WRDE_APPEND */ we.we_offs = 2; r = wordexp("this is", &we, WRDE_DOOFFS); assert(r == 0); r = wordexp("a test", &we, WRDE_APPEND|WRDE_DOOFFS); assert(r == 0); r = wordexp("of wordexp", &we, WRDE_APPEND|WRDE_DOOFFS); assert(r == 0); assert(we.we_wordc == 6); assert(we.we_wordv[0] == NULL); assert(we.we_wordv[1] == NULL); assert(strcmp(we.we_wordv[2], "this") == 0); assert(strcmp(we.we_wordv[3], "is") == 0); assert(strcmp(we.we_wordv[4], "a") == 0); assert(strcmp(we.we_wordv[5], "test") == 0); assert(strcmp(we.we_wordv[6], "of") == 0); assert(strcmp(we.we_wordv[7], "wordexp") == 0); assert(we.we_wordv[8] == NULL); wordfree(&we); /* WRDE_UNDEF */ r = wordexp("${dont_set_me}", &we, WRDE_UNDEF); assert(r == WRDE_BADVAL); /* WRDE_NOCMD */ r = wordexp("`date`", &we, WRDE_NOCMD); assert(r == WRDE_CMDSUB); r = wordexp("\"`date`\"", &we, WRDE_NOCMD); assert(r == WRDE_CMDSUB); r = wordexp("$(date)", &we, WRDE_NOCMD); assert(r == WRDE_CMDSUB); r = wordexp("\"$(date)\"", &we, WRDE_NOCMD); assert(r == WRDE_CMDSUB); r = wordexp("$((3+5))", &we, WRDE_NOCMD); assert(r == 0); r = wordexp("\\$\\(date\\)", &we, WRDE_NOCMD|WRDE_REUSE); assert(r == 0); r = wordexp("'`date`'", &we, WRDE_NOCMD|WRDE_REUSE); assert(r == 0); r = wordexp("'$(date)'", &we, WRDE_NOCMD|WRDE_REUSE); assert(r == 0); wordfree(&we); /* WRDE_BADCHAR */ r = wordexp("'\n|&;<>(){}'", &we, 0); assert(r == 0); r = wordexp("\"\n|&;<>(){}\"", &we, WRDE_REUSE); assert(r == 0); r = wordexp("\\\n\\|\\&\\;\\<\\>\\(\\)\\{\\}", &we, WRDE_REUSE); assert(r == 0); wordfree(&we); r = wordexp("test \n test", &we, 0); assert(r == WRDE_BADCHAR); r = wordexp("test | test", &we, 0); assert(r == WRDE_BADCHAR); r = wordexp("test & test", &we, 0); assert(r == WRDE_BADCHAR); r = wordexp("test ; test", &we, 0); assert(r == WRDE_BADCHAR); r = wordexp("test > test", &we, 0); assert(r == WRDE_BADCHAR); r = wordexp("test < test", &we, 0); assert(r == WRDE_BADCHAR); r = wordexp("test ( test", &we, 0); assert(r == WRDE_BADCHAR); r = wordexp("test ) test", &we, 0); assert(r == WRDE_BADCHAR); r = wordexp("test { test", &we, 0); assert(r == WRDE_BADCHAR); r = wordexp("test } test", &we, 0); assert(r == WRDE_BADCHAR); /* WRDE_SYNTAX */ r = wordexp("'", &we, 0); assert(r == WRDE_SYNTAX); r = wordexp("'", &we, WRDE_UNDEF); assert(r == WRDE_SYNTAX); r = wordexp("'\\'", &we, 0); assert(r == 0); assert(we.we_wordc == 1); assert(strcmp(we.we_wordv[0], "\\") == 0); assert(we.we_wordv[1] == NULL); wordfree(&we); + /* Two syntax errors that are not detected by the current we_check(). */ + r = wordexp("${IFS:+'}", &we, 0); + assert(r == WRDE_SYNTAX); + r = wordexp("${IFS:+'}", &we, WRDE_UNDEF); + assert(r == WRDE_SYNTAX); + r = wordexp("$(case)", &we, 0); + assert(r == WRDE_SYNTAX); + r = wordexp("$(case)", &we, WRDE_UNDEF); + assert(r == WRDE_SYNTAX); /* With a SIGCHLD handler that reaps all zombies. */ sa.sa_flags = 0; sigemptyset(&sa.sa_mask); sa.sa_handler = chld_handler; r = sigaction(SIGCHLD, &sa, NULL); 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); 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); /* * With IFS set to a non-default value, and using it. */ r = setenv("IFS", ":", 1); assert(r == 0); r = wordexp("${IFS+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"); return (0); }