]>
Commit | Line | Data |
---|---|---|
b168057a | 1 | /* Copyright (C) 1997-2015 Free Software Foundation, Inc. |
b5efde2f UD |
2 | This file is part of the GNU C Library. |
3 | ||
4 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
b5efde2f UD |
8 | |
9 | The GNU C Library is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
b5efde2f | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
b5efde2f | 17 | |
8d9618b7 UD |
18 | #include <sys/stat.h> |
19 | #include <sys/types.h> | |
20 | #include <fcntl.h> | |
21 | #include <unistd.h> | |
ac16e905 | 22 | #include <pwd.h> |
b5efde2f UD |
23 | #include <stdio.h> |
24 | #include <stdlib.h> | |
3e1f480e | 25 | #include <string.h> |
ac16e905 | 26 | #include <wordexp.h> |
b5efde2f | 27 | |
6ca96fe2 | 28 | #define IFS " \n\t" |
3db52d94 | 29 | |
a39208bd CD |
30 | extern void *__dso_handle __attribute__ ((__weak__, __visibility__ ("hidden"))); |
31 | extern int __register_atfork (void (*) (void), void (*) (void), void (*) (void), void *); | |
32 | ||
33 | static int __app_register_atfork (void (*prepare) (void), void (*parent) (void), void (*child) (void)) | |
34 | { | |
35 | return __register_atfork (prepare, parent, child, | |
36 | &__dso_handle == NULL ? NULL : __dso_handle); | |
37 | } | |
38 | ||
39 | /* Number of forks seen. */ | |
40 | static int registered_forks; | |
41 | ||
42 | /* For each fork increment the fork count. */ | |
43 | static void | |
44 | register_fork (void) | |
45 | { | |
46 | registered_forks++; | |
47 | } | |
48 | ||
b5efde2f UD |
49 | struct test_case_struct |
50 | { | |
51 | int retval; | |
52 | const char *env; | |
53 | const char *words; | |
54 | int flags; | |
00b16c4a | 55 | size_t wordc; |
b5efde2f | 56 | const char *wordv[10]; |
6ca96fe2 | 57 | const char *ifs; |
b5efde2f UD |
58 | } test_case[] = |
59 | { | |
c06cc21c | 60 | /* Simple word- and field-splitting */ |
6ca96fe2 UD |
61 | { 0, NULL, "one", 0, 1, { "one", }, IFS }, |
62 | { 0, NULL, "one two", 0, 2, { "one", "two", }, IFS }, | |
63 | { 0, NULL, "one two three", 0, 3, { "one", "two", "three", }, IFS }, | |
64 | { 0, NULL, " \tfoo\t\tbar ", 0, 2, { "foo", "bar", }, IFS }, | |
a8125d85 | 65 | { 0, NULL, "red , white blue", 0, 4, { "red", ",", "white", "blue", }, " ," }, |
c06cc21c UD |
66 | { 0, NULL, "one two three", 0, 3, { "one", "two", "three", }, "" }, |
67 | { 0, NULL, "one \"two three\"", 0, 2, { "one", "two three", }, IFS }, | |
68 | { 0, NULL, "one \"two three\"", 0, 2, { "one", "two three", }, "" }, | |
69 | { 0, "two three", "one \"$var\"", 0, 2, { "one", "two three", }, IFS }, | |
70 | { 0, "two three", "one $var", 0, 3, { "one", "two", "three", }, IFS }, | |
71 | { 0, "two three", "one \"$var\"", 0, 2, { "one", "two three", }, "" }, | |
72 | { 0, "two three", "one $var", 0, 2, { "one", "two three", }, "" }, | |
1ffaaca2 UD |
73 | |
74 | /* The non-whitespace IFS char at the end delimits the second field | |
75 | * but does NOT start a new field. */ | |
76 | { 0, ":abc:", "$var", 0, 2, { "", "abc", }, ":" }, | |
77 | ||
4708015f UD |
78 | { 0, NULL, "$(echo :abc:)", 0, 2, { "", "abc", }, ":" }, |
79 | { 0, NULL, "$(echo :abc:\\ )", 0, 2, { "", "abc", }, ": " }, | |
80 | { 0, NULL, "$(echo :abc\\ )", 0, 2, { "", "abc", }, ": " }, | |
81 | { 0, ":abc:", "$(echo $var)", 0, 2, { "", "abc", }, ":" }, | |
a8125d85 | 82 | { 0, NULL, ":abc:", 0, 1, { ":abc:", }, ":" }, |
4708015f | 83 | { 0, NULL, "$(echo :abc:)def", 0, 3, { "", "abc", "def", }, |
b2900a13 | 84 | ":" }, |
4708015f UD |
85 | { 0, NULL, "$(echo abc:de)f", 0, 2, { "abc", "def", }, ":" }, |
86 | { 0, NULL, "$(echo abc:de)f:ghi", 0, 2, { "abc", "def:ghi", }, | |
b2900a13 | 87 | ":" }, |
4708015f | 88 | { 0, NULL, "abc:d$(echo ef:ghi)", 0, 2, { "abc:def", "ghi", }, |
b2900a13 | 89 | ":" }, |
4708015f | 90 | { 0, "abc:", "$var$(echo def:ghi)", 0, 3, { "abc", "def", |
b2900a13 | 91 | "ghi", }, ":" }, |
4708015f | 92 | { 0, "abc:d", "$var$(echo ef:ghi)", 0, 3, { "abc", "def", |
b2900a13 | 93 | "ghi", }, ":" }, |
4708015f | 94 | { 0, "def:ghi", "$(echo abc:)$var", 0, 3, { "abc", "def", |
b2900a13 | 95 | "ghi", }, ":" }, |
4708015f | 96 | { 0, "ef:ghi", "$(echo abc:d)$var", 0, 3, { "abc", "def", |
b2900a13 | 97 | "ghi", }, ":" }, |
3db52d94 UD |
98 | |
99 | /* Simple parameter expansion */ | |
6ca96fe2 UD |
100 | { 0, "foo", "${var}", 0, 1, { "foo", }, IFS }, |
101 | { 0, "foo", "$var", 0, 1, { "foo", }, IFS }, | |
102 | { 0, "foo", "\\\"$var\\\"", 0, 1, { "\"foo\"", }, IFS }, | |
103 | { 0, "foo", "%$var%", 0, 1, { "%foo%", }, IFS }, | |
104 | { 0, "foo", "-$var-", 0, 1, { "-foo-", }, IFS }, | |
3db52d94 UD |
105 | |
106 | /* Simple quote removal */ | |
6ca96fe2 UD |
107 | { 0, NULL, "\"quoted\"", 0, 1, { "quoted", }, IFS }, |
108 | { 0, "foo", "\"$var\"\"$var\"", 0, 1, { "foofoo", }, IFS }, | |
109 | { 0, NULL, "'singly-quoted'", 0, 1, { "singly-quoted", }, IFS }, | |
bac660f8 | 110 | { 0, NULL, "contin\\\nuation", 0, 1, { "continuation", }, IFS }, |
a9c27b3e UD |
111 | { 0, NULL, "explicit ''", 0, 2, { "explicit", "", }, IFS }, |
112 | { 0, NULL, "explicit \"\"", 0, 2, { "explicit", "", }, IFS }, | |
113 | { 0, NULL, "explicit ``", 0, 1, { "explicit", }, IFS }, | |
3db52d94 UD |
114 | |
115 | /* Simple command substitution */ | |
6ca96fe2 UD |
116 | { 0, NULL, "$(echo hello)", 0, 1, { "hello", }, IFS }, |
117 | { 0, NULL, "$( (echo hello) )", 0, 1, { "hello", }, IFS }, | |
118 | { 0, NULL, "$((echo hello);(echo there))", 0, 2, { "hello", "there", }, IFS }, | |
119 | { 0, NULL, "`echo one two`", 0, 2, { "one", "two", }, IFS }, | |
120 | { 0, NULL, "$(echo ')')", 0, 1, { ")" }, IFS }, | |
121 | { 0, NULL, "$(echo hello; echo)", 0, 1, { "hello", }, IFS }, | |
52aec7d1 | 122 | { 0, NULL, "a$(echo b)c", 0, 1, { "abc", }, IFS }, |
3db52d94 UD |
123 | |
124 | /* Simple arithmetic expansion */ | |
6ca96fe2 UD |
125 | { 0, NULL, "$((1 + 1))", 0, 1, { "2", }, IFS }, |
126 | { 0, NULL, "$((2-3))", 0, 1, { "-1", }, IFS }, | |
127 | { 0, NULL, "$((-1))", 0, 1, { "-1", }, IFS }, | |
128 | { 0, NULL, "$[50+20]", 0, 1, { "70", }, IFS }, | |
129 | { 0, NULL, "$(((2+3)*(4+5)))", 0, 1, { "45", }, IFS }, | |
bcc86889 UD |
130 | { 0, NULL, "$((010))", 0, 1, { "8" }, IFS }, |
131 | { 0, NULL, "$((0x10))", 0, 1, { "16" }, IFS }, | |
132 | { 0, NULL, "$((010+0x10))", 0, 1, { "24" }, IFS }, | |
133 | { 0, NULL, "$((-010+0x10))", 0, 1, { "8" }, IFS }, | |
134 | { 0, NULL, "$((-0x10+010))", 0, 1, { "-8" }, IFS }, | |
3db52d94 UD |
135 | |
136 | /* Advanced parameter expansion */ | |
6ca96fe2 UD |
137 | { 0, NULL, "${var:-bar}", 0, 1, { "bar", }, IFS }, |
138 | { 0, NULL, "${var-bar}", 0, 1, { "bar", }, IFS }, | |
139 | { 0, "", "${var:-bar}", 0, 1, { "bar", }, IFS }, | |
140 | { 0, "foo", "${var:-bar}", 0, 1, { "foo", }, IFS }, | |
141 | { 0, "", "${var-bar}", 0, 0, { NULL, }, IFS }, | |
142 | { 0, NULL, "${var:=bar}", 0, 1, { "bar", }, IFS }, | |
143 | { 0, NULL, "${var=bar}", 0, 1, { "bar", }, IFS }, | |
144 | { 0, "", "${var:=bar}", 0, 1, { "bar", }, IFS }, | |
145 | { 0, "foo", "${var:=bar}", 0, 1, { "foo", }, IFS }, | |
146 | { 0, "", "${var=bar}", 0, 0, { NULL, }, IFS }, | |
147 | { 0, "foo", "${var:?bar}", 0, 1, { "foo", }, IFS }, | |
148 | { 0, NULL, "${var:+bar}", 0, 0, { NULL, }, IFS }, | |
149 | { 0, NULL, "${var+bar}", 0, 0, { NULL, }, IFS }, | |
150 | { 0, "", "${var:+bar}", 0, 0, { NULL, }, IFS }, | |
151 | { 0, "foo", "${var:+bar}", 0, 1, { "bar", }, IFS }, | |
152 | { 0, "", "${var+bar}", 0, 1, { "bar", }, IFS }, | |
153 | { 0, "12345", "${#var}", 0, 1, { "5", }, IFS }, | |
154 | { 0, NULL, "${var:-'}'}", 0, 1, { "}", }, IFS }, | |
155 | { 0, NULL, "${var-}", 0, 0, { NULL }, IFS }, | |
156 | ||
bac660f8 UD |
157 | { 0, "pizza", "${var#${var}}", 0, 0, { NULL }, IFS }, |
158 | { 0, "pepperoni", "${var%$(echo oni)}", 0, 1, { "pepper" }, IFS }, | |
159 | { 0, "6pack", "${var#$((6))}", 0, 1, { "pack" }, IFS }, | |
160 | { 0, "b*witched", "${var##b*}", 0, 0, { NULL }, IFS }, | |
161 | { 0, "b*witched", "${var##\"b*\"}", 0, 1, { "witched" }, IFS }, | |
6ca96fe2 UD |
162 | { 0, "banana", "${var%na*}", 0, 1, { "bana", }, IFS }, |
163 | { 0, "banana", "${var%%na*}", 0, 1, { "ba", }, IFS }, | |
164 | { 0, "borabora-island", "${var#*bora}", 0, 1, { "bora-island", }, IFS }, | |
bac660f8 UD |
165 | { 0, "borabora-island", "${var##*bora}", 0, 1, { "-island", }, IFS }, |
166 | { 0, "coconut", "${var##\\*co}", 0, 1, { "coconut", }, IFS }, | |
6ca96fe2 | 167 | { 0, "100%", "${var%0%}", 0, 1, { "10" }, IFS }, |
3db52d94 | 168 | |
8d9618b7 | 169 | /* Pathname expansion */ |
6ca96fe2 UD |
170 | { 0, NULL, "???", 0, 2, { "one", "two", }, IFS }, |
171 | { 0, NULL, "[ot]??", 0, 2, { "one", "two", }, IFS }, | |
172 | { 0, NULL, "t*", 0, 2, { "three", "two", }, IFS }, | |
173 | { 0, NULL, "\"t\"*", 0, 2, { "three", "two", }, IFS }, | |
8d9618b7 UD |
174 | |
175 | /* Nested constructs */ | |
6ca96fe2 UD |
176 | { 0, "one two", "$var", 0, 2, { "one", "two", }, IFS }, |
177 | { 0, "one two three", "$var", 0, 3, { "one", "two", "three", }, IFS }, | |
178 | { 0, " \tfoo\t\tbar ", "$var", 0, 2, { "foo", "bar", }, IFS }, | |
179 | { 0, " red , white blue", "$var", 0, 3, { "red", "white", "blue", }, ", \n\t" }, | |
180 | { 0, " red , white blue", "\"$var\"", 0, 1, { " red , white blue", }, ", \n\t" }, | |
181 | { 0, NULL, "\"$(echo hello there)\"", 0, 1, { "hello there", }, IFS }, | |
182 | { 0, NULL, "\"$(echo \"hello there\")\"", 0, 1, { "hello there", }, IFS }, | |
183 | { 0, NULL, "${var=one two} \"$var\"", 0, 3, { "one", "two", "one two", }, IFS }, | |
184 | { 0, "1", "$(( $(echo 3)+$var ))", 0, 1, { "4", }, IFS }, | |
185 | { 0, NULL, "\"$(echo \"*\")\"", 0, 1, { "*", }, IFS }, | |
52aec7d1 | 186 | { 0, NULL, "\"a\n\n$(echo)b\"", 0, 1, { "a\n\nb", }, IFS }, |
6ca96fe2 UD |
187 | { 0, "foo", "*$var*", 0, 1, { "*foo*", }, IFS }, |
188 | { 0, "o thr", "*$var*", 0, 2, { "two", "three" }, IFS }, | |
8d9618b7 | 189 | |
6796bc80 | 190 | /* Different IFS values */ |
c06cc21c UD |
191 | { 0, "a b\tc\nd ", "$var", 0, 4, { "a", "b", "c", "d" }, NULL /* unset */ }, |
192 | { 0, "a b\tc d ", "$var", 0, 1, { "a b\tc d " }, "" /* `null' */ }, | |
193 | { 0, "a,b c\n, d", "$var", 0, 3, { "a", "b c", " d" }, "\t\n," }, | |
6796bc80 | 194 | |
8d9618b7 | 195 | /* Other things that should succeed */ |
6ca96fe2 UD |
196 | { 0, NULL, "\\*\"|&;<>\"\\(\\)\\{\\}", 0, 1, { "*|&;<>(){}", }, IFS }, |
197 | { 0, "???", "$var", 0, 1, { "???", }, IFS }, | |
198 | { 0, NULL, "$var", 0, 0, { NULL, }, IFS }, | |
199 | { 0, NULL, "\"\\n\"", 0, 1, { "\\n", }, IFS }, | |
200 | { 0, NULL, "", 0, 0, { NULL, }, IFS }, | |
8d9618b7 | 201 | |
52aec7d1 UD |
202 | /* Flags not already covered (testit() has special handling for these) */ |
203 | { 0, NULL, "one two", WRDE_DOOFFS, 2, { "one", "two", }, IFS }, | |
204 | { 0, NULL, "appended", WRDE_APPEND, 3, { "pre1", "pre2", "appended", }, IFS }, | |
205 | { 0, NULL, "appended", WRDE_DOOFFS|WRDE_APPEND, 3, { "pre1", "pre2", "appended", }, IFS }, | |
206 | ||
8d9618b7 | 207 | /* Things that should fail */ |
6796bc80 | 208 | { WRDE_BADCHAR, NULL, "new\nline", 0, 0, { NULL, }, "" /* \n not IFS */ }, |
6ca96fe2 UD |
209 | { WRDE_BADCHAR, NULL, "pipe|symbol", 0, 0, { NULL, }, IFS }, |
210 | { WRDE_BADCHAR, NULL, "&ersand", 0, 0, { NULL, }, IFS }, | |
211 | { WRDE_BADCHAR, NULL, "semi;colon", 0, 0, { NULL, }, IFS }, | |
212 | { WRDE_BADCHAR, NULL, "<greater", 0, 0, { NULL, }, IFS }, | |
213 | { WRDE_BADCHAR, NULL, "less>", 0, 0, { NULL, }, IFS }, | |
214 | { WRDE_BADCHAR, NULL, "(open-paren", 0, 0, { NULL, }, IFS }, | |
215 | { WRDE_BADCHAR, NULL, "close-paren)", 0, 0, { NULL, }, IFS }, | |
216 | { WRDE_BADCHAR, NULL, "{open-brace", 0, 0, { NULL, }, IFS }, | |
217 | { WRDE_BADCHAR, NULL, "close-brace}", 0, 0, { NULL, }, IFS }, | |
218 | { WRDE_CMDSUB, NULL, "$(ls)", WRDE_NOCMD, 0, { NULL, }, IFS }, | |
219 | { WRDE_BADVAL, NULL, "$var", WRDE_UNDEF, 0, { NULL, }, IFS }, | |
220 | { WRDE_BADVAL, NULL, "$9", WRDE_UNDEF, 0, { NULL, }, IFS }, | |
221 | { WRDE_SYNTAX, NULL, "$[50+20))", 0, 0, { NULL, }, IFS }, | |
222 | { WRDE_SYNTAX, NULL, "${%%noparam}", 0, 0, { NULL, }, IFS }, | |
223 | { WRDE_SYNTAX, NULL, "${missing-brace", 0, 0, { NULL, }, IFS }, | |
52aec7d1 | 224 | { WRDE_SYNTAX, NULL, "$(for i in)", 0, 0, { NULL, }, IFS }, |
6ca96fe2 UD |
225 | { WRDE_SYNTAX, NULL, "$((2+))", 0, 0, { NULL, }, IFS }, |
226 | { WRDE_SYNTAX, NULL, "`", 0, 0, { NULL, }, IFS }, | |
bcc86889 | 227 | { WRDE_SYNTAX, NULL, "$((010+4+))", 0, 0, { NULL }, IFS }, |
a39208bd CD |
228 | /* Test for CVE-2014-7817. We test 3 combinations of command |
229 | substitution inside an arithmetic expression to make sure that | |
230 | no commands are executed and error is returned. */ | |
231 | { WRDE_CMDSUB, NULL, "$((`echo 1`))", WRDE_NOCMD, 0, { NULL, }, IFS }, | |
232 | { WRDE_CMDSUB, NULL, "$((1+`echo 1`))", WRDE_NOCMD, 0, { NULL, }, IFS }, | |
233 | { WRDE_CMDSUB, NULL, "$((1+$((`echo 1`))))", WRDE_NOCMD, 0, { NULL, }, IFS }, | |
6ca96fe2 UD |
234 | |
235 | { -1, NULL, NULL, 0, 0, { NULL, }, IFS }, | |
b5efde2f UD |
236 | }; |
237 | ||
ac16e905 | 238 | static int testit (struct test_case_struct *tc); |
8619129f | 239 | static int tests; |
ac16e905 | 240 | |
de149cdb | 241 | static void |
8193034b UD |
242 | command_line_test (const char *words) |
243 | { | |
244 | wordexp_t we; | |
245 | int i; | |
246 | int retval = wordexp (words, &we, 0); | |
247 | printf ("wordexp returned %d\n", retval); | |
248 | for (i = 0; i < we.we_wordc; i++) | |
249 | printf ("we_wordv[%d] = \"%s\"\n", i, we.we_wordv[i]); | |
250 | } | |
251 | ||
b5efde2f | 252 | int |
8193034b | 253 | main (int argc, char *argv[]) |
b5efde2f | 254 | { |
67600288 | 255 | const char *globfile[] = { "one", "two", "three", NULL }; |
8d9618b7 | 256 | char tmpdir[32]; |
ac16e905 | 257 | struct passwd *pw; |
31161268 | 258 | const char *cwd; |
b5efde2f UD |
259 | int test; |
260 | int fail = 0; | |
67600288 | 261 | int i; |
52aec7d1 | 262 | struct test_case_struct ts; |
b5efde2f | 263 | |
8193034b UD |
264 | if (argc > 1) |
265 | { | |
266 | command_line_test (argv[1]); | |
267 | return 0; | |
268 | } | |
269 | ||
67600288 | 270 | cwd = getcwd (NULL, 0); |
8d9618b7 UD |
271 | |
272 | /* Set up arena for pathname expansion */ | |
273 | tmpnam (tmpdir); | |
67600288 | 274 | if (mkdir (tmpdir, S_IRWXU) || chdir (tmpdir)) |
8d9618b7 | 275 | return -1; |
67600288 UD |
276 | else |
277 | { | |
278 | int fd; | |
279 | ||
280 | for (i = 0; globfile[i]; ++i) | |
281 | if ((fd = creat (globfile[i], S_IRUSR | S_IWUSR)) == -1 | |
282 | || close (fd)) | |
283 | return -1; | |
284 | } | |
8d9618b7 | 285 | |
a39208bd CD |
286 | /* If we are not allowed to do command substitution, we install |
287 | fork handlers to verify that no forks happened. No forks should | |
288 | happen at all if command substitution is disabled. */ | |
289 | if (__app_register_atfork (register_fork, NULL, NULL) != 0) | |
290 | { | |
291 | printf ("Failed to register fork handler.\n"); | |
292 | return -1; | |
293 | } | |
294 | ||
2bcf29ba UD |
295 | for (test = 0; test_case[test].retval != -1; test++) |
296 | if (testit (&test_case[test])) | |
297 | ++fail; | |
298 | ||
a9c27b3e | 299 | /* Tilde-expansion tests. */ |
ac16e905 UD |
300 | pw = getpwnam ("root"); |
301 | if (pw != NULL) | |
b5efde2f | 302 | { |
52aec7d1 UD |
303 | ts.retval = 0; |
304 | ts.env = NULL; | |
305 | ts.words = "~root "; | |
306 | ts.flags = 0; | |
307 | ts.wordc = 1; | |
308 | ts.wordv[0] = pw->pw_dir; | |
309 | ts.ifs = IFS; | |
310 | ||
91eecefd UD |
311 | if (testit (&ts)) |
312 | ++fail; | |
313 | ||
314 | ts.retval = 0; | |
315 | ts.env = pw->pw_dir; | |
316 | ts.words = "${var#~root}x"; | |
317 | ts.flags = 0; | |
318 | ts.wordc = 1; | |
319 | ts.wordv[0] = "x"; | |
320 | ts.ifs = IFS; | |
321 | ||
52aec7d1 UD |
322 | if (testit (&ts)) |
323 | ++fail; | |
324 | } | |
325 | ||
326 | /* "~" expands to value of $HOME when HOME is set */ | |
327 | ||
328 | setenv ("HOME", "/dummy/home", 1); | |
329 | ts.retval = 0; | |
330 | ts.env = NULL; | |
331 | ts.words = "~ ~/foo"; | |
332 | ts.flags = 0; | |
333 | ts.wordc = 2; | |
334 | ts.wordv[0] = "/dummy/home"; | |
335 | ts.wordv[1] = "/dummy/home/foo"; | |
336 | ts.ifs = IFS; | |
337 | ||
338 | if (testit (&ts)) | |
339 | ++fail; | |
ac16e905 | 340 | |
52aec7d1 UD |
341 | /* "~" expands to home dir from passwd file if HOME is not set */ |
342 | ||
343 | pw = getpwuid (getuid ()); | |
344 | if (pw != NULL) | |
345 | { | |
346 | unsetenv ("HOME"); | |
ac16e905 UD |
347 | ts.retval = 0; |
348 | ts.env = NULL; | |
52aec7d1 | 349 | ts.words = "~"; |
ac16e905 UD |
350 | ts.flags = 0; |
351 | ts.wordc = 1; | |
352 | ts.wordv[0] = pw->pw_dir; | |
8619129f | 353 | ts.ifs = IFS; |
ac16e905 UD |
354 | |
355 | if (testit (&ts)) | |
b17277cf | 356 | ++fail; |
ac16e905 UD |
357 | } |
358 | ||
8619129f UD |
359 | puts ("tests completed, now cleaning up"); |
360 | ||
67600288 UD |
361 | /* Clean up */ |
362 | for (i = 0; globfile[i]; ++i) | |
363 | remove (globfile[i]); | |
364 | ||
22bc7978 UD |
365 | if (cwd == NULL) |
366 | cwd = ".."; | |
67600288 UD |
367 | |
368 | chdir (cwd); | |
369 | rmdir (tmpdir); | |
370 | ||
8619129f UD |
371 | printf ("tests failed: %d\n", fail); |
372 | ||
ac16e905 UD |
373 | return fail != 0; |
374 | } | |
375 | ||
376 | ||
377 | static int | |
378 | testit (struct test_case_struct *tc) | |
379 | { | |
ac16e905 | 380 | int retval; |
52aec7d1 UD |
381 | wordexp_t we, sav_we; |
382 | char *dummy; | |
ac16e905 | 383 | int bzzzt = 0; |
52aec7d1 | 384 | int start_offs = 0; |
ac16e905 UD |
385 | int i; |
386 | ||
387 | if (tc->env) | |
388 | setenv ("var", tc->env, 1); | |
389 | else | |
390 | unsetenv ("var"); | |
391 | ||
6ca96fe2 UD |
392 | if (tc->ifs) |
393 | setenv ("IFS", tc->ifs, 1); | |
394 | else | |
395 | unsetenv ("IFS"); | |
396 | ||
52aec7d1 UD |
397 | sav_we.we_wordc = 99; |
398 | sav_we.we_wordv = &dummy; | |
399 | sav_we.we_offs = 3; | |
400 | we = sav_we; | |
401 | ||
8619129f | 402 | printf ("Test %d (%s): ", ++tests, tc->words); |
52aec7d1 | 403 | |
a39208bd CD |
404 | if (tc->flags & WRDE_NOCMD) |
405 | registered_forks = 0; | |
406 | ||
52aec7d1 UD |
407 | if (tc->flags & WRDE_APPEND) |
408 | { | |
409 | /* initial wordexp() call, to be appended to */ | |
410 | if (wordexp ("pre1 pre2", &we, tc->flags & ~WRDE_APPEND) != 0) | |
411 | { | |
412 | printf ("FAILED setup\n"); | |
413 | return 1; | |
414 | } | |
415 | } | |
ac16e905 UD |
416 | retval = wordexp (tc->words, &we, tc->flags); |
417 | ||
a39208bd CD |
418 | if ((tc->flags & WRDE_NOCMD) |
419 | && (registered_forks > 0)) | |
420 | { | |
421 | printf ("FAILED fork called for WRDE_NOCMD\n"); | |
422 | return 1; | |
423 | } | |
424 | ||
52aec7d1 UD |
425 | if (tc->flags & WRDE_DOOFFS) |
426 | start_offs = sav_we.we_offs; | |
427 | ||
22bc7978 | 428 | if (retval != tc->retval || (retval == 0 && we.we_wordc != tc->wordc)) |
ac16e905 | 429 | bzzzt = 1; |
52aec7d1 UD |
430 | else if (retval == 0) |
431 | { | |
432 | for (i = 0; i < start_offs; ++i) | |
433 | if (we.we_wordv[i] != NULL) | |
434 | { | |
435 | bzzzt = 1; | |
436 | break; | |
437 | } | |
438 | ||
439 | for (i = 0; i < we.we_wordc; ++i) | |
440 | if (we.we_wordv[i+start_offs] == NULL || | |
441 | strcmp (tc->wordv[i], we.we_wordv[i+start_offs]) != 0) | |
442 | { | |
443 | bzzzt = 1; | |
444 | break; | |
445 | } | |
446 | } | |
b5efde2f | 447 | |
ac16e905 UD |
448 | if (bzzzt) |
449 | { | |
450 | printf ("FAILED\n"); | |
00b16c4a | 451 | printf ("Test words: <%s>, need retval %d, wordc %Zd\n", |
ac16e905 | 452 | tc->words, tc->retval, tc->wordc); |
52aec7d1 UD |
453 | if (start_offs != 0) |
454 | printf ("(preceded by %d NULLs)\n", start_offs); | |
00b16c4a | 455 | printf ("Got retval %d, wordc %Zd: ", retval, we.we_wordc); |
52aec7d1 UD |
456 | if (retval == 0 || retval == WRDE_NOSPACE) |
457 | { | |
458 | for (i = 0; i < we.we_wordc + start_offs; ++i) | |
459 | if (we.we_wordv[i] == NULL) | |
460 | printf ("NULL "); | |
461 | else | |
462 | printf ("<%s> ", we.we_wordv[i]); | |
463 | } | |
ac16e905 | 464 | printf ("\n"); |
b5efde2f | 465 | } |
52aec7d1 UD |
466 | else if (retval != 0 && retval != WRDE_NOSPACE && |
467 | (we.we_wordc != sav_we.we_wordc || | |
468 | we.we_wordv != sav_we.we_wordv || | |
469 | we.we_offs != sav_we.we_offs)) | |
470 | { | |
471 | bzzzt = 1; | |
472 | printf ("FAILED to restore wordexp_t members\n"); | |
473 | } | |
ac16e905 UD |
474 | else |
475 | printf ("OK\n"); | |
476 | ||
a8125d85 UD |
477 | if (retval == 0 || retval == WRDE_NOSPACE) |
478 | wordfree (&we); | |
b5efde2f | 479 | |
ac16e905 | 480 | return bzzzt; |
b5efde2f | 481 | } |