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 <TargetConditionals.h>
30 /* <rdar://problem/13875458> */
33 int wordexp(const char *restrict words __unused
, wordexp_t
*restrict pwordexp __unused
, int flags __unused
) {
37 void wordfree(wordexp_t
*pwordexp __unused
) {
42 #include "namespace.h"
43 #include <sys/cdefs.h>
44 #include <sys/types.h>
55 #include "un-namespace.h"
57 // For _NSGetEnviron() -- which gives us a pointer to environ
58 #include <crt_externs.h>
60 #endif /* __APPLE__ */
62 __FBSDID("$FreeBSD$");
66 * To maintain backwards compatibility with wordexp_t, we can't put
67 * we_strings and we_nbytes in wordexp_t. So we create a new structure,
68 * we_int_t, that has wi_strings and wi_nbytes, as well as the we_wordv
77 * Normally, we_wordv will point to wi_wordv, so we need macros to convert
78 * back and forth between wi_wordv and the we_int_t structure.
80 #define WE_INT_T(x) ((we_int_t *)((char *)(x) - sizeof(we_int_t)))
81 #define WE_STRINGS(we) (WE_INT_T((we)->we_wordv)->wi_strings)
82 #define WE_WORDV(x) ((x)->wi_wordv)
85 * bash will return success, yet print a command substitution/syntax error
86 * to stderr. So we need to capture stderr, and see if it contains this error.
88 static const char command_substitution_str
[] = "command substitution";
89 static const char syntax_error_str
[] = "syntax error";
90 static const char unbound_variable_str
[] = " unbound variable";
91 #endif /* __APPLE__ */
93 static int we_askshell(const char *, wordexp_t
*, int);
94 static int we_check(const char *, int);
98 * Perform shell word expansion on `words' and place the resulting list
99 * of words in `we'. See wordexp(3).
101 * Specified by IEEE Std. 1003.1-2001.
105 wordexp(const char * __restrict words
, wordexp_t
* __restrict we0
, int flags
)
106 #else /* !__APPLE__ */
107 wordexp(const char * __restrict words
, wordexp_t
* __restrict we
, int flags
)
108 #endif /* !__APPLE__ */
113 wordexp_t
*we
= &temp
;
115 if ((error
= we_check(words
, flags
)) != 0) return (error
);
116 we
->we_offs
= we0
->we_offs
;
117 if (flags
& WRDE_APPEND
) {
119 size_t vofs
= we0
->we_wordc
+ (flags
& WRDE_DOOFFS
? we0
->we_offs
: 0);
120 we_int_t
*wi0
= WE_INT_T(we0
->we_wordv
);
121 we_int_t
*wi
= malloc((vofs
+ 1) * sizeof(char *) + sizeof(we_int_t
));
123 if (!wi
) return (WRDE_NOSPACE
);
124 memcpy(wi
, wi0
, (vofs
+ 1) * sizeof(char *) + sizeof(we_int_t
));
125 wi
->wi_strings
= malloc(wi
->wi_nbytes
);
126 if (!wi
->wi_strings
) {
128 return (WRDE_NOSPACE
);
130 memcpy(wi
->wi_strings
, wi0
->wi_strings
, wi
->wi_nbytes
);
131 for (i
= 0; i
< vofs
; i
++)
132 if (wi
->wi_wordv
[i
] != NULL
)
133 wi
->wi_wordv
[i
] += wi
->wi_strings
- wi0
->wi_strings
;
134 we
->we_wordc
= we0
->we_wordc
;
135 we
->we_wordv
= WE_WORDV(wi
);
140 #else /* !__APPLE__ */
141 if (flags
& WRDE_REUSE
)
143 if ((flags
& WRDE_APPEND
) == 0) {
146 we
->we_strings
= NULL
;
149 if ((error
= we_check(words
, flags
)) != 0) {
153 #endif /* !__APPLE__ */
154 if ((error
= we_askshell(words
, we
, flags
)) != 0) {
156 if (error
== WRDE_NOSPACE
) {
157 if (flags
& WRDE_REUSE
)
163 #else /* !__APPLE__ */
165 #endif /* !__APPLE__ */
169 if (flags
& WRDE_REUSE
)
172 #endif /* __APPLE__ */
177 we_read_fully(int fd
, char *buffer
, size_t len
)
184 nread
= _read(fd
, buffer
+ done
, len
- done
);
185 if (nread
== -1 && errno
== EINTR
)
190 } while (done
!= len
);
196 * Use the `wordexp' /bin/sh builtin function to do most of the work
197 * in expanding the word string. This function is complicated by
201 we_askshell(const char *words
, wordexp_t
*we
, int flags
)
203 int pdes
[2]; /* Pipe to child */
204 char bbuf
[9]; /* Buffer for byte count */
205 char wbuf
[9]; /* Buffer for word count */
206 long nwords
, nbytes
; /* Number of words, bytes from child */
207 long i
; /* Handy integer */
208 size_t sofs
; /* Offset into we->we_strings */
209 size_t vofs
; /* Offset into we->we_wordv */
210 pid_t pid
; /* Process ID of child */
211 pid_t wpid
; /* waitpid return value */
212 int status
; /* Child exit status */
213 int error
; /* Our return value */
214 int serrno
; /* errno to return */
215 char *np
, *p
; /* Handy pointers */
216 char *nstrings
; /* Temporary for realloc() */
218 int perr
[2]; /* Pipe to child error */
219 we_int_t
*nwv
; /* Temporary for realloc() */
221 posix_spawn_file_actions_t file_actions
;
222 posix_spawnattr_t attr
;
223 #else /* !__APPLE__ */
224 char **nwv
; /* Temporary for realloc() */
225 #endif /* !__APPLE__ */
226 sigset_t newsigblock
, oldsigblock
;
232 #else /* !__APPLE__ */
233 if (pipe2(pdes
, O_CLOEXEC
) < 0)
234 #endif /* !__APPLE__ */
235 return (WRDE_NOSPACE
); /* XXX */
237 if (pipe(perr
) < 0) {
240 return (WRDE_NOSPACE
); /* XXX */
242 #endif /* __APPLE__ */
243 (void)sigemptyset(&newsigblock
);
244 (void)sigaddset(&newsigblock
, SIGCHLD
);
245 (void)_sigprocmask(SIG_BLOCK
, &newsigblock
, &oldsigblock
);
247 if ((spawnerr
= posix_spawnattr_init(&attr
)) != 0) goto spawnerrexit
;
250 if ((spawnerr
= posix_spawnattr_setflags(&attr
, POSIX_SPAWN_CLOEXEC_DEFAULT
)) != 0) break;
251 (void)sigfillset(&spawnsig
);
252 if ((spawnerr
= posix_spawnattr_setsigdefault(&attr
, &spawnsig
)) != 0) break;
253 (void)sigemptyset(&spawnsig
);
254 if ((spawnerr
= posix_spawnattr_setsigmask(&attr
, &spawnsig
)) != 0) break;
255 if ((spawnerr
= posix_spawn_file_actions_init(&file_actions
)) != 0) break;
257 char *argv
[7] = {"sh"};
258 const char cmd
[] = "[ $# -gt 0 ] && export IFS=\"$1\";/usr/lib/system/wordexp-helper ";
263 if (pdes
[1] == STDOUT_FILENO
) {
264 if ((spawnerr
= posix_spawn_file_actions_addinherit_np(&file_actions
, STDOUT_FILENO
)) != 0) break;
266 if ((spawnerr
= posix_spawn_file_actions_adddup2(&file_actions
, pdes
[1], STDOUT_FILENO
)) != 0) break;
268 if (perr
[1] == STDERR_FILENO
) {
269 if ((spawnerr
= posix_spawn_file_actions_addinherit_np(&file_actions
, STDERR_FILENO
)) != 0) break;
271 if ((spawnerr
= posix_spawn_file_actions_adddup2(&file_actions
, perr
[1], STDERR_FILENO
)) != 0) break;
273 if (flags
& WRDE_UNDEF
) argv
[a
++] = "-u";
275 buflen
= (sizeof(cmd
) - 1) + strlen(words
) + 1;
276 if ((buf
= malloc(buflen
)) == NULL
) {
283 if ((IFS
= getenv("IFS")) != NULL
) {
288 spawnerr
= posix_spawn(&pid
, _PATH_BSHELL
, &file_actions
, &attr
, argv
, *_NSGetEnviron());
291 posix_spawn_file_actions_destroy(&file_actions
);
293 posix_spawnattr_destroy(&attr
);
301 return (WRDE_NOSPACE
); /* XXX */
303 #else /* !__APPLE__ */
304 if ((pid
= fork()) < 0) {
308 (void)_sigprocmask(SIG_SETMASK
, &oldsigblock
, NULL
);
310 return (WRDE_NOSPACE
); /* XXX */
314 * We are the child; just get /bin/sh to run the wordexp
315 * builtin on `words'.
317 (void)_sigprocmask(SIG_SETMASK
, &oldsigblock
, NULL
);
318 if ((pdes
[1] != STDOUT_FILENO
?
319 _dup2(pdes
[1], STDOUT_FILENO
) :
320 _fcntl(pdes
[1], F_SETFD
, 0)) < 0)
322 execl(_PATH_BSHELL
, "sh", flags
& WRDE_UNDEF
? "-u" : "+u",
323 "-c", "eval \"$1\";eval \"wordexp $2\"", "",
324 flags
& WRDE_SHOWERR
? "" : "exec 2>/dev/null", words
,
328 #endif /* !__APPLE__ */
331 * We are the parent; read the output of the shell wordexp function,
332 * which is a 32-bit hexadecimal word count, a 32-bit hexadecimal
333 * byte count (not including terminating null bytes), followed by
334 * the expanded words separated by nulls.
339 #endif /* __APPLE__ */
340 if (we_read_fully(pdes
[0], wbuf
, 8) != 8 ||
341 we_read_fully(pdes
[0], bbuf
, 8) != 8) {
342 error
= flags
& WRDE_UNDEF
? WRDE_BADVAL
: WRDE_SYNTAX
;
346 wbuf
[8] = bbuf
[8] = '\0';
347 nwords
= strtol(wbuf
, NULL
, 16);
348 nbytes
= strtol(bbuf
, NULL
, 16) + nwords
;
351 * Allocate or reallocate (when flags & WRDE_APPEND) the word vector
352 * and string storage buffers for the expanded words we're about to
353 * read from the child.
356 sofs
= we
->we_nbytes
;
357 #endif /* !__APPLE__ */
359 if ((flags
& (WRDE_DOOFFS
|WRDE_APPEND
)) == (WRDE_DOOFFS
|WRDE_APPEND
))
361 we
->we_wordc
+= nwords
;
363 we
->we_nbytes
+= nbytes
;
364 #endif /* !__APPLE__ */
366 if ((nwv
= realloc(we
->we_wordv
? WE_INT_T(we
->we_wordv
) : NULL
, (we
->we_wordc
+ 1 +
367 (flags
& WRDE_DOOFFS
? we
->we_offs
: 0)) *
368 sizeof(char *) + sizeof(we_int_t
))) == NULL
)
369 #else /* !__APPLE__ */
370 if ((nwv
= realloc(we
->we_wordv
, (we
->we_wordc
+ 1 +
371 (flags
& WRDE_DOOFFS
? we
->we_offs
: 0)) *
372 sizeof(char *))) == NULL
)
373 #endif /* !__APPLE__ */
375 error
= WRDE_NOSPACE
;
380 nwv
->wi_strings
= NULL
;
383 sofs
= nwv
->wi_nbytes
;
384 nwv
->wi_nbytes
+= nbytes
;
385 we
->we_wordv
= WE_WORDV(nwv
);
386 #else /* !__APPLE__ */
388 #endif /* !__APPLE__ */
390 if ((nstrings
= realloc(nwv
->wi_strings
, nwv
->wi_nbytes
)) == NULL
)
391 #else /* !__APPLE__ */
392 if ((nstrings
= realloc(we
->we_strings
, we
->we_nbytes
)) == NULL
)
393 #endif /* !__APPLE__ */
395 error
= WRDE_NOSPACE
;
398 for (i
= 0; i
< vofs
; i
++)
399 if (we
->we_wordv
[i
] != NULL
)
401 we
->we_wordv
[i
] += nstrings
- nwv
->wi_strings
;
402 #else /* !__APPLE__ */
403 we
->we_wordv
[i
] += nstrings
- we
->we_strings
;
404 #endif /* !__APPLE__ */
406 nwv
->wi_strings
= nstrings
;
407 #else /* !__APPLE__ */
408 we
->we_strings
= nstrings
;
409 #endif /* !__APPLE__ */
412 if (we_read_fully(pdes
[0], nwv
->wi_strings
+ sofs
, nbytes
) != nbytes
)
413 #else /* !__APPLE__ */
414 if (we_read_fully(pdes
[0], we
->we_strings
+ sofs
, nbytes
) != nbytes
)
415 #endif /* !__APPLE__ */
417 error
= flags
& WRDE_UNDEF
? WRDE_BADVAL
: WRDE_SYNTAX
;
423 ssize_t err_sz
= read(perr
[0], err_buf
, sizeof(err_buf
) - 1);
425 err_buf
[err_sz
] = '\0';
426 if (flags
& WRDE_SHOWERR
) {
427 fputs(err_buf
, stderr
);
429 } else if (err_sz
< 0) {
431 error
= WRDE_NOSPACE
;
434 #endif /* __APPLE__ */
441 #endif /* __APPLE__ */
443 wpid
= _waitpid(pid
, &status
, 0);
444 while (wpid
< 0 && errno
== EINTR
);
445 (void)_sigprocmask(SIG_SETMASK
, &oldsigblock
, NULL
);
450 if (wpid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
451 return (flags
& WRDE_UNDEF
? WRDE_BADVAL
: WRDE_SYNTAX
);
453 const char *cs
= strstr(err_buf
, command_substitution_str
);
454 if (cs
&& strstr(cs
+ (sizeof(command_substitution_str
) - 1), syntax_error_str
)) {
455 return (strstr(err_buf
, unbound_variable_str
) ? WRDE_BADVAL
: WRDE_SYNTAX
);
457 #endif /* __APPLE__ */
460 * Break the null-terminated expanded word strings out into
463 if (vofs
== 0 && flags
& WRDE_DOOFFS
)
464 while (vofs
< we
->we_offs
)
465 we
->we_wordv
[vofs
++] = NULL
;
467 p
= nwv
->wi_strings
+ sofs
;
468 #else /* !__APPLE__ */
469 p
= we
->we_strings
+ sofs
;
470 #endif /* !__APPLE__ */
471 while (nwords
-- != 0) {
472 we
->we_wordv
[vofs
++] = p
;
473 if ((np
= memchr(p
, '\0', nbytes
)) == NULL
)
474 return (WRDE_NOSPACE
); /* XXX */
475 nbytes
-= np
- p
+ 1;
478 we
->we_wordv
[vofs
] = NULL
;
485 * Check that the string contains none of the following unquoted
486 * special characters: <newline> |&;<>(){}
487 * or command substitutions when WRDE_NOCMD is set in flags.
490 we_check(const char *words
, int flags
)
493 int dquote
, level
, quote
, squote
;
495 quote
= squote
= dquote
= 0;
496 while ((c
= *words
++) != '\0') {
503 if (quote
+ dquote
== 0)
507 if (quote
+ squote
== 0)
511 if (quote
+ squote
== 0 && flags
& WRDE_NOCMD
)
512 return (WRDE_CMDSUB
);
513 while ((c
= *words
++) != '\0' && c
!= '`')
514 if (c
== '\\' && (c
= *words
++) == '\0')
517 return (WRDE_SYNTAX
);
519 case '|': case '&': case ';': case '<': case '>':
520 case '{': case '}': case '(': case ')': case '\n':
521 if (quote
+ squote
+ dquote
== 0)
522 return (WRDE_BADCHAR
);
525 if ((c
= *words
++) == '\0')
527 else if (quote
+ squote
== 0 && c
== '(') {
528 if (flags
& WRDE_NOCMD
&& *words
!= '(')
529 return (WRDE_CMDSUB
);
531 while ((c
= *words
++) != '\0') {
533 if ((c
= *words
++) == '\0')
537 else if (c
== ')' && --level
== 0)
540 if (c
== '\0' || level
!= 0)
541 return (WRDE_SYNTAX
);
542 } else if (quote
+ squote
== 0 && c
== '{') {
544 while ((c
= *words
++) != '\0') {
546 if ((c
= *words
++) == '\0')
550 else if (c
== '}' && --level
== 0)
553 if (c
== '\0' || level
!= 0)
554 return (WRDE_SYNTAX
);
563 if (quote
+ squote
+ dquote
!= 0)
564 return (WRDE_SYNTAX
);
571 * Free the result of wordexp(). See wordexp(3).
573 * Specified by IEEE Std. 1003.1-2001.
576 wordfree(wordexp_t
*we
)
583 free(WE_STRINGS(we
));
584 free(WE_INT_T(we
->we_wordv
));
586 #else /* !__APPLE__ */
588 #endif /* !__APPLE__ */
590 free(we
->we_strings
);
591 #endif /* !__APPLE__ */
594 we
->we_strings
= NULL
;
596 #endif /* !__APPLE__ */
600 #endif /* TARGET_OS_IPHONE */