2 * Copyright (c) 2002 Tim J. Robbins.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include "namespace.h"
28 #include <sys/cdefs.h>
29 #include <sys/types.h>
40 #include "un-namespace.h"
41 #include "libc_private.h"
43 __FBSDID("$FreeBSD$");
45 static int we_askshell(const char *, wordexp_t *, int);
46 static int we_check(const char *, int);
50 * Perform shell word expansion on `words' and place the resulting list
51 * of words in `we'. See wordexp(3).
53 * Specified by IEEE Std. 1003.1-2001.
56 wordexp(const char * __restrict words, wordexp_t * __restrict we, int flags)
60 if (flags & WRDE_REUSE)
62 if ((flags & WRDE_APPEND) == 0) {
65 we->we_strings = NULL;
68 if ((error = we_check(words, flags)) != 0) {
72 if ((error = we_askshell(words, we, flags)) != 0) {
80 we_read_fully(int fd, char *buffer, size_t len)
87 nread = _read(fd, buffer + done, len - done);
88 if (nread == -1 && errno == EINTR)
93 } while (done != len);
99 * Use the `wordexp' /bin/sh builtin function to do most of the work
100 * in expanding the word string. This function is complicated by
104 we_askshell(const char *words, wordexp_t *we, int flags)
106 int pdes[2]; /* Pipe to child */
107 char bbuf[9]; /* Buffer for byte count */
108 char wbuf[9]; /* Buffer for word count */
109 long nwords, nbytes; /* Number of words, bytes from child */
110 long i; /* Handy integer */
111 size_t sofs; /* Offset into we->we_strings */
112 size_t vofs; /* Offset into we->we_wordv */
113 pid_t pid; /* Process ID of child */
114 pid_t wpid; /* waitpid return value */
115 int status; /* Child exit status */
116 int error; /* Our return value */
117 int serrno; /* errno to return */
118 char *np, *p; /* Handy pointers */
119 char *nstrings; /* Temporary for realloc() */
120 char **nwv; /* Temporary for realloc() */
121 sigset_t newsigblock, oldsigblock;
127 if (pipe2(pdes, O_CLOEXEC) < 0)
128 return (WRDE_NOSPACE); /* XXX */
129 (void)sigemptyset(&newsigblock);
130 (void)sigaddset(&newsigblock, SIGCHLD);
131 (void)__libc_sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock);
132 if ((pid = fork()) < 0) {
136 (void)__libc_sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
138 return (WRDE_NOSPACE); /* XXX */
142 * We are the child; just get /bin/sh to run the wordexp
143 * builtin on `words'.
145 (void)__libc_sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
146 if ((pdes[1] != STDOUT_FILENO ?
147 _dup2(pdes[1], STDOUT_FILENO) :
148 _fcntl(pdes[1], F_SETFD, 0)) < 0)
150 execl(_PATH_BSHELL, "sh", flags & WRDE_UNDEF ? "-u" : "+u",
151 "-c", "IFS=$1;eval \"$2\";eval \"wordexp $3\"", "",
152 ifs != NULL ? ifs : " \t\n",
153 flags & WRDE_SHOWERR ? "" : "exec 2>/dev/null", words,
159 * We are the parent; read the output of the shell wordexp function,
160 * which is a 32-bit hexadecimal word count, a 32-bit hexadecimal
161 * byte count (not including terminating null bytes), followed by
162 * the expanded words separated by nulls.
165 if (we_read_fully(pdes[0], wbuf, 8) != 8 ||
166 we_read_fully(pdes[0], bbuf, 8) != 8) {
167 error = flags & WRDE_UNDEF ? WRDE_BADVAL : WRDE_SYNTAX;
171 wbuf[8] = bbuf[8] = '\0';
172 nwords = strtol(wbuf, NULL, 16);
173 nbytes = strtol(bbuf, NULL, 16) + nwords;
176 * Allocate or reallocate (when flags & WRDE_APPEND) the word vector
177 * and string storage buffers for the expanded words we're about to
178 * read from the child.
180 sofs = we->we_nbytes;
182 if ((flags & (WRDE_DOOFFS|WRDE_APPEND)) == (WRDE_DOOFFS|WRDE_APPEND))
184 we->we_wordc += nwords;
185 we->we_nbytes += nbytes;
186 if ((nwv = realloc(we->we_wordv, (we->we_wordc + 1 +
187 (flags & WRDE_DOOFFS ? we->we_offs : 0)) *
188 sizeof(char *))) == NULL) {
189 error = WRDE_NOSPACE;
193 if ((nstrings = realloc(we->we_strings, we->we_nbytes)) == NULL) {
194 error = WRDE_NOSPACE;
197 for (i = 0; i < vofs; i++)
198 if (we->we_wordv[i] != NULL)
199 we->we_wordv[i] += nstrings - we->we_strings;
200 we->we_strings = nstrings;
202 if (we_read_fully(pdes[0], we->we_strings + sofs, nbytes) != nbytes) {
203 error = flags & WRDE_UNDEF ? WRDE_BADVAL : WRDE_SYNTAX;
212 wpid = _waitpid(pid, &status, 0);
213 while (wpid < 0 && errno == EINTR);
214 (void)__libc_sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
219 if (wpid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0)
220 return (flags & WRDE_UNDEF ? WRDE_BADVAL : WRDE_SYNTAX);
223 * Break the null-terminated expanded word strings out into
226 if (vofs == 0 && flags & WRDE_DOOFFS)
227 while (vofs < we->we_offs)
228 we->we_wordv[vofs++] = NULL;
229 p = we->we_strings + sofs;
230 while (nwords-- != 0) {
231 we->we_wordv[vofs++] = p;
232 if ((np = memchr(p, '\0', nbytes)) == NULL)
233 return (WRDE_NOSPACE); /* XXX */
234 nbytes -= np - p + 1;
237 we->we_wordv[vofs] = NULL;
244 * Check that the string contains none of the following unquoted
245 * special characters: <newline> |&;<>(){}
246 * or command substitutions when WRDE_NOCMD is set in flags.
249 we_check(const char *words, int flags)
252 int dquote, level, quote, squote;
254 quote = squote = dquote = 0;
255 while ((c = *words++) != '\0') {
262 if (quote + dquote == 0)
266 if (quote + squote == 0)
270 if (quote + squote == 0 && flags & WRDE_NOCMD)
271 return (WRDE_CMDSUB);
272 while ((c = *words++) != '\0' && c != '`')
273 if (c == '\\' && (c = *words++) == '\0')
276 return (WRDE_SYNTAX);
278 case '|': case '&': case ';': case '<': case '>':
279 case '{': case '}': case '(': case ')': case '\n':
280 if (quote + squote + dquote == 0)
281 return (WRDE_BADCHAR);
284 if ((c = *words++) == '\0')
286 else if (quote + squote == 0 && c == '(') {
287 if (flags & WRDE_NOCMD && *words != '(')
288 return (WRDE_CMDSUB);
290 while ((c = *words++) != '\0') {
292 if ((c = *words++) == '\0')
296 else if (c == ')' && --level == 0)
299 if (c == '\0' || level != 0)
300 return (WRDE_SYNTAX);
301 } else if (quote + squote == 0 && c == '{') {
303 while ((c = *words++) != '\0') {
305 if ((c = *words++) == '\0')
309 else if (c == '}' && --level == 0)
312 if (c == '\0' || level != 0)
313 return (WRDE_SYNTAX);
322 if (quote + squote + dquote != 0)
323 return (WRDE_SYNTAX);
330 * Free the result of wordexp(). See wordexp(3).
332 * Specified by IEEE Std. 1003.1-2001.
335 wordfree(wordexp_t *we)
341 free(we->we_strings);
343 we->we_strings = NULL;