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> */
32 #if defined(__OPEN_SOURCE__) || !__LP64__
34 int wordexp(const char *restrict words __unused
, wordexp_t
*restrict pwordexp __unused
, int flags __unused
) {
38 void wordfree(wordexp_t
*pwordexp __unused
) {
41 struct __not_an_empty_c_file
;
46 #include "namespace.h"
47 #include <sys/cdefs.h>
48 #include <sys/types.h>
59 #include "un-namespace.h"
61 // For _NSGetEnviron() -- which gives us a pointer to environ
62 #include <crt_externs.h>
64 #endif /* __APPLE__ */
66 __FBSDID("$FreeBSD$");
70 * To maintain backwards compatibility with wordexp_t, we can't put
71 * we_strings and we_nbytes in wordexp_t. So we create a new structure,
72 * we_int_t, that has wi_strings and wi_nbytes, as well as the we_wordv
81 * Normally, we_wordv will point to wi_wordv, so we need macros to convert
82 * back and forth between wi_wordv and the we_int_t structure.
84 #define WE_INT_T(x) ((we_int_t *)((char *)(x) - sizeof(we_int_t)))
85 #define WE_STRINGS(we) (WE_INT_T((we)->we_wordv)->wi_strings)
86 #define WE_WORDV(x) ((x)->wi_wordv)
89 * bash will return success, yet print a command substitution/syntax error
90 * to stderr. So we need to capture stderr, and see if it contains this error.
92 static const char command_substitution_str
[] = "command substitution";
93 static const char syntax_error_str
[] = "syntax error";
94 static const char unbound_variable_str
[] = " unbound variable";
95 #endif /* __APPLE__ */
97 static int we_askshell(const char *, wordexp_t
*, int);
98 static int we_check(const char *, int);
102 * Perform shell word expansion on `words' and place the resulting list
103 * of words in `we'. See wordexp(3).
105 * Specified by IEEE Std. 1003.1-2001.
109 wordexp(const char * __restrict words
, wordexp_t
* __restrict we0
, int flags
)
110 #else /* !__APPLE__ */
111 wordexp(const char * __restrict words
, wordexp_t
* __restrict we
, int flags
)
112 #endif /* !__APPLE__ */
117 wordexp_t
*we
= &temp
;
119 if ((error
= we_check(words
, flags
)) != 0) return (error
);
120 we
->we_offs
= we0
->we_offs
;
121 if (flags
& WRDE_APPEND
) {
123 size_t vofs
= we0
->we_wordc
+ (flags
& WRDE_DOOFFS
? we0
->we_offs
: 0);
124 we_int_t
*wi0
= WE_INT_T(we0
->we_wordv
);
125 we_int_t
*wi
= malloc((vofs
+ 1) * sizeof(char *) + sizeof(we_int_t
));
127 if (!wi
) return (WRDE_NOSPACE
);
128 memcpy(wi
, wi0
, (vofs
+ 1) * sizeof(char *) + sizeof(we_int_t
));
129 wi
->wi_strings
= malloc(wi
->wi_nbytes
);
130 if (!wi
->wi_strings
) {
132 return (WRDE_NOSPACE
);
134 memcpy(wi
->wi_strings
, wi0
->wi_strings
, wi
->wi_nbytes
);
135 for (i
= 0; i
< vofs
; i
++)
136 if (wi
->wi_wordv
[i
] != NULL
)
137 wi
->wi_wordv
[i
] += wi
->wi_strings
- wi0
->wi_strings
;
138 we
->we_wordc
= we0
->we_wordc
;
139 we
->we_wordv
= WE_WORDV(wi
);
144 #else /* !__APPLE__ */
145 if (flags
& WRDE_REUSE
)
147 if ((flags
& WRDE_APPEND
) == 0) {
150 we
->we_strings
= NULL
;
153 if ((error
= we_check(words
, flags
)) != 0) {
157 #endif /* !__APPLE__ */
158 if ((error
= we_askshell(words
, we
, flags
)) != 0) {
160 if (error
== WRDE_NOSPACE
) {
161 if (flags
& WRDE_REUSE
)
167 #else /* !__APPLE__ */
169 #endif /* !__APPLE__ */
173 if (flags
& WRDE_REUSE
)
176 #endif /* __APPLE__ */
181 we_read_fully(int fd
, char *buffer
, size_t len
)
188 nread
= _read(fd
, buffer
+ done
, len
- done
);
189 if (nread
== -1 && errno
== EINTR
)
194 } while (done
!= len
);
200 * Use the `wordexp' /bin/sh builtin function to do most of the work
201 * in expanding the word string. This function is complicated by
205 we_askshell(const char *words
, wordexp_t
*we
, int flags
)
207 int pdes
[2]; /* Pipe to child */
208 char bbuf
[9]; /* Buffer for byte count */
209 char wbuf
[9]; /* Buffer for word count */
210 long nwords
, nbytes
; /* Number of words, bytes from child */
211 long i
; /* Handy integer */
212 size_t sofs
; /* Offset into we->we_strings */
213 size_t vofs
; /* Offset into we->we_wordv */
214 pid_t pid
; /* Process ID of child */
215 pid_t wpid
; /* waitpid return value */
216 int status
; /* Child exit status */
217 int error
; /* Our return value */
218 int serrno
; /* errno to return */
219 char *np
, *p
; /* Handy pointers */
220 char *nstrings
; /* Temporary for realloc() */
222 int perr
[2]; /* Pipe to child error */
223 we_int_t
*nwv
; /* Temporary for realloc() */
225 posix_spawn_file_actions_t file_actions
;
226 posix_spawnattr_t attr
;
227 #else /* !__APPLE__ */
228 char **nwv
; /* Temporary for realloc() */
229 #endif /* !__APPLE__ */
230 sigset_t newsigblock
, oldsigblock
;
236 #else /* !__APPLE__ */
237 if (pipe2(pdes
, O_CLOEXEC
) < 0)
238 #endif /* !__APPLE__ */
239 return (WRDE_NOSPACE
); /* XXX */
241 if (pipe(perr
) < 0) {
244 return (WRDE_NOSPACE
); /* XXX */
246 #endif /* __APPLE__ */
247 (void)sigemptyset(&newsigblock
);
248 (void)sigaddset(&newsigblock
, SIGCHLD
);
249 (void)_sigprocmask(SIG_BLOCK
, &newsigblock
, &oldsigblock
);
251 if ((spawnerr
= posix_spawnattr_init(&attr
)) != 0) goto spawnerrexit
;
254 if ((spawnerr
= posix_spawnattr_setflags(&attr
, POSIX_SPAWN_CLOEXEC_DEFAULT
)) != 0) break;
255 (void)sigfillset(&spawnsig
);
256 if ((spawnerr
= posix_spawnattr_setsigdefault(&attr
, &spawnsig
)) != 0) break;
257 (void)sigemptyset(&spawnsig
);
258 if ((spawnerr
= posix_spawnattr_setsigmask(&attr
, &spawnsig
)) != 0) break;
259 if ((spawnerr
= posix_spawn_file_actions_init(&file_actions
)) != 0) break;
261 char *argv
[7] = {"sh"};
262 const char cmd
[] = "[ $# -gt 0 ] && export IFS=\"$1\";/usr/lib/system/wordexp-helper ";
267 if (pdes
[1] == STDOUT_FILENO
) {
268 if ((spawnerr
= posix_spawn_file_actions_addinherit_np(&file_actions
, STDOUT_FILENO
)) != 0) break;
270 if ((spawnerr
= posix_spawn_file_actions_adddup2(&file_actions
, pdes
[1], STDOUT_FILENO
)) != 0) break;
272 if (perr
[1] == STDERR_FILENO
) {
273 if ((spawnerr
= posix_spawn_file_actions_addinherit_np(&file_actions
, STDERR_FILENO
)) != 0) break;
275 if ((spawnerr
= posix_spawn_file_actions_adddup2(&file_actions
, perr
[1], STDERR_FILENO
)) != 0) break;
277 if (flags
& WRDE_UNDEF
) argv
[a
++] = "-u";
279 buflen
= (sizeof(cmd
) - 1) + strlen(words
) + 1;
280 if ((buf
= malloc(buflen
)) == NULL
) {
287 if ((IFS
= getenv("IFS")) != NULL
) {
292 spawnerr
= posix_spawn(&pid
, _PATH_BSHELL
, &file_actions
, &attr
, argv
, *_NSGetEnviron());
295 posix_spawn_file_actions_destroy(&file_actions
);
297 posix_spawnattr_destroy(&attr
);
305 return (WRDE_NOSPACE
); /* XXX */
307 #else /* !__APPLE__ */
308 if ((pid
= fork()) < 0) {
312 (void)_sigprocmask(SIG_SETMASK
, &oldsigblock
, NULL
);
314 return (WRDE_NOSPACE
); /* XXX */
318 * We are the child; just get /bin/sh to run the wordexp
319 * builtin on `words'.
321 (void)_sigprocmask(SIG_SETMASK
, &oldsigblock
, NULL
);
322 if ((pdes
[1] != STDOUT_FILENO
?
323 _dup2(pdes
[1], STDOUT_FILENO
) :
324 _fcntl(pdes
[1], F_SETFD
, 0)) < 0)
326 execl(_PATH_BSHELL
, "sh", flags
& WRDE_UNDEF
? "-u" : "+u",
327 "-c", "eval \"$1\";eval \"wordexp $2\"", "",
328 flags
& WRDE_SHOWERR
? "" : "exec 2>/dev/null", words
,
332 #endif /* !__APPLE__ */
335 * We are the parent; read the output of the shell wordexp function,
336 * which is a 32-bit hexadecimal word count, a 32-bit hexadecimal
337 * byte count (not including terminating null bytes), followed by
338 * the expanded words separated by nulls.
343 #endif /* __APPLE__ */
344 if (we_read_fully(pdes
[0], wbuf
, 8) != 8 ||
345 we_read_fully(pdes
[0], bbuf
, 8) != 8) {
346 error
= flags
& WRDE_UNDEF
? WRDE_BADVAL
: WRDE_SYNTAX
;
350 wbuf
[8] = bbuf
[8] = '\0';
351 nwords
= strtol(wbuf
, NULL
, 16);
352 nbytes
= strtol(bbuf
, NULL
, 16) + nwords
;
355 * Allocate or reallocate (when flags & WRDE_APPEND) the word vector
356 * and string storage buffers for the expanded words we're about to
357 * read from the child.
360 sofs
= we
->we_nbytes
;
361 #endif /* !__APPLE__ */
363 if ((flags
& (WRDE_DOOFFS
|WRDE_APPEND
)) == (WRDE_DOOFFS
|WRDE_APPEND
))
365 we
->we_wordc
+= nwords
;
367 we
->we_nbytes
+= nbytes
;
368 #endif /* !__APPLE__ */
370 if ((nwv
= realloc(we
->we_wordv
? WE_INT_T(we
->we_wordv
) : NULL
, (we
->we_wordc
+ 1 +
371 (flags
& WRDE_DOOFFS
? we
->we_offs
: 0)) *
372 sizeof(char *) + sizeof(we_int_t
))) == NULL
)
373 #else /* !__APPLE__ */
374 if ((nwv
= realloc(we
->we_wordv
, (we
->we_wordc
+ 1 +
375 (flags
& WRDE_DOOFFS
? we
->we_offs
: 0)) *
376 sizeof(char *))) == NULL
)
377 #endif /* !__APPLE__ */
379 error
= WRDE_NOSPACE
;
384 nwv
->wi_strings
= NULL
;
387 sofs
= nwv
->wi_nbytes
;
388 nwv
->wi_nbytes
+= nbytes
;
389 we
->we_wordv
= WE_WORDV(nwv
);
390 #else /* !__APPLE__ */
392 #endif /* !__APPLE__ */
394 if ((nstrings
= realloc(nwv
->wi_strings
, nwv
->wi_nbytes
)) == NULL
)
395 #else /* !__APPLE__ */
396 if ((nstrings
= realloc(we
->we_strings
, we
->we_nbytes
)) == NULL
)
397 #endif /* !__APPLE__ */
399 error
= WRDE_NOSPACE
;
402 for (i
= 0; i
< vofs
; i
++)
403 if (we
->we_wordv
[i
] != NULL
)
405 we
->we_wordv
[i
] += nstrings
- nwv
->wi_strings
;
406 #else /* !__APPLE__ */
407 we
->we_wordv
[i
] += nstrings
- we
->we_strings
;
408 #endif /* !__APPLE__ */
410 nwv
->wi_strings
= nstrings
;
411 #else /* !__APPLE__ */
412 we
->we_strings
= nstrings
;
413 #endif /* !__APPLE__ */
416 if (we_read_fully(pdes
[0], nwv
->wi_strings
+ sofs
, nbytes
) != nbytes
)
417 #else /* !__APPLE__ */
418 if (we_read_fully(pdes
[0], we
->we_strings
+ sofs
, nbytes
) != nbytes
)
419 #endif /* !__APPLE__ */
421 error
= flags
& WRDE_UNDEF
? WRDE_BADVAL
: WRDE_SYNTAX
;
427 ssize_t err_sz
= read(perr
[0], err_buf
, sizeof(err_buf
) - 1);
429 err_buf
[err_sz
] = '\0';
430 if (flags
& WRDE_SHOWERR
) {
431 fputs(err_buf
, stderr
);
433 } else if (err_sz
< 0) {
435 error
= WRDE_NOSPACE
;
438 #endif /* __APPLE__ */
445 #endif /* __APPLE__ */
447 wpid
= _waitpid(pid
, &status
, 0);
448 while (wpid
< 0 && errno
== EINTR
);
449 (void)_sigprocmask(SIG_SETMASK
, &oldsigblock
, NULL
);
454 if (wpid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
455 return (flags
& WRDE_UNDEF
? WRDE_BADVAL
: WRDE_SYNTAX
);
457 const char *cs
= strstr(err_buf
, command_substitution_str
);
458 if (cs
&& strstr(cs
+ (sizeof(command_substitution_str
) - 1), syntax_error_str
)) {
459 return (strstr(err_buf
, unbound_variable_str
) ? WRDE_BADVAL
: WRDE_SYNTAX
);
461 #endif /* __APPLE__ */
464 * Break the null-terminated expanded word strings out into
467 if (vofs
== 0 && flags
& WRDE_DOOFFS
)
468 while (vofs
< we
->we_offs
)
469 we
->we_wordv
[vofs
++] = NULL
;
471 p
= nwv
->wi_strings
+ sofs
;
472 #else /* !__APPLE__ */
473 p
= we
->we_strings
+ sofs
;
474 #endif /* !__APPLE__ */
475 while (nwords
-- != 0) {
476 we
->we_wordv
[vofs
++] = p
;
477 if ((np
= memchr(p
, '\0', nbytes
)) == NULL
)
478 return (WRDE_NOSPACE
); /* XXX */
479 nbytes
-= np
- p
+ 1;
482 we
->we_wordv
[vofs
] = NULL
;
489 * Check that the string contains none of the following unquoted
490 * special characters: <newline> |&;<>(){}
491 * or command substitutions when WRDE_NOCMD is set in flags.
494 we_check(const char *words
, int flags
)
497 int dquote
, level
, quote
, squote
;
499 quote
= squote
= dquote
= 0;
500 while ((c
= *words
++) != '\0') {
507 if (quote
+ dquote
== 0)
511 if (quote
+ squote
== 0)
515 if (quote
+ squote
== 0 && flags
& WRDE_NOCMD
)
516 return (WRDE_CMDSUB
);
517 while ((c
= *words
++) != '\0' && c
!= '`')
518 if (c
== '\\' && (c
= *words
++) == '\0')
521 return (WRDE_SYNTAX
);
523 case '|': case '&': case ';': case '<': case '>':
524 case '{': case '}': case '(': case ')': case '\n':
525 if (quote
+ squote
+ dquote
== 0)
526 return (WRDE_BADCHAR
);
529 if ((c
= *words
++) == '\0')
531 else if (quote
+ squote
== 0 && c
== '(') {
532 if (flags
& WRDE_NOCMD
&& *words
!= '(')
533 return (WRDE_CMDSUB
);
535 while ((c
= *words
++) != '\0') {
537 if ((c
= *words
++) == '\0')
541 else if (c
== ')' && --level
== 0)
544 if (c
== '\0' || level
!= 0)
545 return (WRDE_SYNTAX
);
546 } else if (quote
+ squote
== 0 && c
== '{') {
548 while ((c
= *words
++) != '\0') {
550 if ((c
= *words
++) == '\0')
554 else if (c
== '}' && --level
== 0)
557 if (c
== '\0' || level
!= 0)
558 return (WRDE_SYNTAX
);
567 if (quote
+ squote
+ dquote
!= 0)
568 return (WRDE_SYNTAX
);
575 * Free the result of wordexp(). See wordexp(3).
577 * Specified by IEEE Std. 1003.1-2001.
580 wordfree(wordexp_t
*we
)
587 free(WE_STRINGS(we
));
588 free(WE_INT_T(we
->we_wordv
));
590 #else /* !__APPLE__ */
592 #endif /* !__APPLE__ */
594 free(we
->we_strings
);
595 #endif /* !__APPLE__ */
598 we
->we_strings
= NULL
;
600 #endif /* !__APPLE__ */
604 #endif /* TARGET_OS_IPHONE */