2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002 Tim J. Robbins.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: head/lib/libc/gen/wordexp.c 326193 2017-11-25 17:12:48Z pfg $
31 #include "namespace.h"
32 #include <sys/types.h>
44 #include "un-namespace.h"
46 static int we_askshell(const char *, wordexp_t *, int);
47 static int we_check(const char *);
51 * Perform shell word expansion on `words' and place the resulting list
52 * of words in `we'. See wordexp(3).
54 * Specified by IEEE Std. 1003.1-2001.
57 wordexp(const char * __restrict words, wordexp_t * __restrict we, int flags)
61 if (flags & WRDE_REUSE)
63 if ((flags & WRDE_APPEND) == 0) {
66 we->we_strings = NULL;
69 if ((error = we_check(words)) != 0) {
73 if ((error = we_askshell(words, we, flags)) != 0) {
81 we_read_fully(int fd, char *buffer, size_t len)
88 nread = _read(fd, buffer + done, len - done);
89 if (nread == -1 && errno == EINTR)
94 } while (done != len);
99 we_write_fully(int fd, const char *buffer, size_t len)
106 nwritten = _write(fd, buffer + done, len - done);
107 if (nwritten == -1 && errno == EINTR)
112 } while (done != len);
118 * Use the `wordexp2' /bin/sh builtin function to do most of the
119 * work in expanding the word string. This function is complicated by
123 we_askshell(const char *words, wordexp_t *we, int flags)
125 int pdesw[2]; /* Pipe for writing words */
126 int pdes[2]; /* Pipe for reading output */
127 char wfdstr[sizeof(int) * 3 + 1];
128 char buf[35]; /* Buffer for byte and word count */
129 long nwords, nbytes; /* Number of words, bytes from child */
130 long i; /* Handy integer */
131 size_t sofs; /* Offset into we->we_strings */
132 size_t vofs; /* Offset into we->we_wordv */
133 pid_t pid; /* Process ID of child */
134 pid_t wpid; /* waitpid return value */
135 int status; /* Child exit status */
136 int error; /* Our return value */
137 int serrno; /* errno to return */
138 char *np, *p; /* Handy pointers */
139 char *nstrings; /* Temporary for realloc() */
140 char **nwv; /* Temporary for realloc() */
141 sigset_t newsigblock, oldsigblock;
147 if (pipe2(pdesw, O_CLOEXEC) < 0)
148 return (WRDE_NOSPACE); /* XXX */
149 snprintf(wfdstr, sizeof(wfdstr), "%d", pdesw[0]);
150 if (pipe2(pdes, O_CLOEXEC) < 0) {
153 return (WRDE_NOSPACE); /* XXX */
155 sigemptyset(&newsigblock);
156 sigaddset(&newsigblock, SIGCHLD);
157 _sigprocmask(SIG_BLOCK, &newsigblock, &oldsigblock);
158 if ((pid = fork()) < 0) {
164 _sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
166 return (WRDE_NOSPACE); /* XXX */
167 } else if (pid == 0) {
169 * We are the child; make /bin/sh expand `words'.
171 _sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
172 if ((pdes[1] != STDOUT_FILENO ?
173 _dup2(pdes[1], STDOUT_FILENO) :
174 _fcntl(pdes[1], F_SETFD, 0)) < 0)
176 if (_fcntl(pdesw[0], F_SETFD, 0) < 0)
178 execl(_PATH_BSHELL, "sh", flags & WRDE_UNDEF ? "-u" : "+u",
179 "-c", "IFS=$1;eval \"$2\";wordexp2 -f \"$3\" ${4:+\"$4\"}",
181 ifs != NULL ? ifs : " \t\n",
182 flags & WRDE_SHOWERR ? "" : "exec 2>/dev/null",
184 flags & WRDE_NOCMD ? "-p" : "",
190 * We are the parent; write the words.
194 if (!we_write_fully(pdesw[1], words, strlen(words))) {
201 * Read the output of the shell wordexp function,
202 * which is a byte indicating that the words were parsed successfully,
203 * a 64-bit hexadecimal word count, a dummy byte, a 64-bit hexadecimal
204 * byte count (not including terminating null bytes), followed by the
205 * expanded words separated by nulls.
207 switch (we_read_fully(pdes[0], buf, 34)) {
209 error = buf[0] == 'C' ? WRDE_CMDSUB : WRDE_BADVAL;
220 nwords = strtol(buf + 1, NULL, 16);
222 nbytes = strtol(buf + 18, NULL, 16) + nwords;
225 * Allocate or reallocate (when flags & WRDE_APPEND) the word vector
226 * and string storage buffers for the expanded words we're about to
227 * read from the child.
229 sofs = we->we_nbytes;
231 if ((flags & (WRDE_DOOFFS|WRDE_APPEND)) == (WRDE_DOOFFS|WRDE_APPEND))
233 we->we_wordc += nwords;
234 we->we_nbytes += nbytes;
235 if ((nwv = reallocarray(we->we_wordv, (we->we_wordc + 1 +
236 (flags & WRDE_DOOFFS ? we->we_offs : 0)),
237 sizeof(char *))) == NULL) {
238 error = WRDE_NOSPACE;
242 if ((nstrings = realloc(we->we_strings, we->we_nbytes)) == NULL) {
243 error = WRDE_NOSPACE;
246 for (i = 0; i < vofs; i++)
247 if (we->we_wordv[i] != NULL)
248 we->we_wordv[i] += nstrings - we->we_strings;
249 we->we_strings = nstrings;
251 if (we_read_fully(pdes[0], we->we_strings + sofs, nbytes) != nbytes) {
252 error = WRDE_NOSPACE; /* abort for unknown reason */
261 wpid = _waitpid(pid, &status, 0);
262 while (wpid < 0 && errno == EINTR);
263 _sigprocmask(SIG_SETMASK, &oldsigblock, NULL);
268 if (wpid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0)
269 return (WRDE_NOSPACE); /* abort for unknown reason */
272 * Break the null-terminated expanded word strings out into
275 if (vofs == 0 && flags & WRDE_DOOFFS)
276 while (vofs < we->we_offs)
277 we->we_wordv[vofs++] = NULL;
278 p = we->we_strings + sofs;
279 while (nwords-- != 0) {
280 we->we_wordv[vofs++] = p;
281 if ((np = memchr(p, '\0', nbytes)) == NULL)
282 return (WRDE_NOSPACE); /* XXX */
283 nbytes -= np - p + 1;
286 we->we_wordv[vofs] = NULL;
293 * Check that the string contains none of the following unquoted
294 * special characters: <newline> |&;<>(){}
295 * This mainly serves for {} which are normally legal in sh.
296 * It deliberately does not attempt to model full sh syntax.
299 we_check(const char *words)
302 /* Saw \ or $, possibly not special: */
303 bool quote = false, dollar = false;
304 /* Saw ', ", ${, ` or $(, possibly not special: */
305 bool have_sq = false, have_dq = false, have_par_begin = false;
306 bool have_cmd = false;
307 /* Definitely saw a ', ", ${, ` or $(, need a closing character: */
308 bool need_sq = false, need_dq = false, need_par_end = false;
309 bool need_cmd_old = false, need_cmd_new = false;
311 while ((c = *words++) != '\0') {
323 if (!quote && !have_sq && !have_dq)
330 if (!quote && !have_sq && !have_dq)
337 if (!quote && !have_sq && !have_cmd)
340 need_cmd_old = false;
344 if (!quote && !dollar && !have_sq && !have_dq &&
346 return (WRDE_BADCHAR);
348 if (!quote && !have_sq)
350 have_par_begin = true;
354 if (!quote && !have_sq && !have_dq && !have_par_begin &&
356 return (WRDE_BADCHAR);
357 need_par_end = false;
360 if (!quote && !dollar && !have_sq && !have_dq &&
362 return (WRDE_BADCHAR);
364 if (!quote && !have_sq)
370 if (!quote && !have_sq && !have_dq && !have_cmd)
371 return (WRDE_BADCHAR);
372 need_cmd_new = false;
374 case '|': case '&': case ';': case '<': case '>': case '\n':
375 if (!quote && !have_sq && !have_dq && !have_cmd)
376 return (WRDE_BADCHAR);
381 quote = dollar = false;
383 if (quote || dollar || need_sq || need_dq || need_par_end ||
384 need_cmd_old || need_cmd_new)
385 return (WRDE_SYNTAX);
392 * Free the result of wordexp(). See wordexp(3).
394 * Specified by IEEE Std. 1003.1-2001.
397 wordfree(wordexp_t *we)
403 free(we->we_strings);
405 we->we_strings = NULL;