1 /* $NetBSD: wordexp.c,v 1.3 2009/02/12 04:10:52 lukem Exp $ */
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
29 #include "namespace.h"
30 #include <sys/cdefs.h>
31 #include <sys/types.h>
42 #if defined(LIBC_SCCS) && !defined(lint)
44 __FBSDID("$FreeBSD: /repoman/r/ncvs/src/lib/libc/gen/wordexp.c,v 1.5 2004/04/09 11:32:32 tjr Exp $");
46 __RCSID("$NetBSD: wordexp.c,v 1.3 2009/02/12 04:10:52 lukem Exp $");
48 #endif /* LIBC_SCCS and not lint */
50 static int we_askshell(const char *, wordexp_t
*, int);
51 static int we_check(const char *, int);
55 * Perform shell word expansion on `words' and place the resulting list
56 * of words in `we'. See wordexp(3).
60 wordexp(const char * __restrict words
, wordexp_t
* __restrict we
, int flags
)
64 _DIAGASSERT(we
!= NULL
);
65 _DIAGASSERT(words
!= NULL
);
66 if (flags
& WRDE_REUSE
)
68 if ((flags
& WRDE_APPEND
) == 0) {
71 we
->we_strings
= NULL
;
74 if ((error
= we_check(words
, flags
)) != 0) {
78 if ((error
= we_askshell(words
, we
, flags
)) != 0) {
87 * Use the `wordexp' /bin/sh builtin function to do most of the work
88 * in expanding the word string. This function is complicated by
92 we_askshell(const char *words
, wordexp_t
*we
, int flags
)
94 int pdes
[2]; /* Pipe to child */
95 size_t nwords
, nbytes
; /* Number of words, bytes from child */
96 int i
; /* Handy integer */
97 unsigned int ui
; /* For array iteration */
98 size_t sofs
; /* Offset into we->we_strings */
99 size_t vofs
; /* Offset into we->we_wordv */
100 pid_t pid
; /* Process ID of child */
101 int status
; /* Child exit status */
102 const char *ifs
; /* IFS env. var. */
103 char *np
, *p
; /* Handy pointers */
104 char *nstrings
; /* Temporary for realloc() */
105 char **nwv
; /* Temporary for realloc() */
106 FILE *fp
; /* Stream to read pipe */
107 extern char **environ
;
110 if ((ifs
= getenv("IFS")) == NULL
)
112 if (asprintf(&cmd
, "wordexp%c%s\n", *ifs
, words
) < 0)
113 return (WRDE_NOSPACE
);
114 if (pipe(pdes
) < 0) {
118 if ((fp
= fdopen(pdes
[0], "r")) == NULL
) {
122 if ((pid
= fork()) < 0) {
130 * We are the child; just get /bin/sh to run the wordexp
131 * builtin on `words'.
136 if (pdes
[1] != STDOUT_FILENO
) {
137 if (dup2(pdes
[1], STDOUT_FILENO
) < 0)
141 if ((flags
& WRDE_SHOWERR
) == 0) {
142 if ((devnull
= open(_PATH_DEVNULL
, O_RDWR
, 0666)) < 0)
144 if (dup2(devnull
, STDERR_FILENO
) < 0)
148 execle(_PATH_BSHELL
, "sh", flags
& WRDE_UNDEF
? "-u" : "+u",
149 "-c", cmd
, (char *)NULL
, environ
);
154 * We are the parent; read the output of the shell wordexp function,
155 * which is a decimal word count, an null, a decimal byte count,
156 * (not including terminating null bytes), a null and then followed
157 * by the expanded words separated by nulls.
161 /* read the word count */
163 while ((i
= getc(fp
)) != EOF
) {
169 /* read the byte count */
171 while ((i
= getc(fp
)) != EOF
) {
179 waitpid(pid
, &status
, 0);
180 return (flags
& WRDE_UNDEF
? WRDE_BADVAL
: WRDE_SYNTAX
);
185 * Allocate or reallocate (when flags & WRDE_APPEND) the word vector
186 * and string storage buffers for the expanded words we're about to
187 * read from the child.
189 sofs
= we
->we_nbytes
;
191 if ((flags
& (WRDE_DOOFFS
|WRDE_APPEND
)) == (WRDE_DOOFFS
|WRDE_APPEND
))
193 we
->we_wordc
+= nwords
;
194 we
->we_nbytes
+= nbytes
;
195 if ((nwv
= realloc(we
->we_wordv
, (we
->we_wordc
+ 1 +
196 (flags
& WRDE_DOOFFS
? we
->we_offs
: 0)) *
197 sizeof(char *))) == NULL
) {
199 waitpid(pid
, &status
, 0);
200 return (WRDE_NOSPACE
);
203 if ((nstrings
= realloc(we
->we_strings
, we
->we_nbytes
)) == NULL
) {
205 waitpid(pid
, &status
, 0);
206 return (WRDE_NOSPACE
);
208 for (ui
= 0; ui
< vofs
; ui
++)
209 if (we
->we_wordv
[ui
] != NULL
)
210 we
->we_wordv
[ui
] += nstrings
- we
->we_strings
;
211 we
->we_strings
= nstrings
;
213 if (fread(we
->we_strings
+ sofs
, sizeof(char), nbytes
, fp
) != nbytes
) {
215 waitpid(pid
, &status
, 0);
216 return (flags
& WRDE_UNDEF
? WRDE_BADVAL
: WRDE_SYNTAX
);
219 if (waitpid(pid
, &status
, 0) < 0 || !WIFEXITED(status
) ||
220 WEXITSTATUS(status
) != 0) {
222 return (flags
& WRDE_UNDEF
? WRDE_BADVAL
: WRDE_SYNTAX
);
227 * Break the null-terminated expanded word strings out into
230 if (vofs
== 0 && flags
& WRDE_DOOFFS
)
231 while (vofs
< we
->we_offs
)
232 we
->we_wordv
[vofs
++] = NULL
;
233 p
= we
->we_strings
+ sofs
;
234 while (nwords
-- != 0) {
235 we
->we_wordv
[vofs
++] = p
;
236 if ((np
= memchr(p
, '\0', nbytes
)) == NULL
)
237 return (WRDE_NOSPACE
); /* XXX */
238 nbytes
-= np
- p
+ 1;
241 we
->we_wordv
[vofs
] = NULL
;
248 * Check that the string contains none of the following unquoted
249 * special characters: <newline> |&;<>(){}
250 * or command substitutions when WRDE_NOCMD is set in flags.
253 we_check(const char *words
, int flags
)
256 int dquote
, level
, quote
, squote
;
258 quote
= squote
= dquote
= 0;
259 while ((c
= *words
++) != '\0') {
265 if (quote
+ dquote
== 0)
269 if (quote
+ squote
== 0)
273 if (quote
+ squote
== 0 && flags
& WRDE_NOCMD
)
274 return (WRDE_CMDSUB
);
275 while ((c
= *words
++) != '\0' && c
!= '`')
276 if (c
== '\\' && (c
= *words
++) == '\0')
279 return (WRDE_SYNTAX
);
281 case '|': case '&': case ';': case '<': case '>':
282 case '{': case '}': case '(': case ')': case '\n':
283 if (quote
+ squote
+ dquote
== 0)
284 return (WRDE_BADCHAR
);
287 if ((c
= *words
++) == '\0')
289 else if (quote
+ squote
== 0 && c
== '(') {
290 if (flags
& WRDE_NOCMD
&& *words
!= '(')
291 return (WRDE_CMDSUB
);
293 while ((c
= *words
++) != '\0') {
295 if ((c
= *words
++) == '\0')
299 else if (c
== ')' && --level
== 0)
302 if (c
== '\0' || level
!= 0)
303 return (WRDE_SYNTAX
);
304 } else if (quote
+ squote
== 0 && c
== '{') {
306 while ((c
= *words
++) != '\0') {
308 if ((c
= *words
++) == '\0')
312 else if (c
== '}' && --level
== 0)
315 if (c
== '\0' || level
!= 0)
316 return (WRDE_SYNTAX
);
325 if (quote
+ squote
+ dquote
!= 0)
326 return (WRDE_SYNTAX
);
333 * Free the result of wordexp(). See wordexp(3).
337 wordfree(wordexp_t
*we
)
339 _DIAGASSERT(we
!= NULL
);
341 free(we
->we_strings
);
343 we
->we_strings
= NULL
;