SH: Convert fork.c to arch-fork.h
[glibc.git] / posix / wordexp-test.c
blob4957006da74b27e6cc4a146e1269f0d989288dcc
1 /* Copyright (C) 1997-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
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.
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
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <pwd.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <wordexp.h>
28 #define IFS " \n\t"
30 struct test_case_struct
32 int retval;
33 const char *env;
34 const char *words;
35 int flags;
36 size_t wordc;
37 const char *wordv[10];
38 const char *ifs;
39 } test_case[] =
41 /* Simple word- and field-splitting */
42 { 0, NULL, "one", 0, 1, { "one", }, IFS },
43 { 0, NULL, "one two", 0, 2, { "one", "two", }, IFS },
44 { 0, NULL, "one two three", 0, 3, { "one", "two", "three", }, IFS },
45 { 0, NULL, " \tfoo\t\tbar ", 0, 2, { "foo", "bar", }, IFS },
46 { 0, NULL, "red , white blue", 0, 4, { "red", ",", "white", "blue", }, " ," },
47 { 0, NULL, "one two three", 0, 3, { "one", "two", "three", }, "" },
48 { 0, NULL, "one \"two three\"", 0, 2, { "one", "two three", }, IFS },
49 { 0, NULL, "one \"two three\"", 0, 2, { "one", "two three", }, "" },
50 { 0, "two three", "one \"$var\"", 0, 2, { "one", "two three", }, IFS },
51 { 0, "two three", "one $var", 0, 3, { "one", "two", "three", }, IFS },
52 { 0, "two three", "one \"$var\"", 0, 2, { "one", "two three", }, "" },
53 { 0, "two three", "one $var", 0, 2, { "one", "two three", }, "" },
55 /* The non-whitespace IFS char at the end delimits the second field
56 * but does NOT start a new field. */
57 { 0, ":abc:", "$var", 0, 2, { "", "abc", }, ":" },
59 { 0, NULL, "$(echo :abc:)", 0, 2, { "", "abc", }, ":" },
60 { 0, NULL, "$(echo :abc:\\ )", 0, 2, { "", "abc", }, ": " },
61 { 0, NULL, "$(echo :abc\\ )", 0, 2, { "", "abc", }, ": " },
62 { 0, ":abc:", "$(echo $var)", 0, 2, { "", "abc", }, ":" },
63 { 0, NULL, ":abc:", 0, 1, { ":abc:", }, ":" },
64 { 0, NULL, "$(echo :abc:)def", 0, 3, { "", "abc", "def", },
65 ":" },
66 { 0, NULL, "$(echo abc:de)f", 0, 2, { "abc", "def", }, ":" },
67 { 0, NULL, "$(echo abc:de)f:ghi", 0, 2, { "abc", "def:ghi", },
68 ":" },
69 { 0, NULL, "abc:d$(echo ef:ghi)", 0, 2, { "abc:def", "ghi", },
70 ":" },
71 { 0, "abc:", "$var$(echo def:ghi)", 0, 3, { "abc", "def",
72 "ghi", }, ":" },
73 { 0, "abc:d", "$var$(echo ef:ghi)", 0, 3, { "abc", "def",
74 "ghi", }, ":" },
75 { 0, "def:ghi", "$(echo abc:)$var", 0, 3, { "abc", "def",
76 "ghi", }, ":" },
77 { 0, "ef:ghi", "$(echo abc:d)$var", 0, 3, { "abc", "def",
78 "ghi", }, ":" },
80 /* Simple parameter expansion */
81 { 0, "foo", "${var}", 0, 1, { "foo", }, IFS },
82 { 0, "foo", "$var", 0, 1, { "foo", }, IFS },
83 { 0, "foo", "\\\"$var\\\"", 0, 1, { "\"foo\"", }, IFS },
84 { 0, "foo", "%$var%", 0, 1, { "%foo%", }, IFS },
85 { 0, "foo", "-$var-", 0, 1, { "-foo-", }, IFS },
87 /* Simple quote removal */
88 { 0, NULL, "\"quoted\"", 0, 1, { "quoted", }, IFS },
89 { 0, "foo", "\"$var\"\"$var\"", 0, 1, { "foofoo", }, IFS },
90 { 0, NULL, "'singly-quoted'", 0, 1, { "singly-quoted", }, IFS },
91 { 0, NULL, "contin\\\nuation", 0, 1, { "continuation", }, IFS },
92 { 0, NULL, "explicit ''", 0, 2, { "explicit", "", }, IFS },
93 { 0, NULL, "explicit \"\"", 0, 2, { "explicit", "", }, IFS },
94 { 0, NULL, "explicit ``", 0, 1, { "explicit", }, IFS },
96 /* Simple command substitution */
97 { 0, NULL, "$(echo hello)", 0, 1, { "hello", }, IFS },
98 { 0, NULL, "$( (echo hello) )", 0, 1, { "hello", }, IFS },
99 { 0, NULL, "$((echo hello);(echo there))", 0, 2, { "hello", "there", }, IFS },
100 { 0, NULL, "`echo one two`", 0, 2, { "one", "two", }, IFS },
101 { 0, NULL, "$(echo ')')", 0, 1, { ")" }, IFS },
102 { 0, NULL, "$(echo hello; echo)", 0, 1, { "hello", }, IFS },
103 { 0, NULL, "a$(echo b)c", 0, 1, { "abc", }, IFS },
105 /* Simple arithmetic expansion */
106 { 0, NULL, "$((1 + 1))", 0, 1, { "2", }, IFS },
107 { 0, NULL, "$((2-3))", 0, 1, { "-1", }, IFS },
108 { 0, NULL, "$((-1))", 0, 1, { "-1", }, IFS },
109 { 0, NULL, "$[50+20]", 0, 1, { "70", }, IFS },
110 { 0, NULL, "$(((2+3)*(4+5)))", 0, 1, { "45", }, IFS },
111 { 0, NULL, "$((010))", 0, 1, { "8" }, IFS },
112 { 0, NULL, "$((0x10))", 0, 1, { "16" }, IFS },
113 { 0, NULL, "$((010+0x10))", 0, 1, { "24" }, IFS },
114 { 0, NULL, "$((-010+0x10))", 0, 1, { "8" }, IFS },
115 { 0, NULL, "$((-0x10+010))", 0, 1, { "-8" }, IFS },
117 /* Advanced parameter expansion */
118 { 0, NULL, "${var:-bar}", 0, 1, { "bar", }, IFS },
119 { 0, NULL, "${var-bar}", 0, 1, { "bar", }, IFS },
120 { 0, "", "${var:-bar}", 0, 1, { "bar", }, IFS },
121 { 0, "foo", "${var:-bar}", 0, 1, { "foo", }, IFS },
122 { 0, "", "${var-bar}", 0, 0, { NULL, }, IFS },
123 { 0, NULL, "${var:=bar}", 0, 1, { "bar", }, IFS },
124 { 0, NULL, "${var=bar}", 0, 1, { "bar", }, IFS },
125 { 0, "", "${var:=bar}", 0, 1, { "bar", }, IFS },
126 { 0, "foo", "${var:=bar}", 0, 1, { "foo", }, IFS },
127 { 0, "", "${var=bar}", 0, 0, { NULL, }, IFS },
128 { 0, "foo", "${var:?bar}", 0, 1, { "foo", }, IFS },
129 { 0, NULL, "${var:+bar}", 0, 0, { NULL, }, IFS },
130 { 0, NULL, "${var+bar}", 0, 0, { NULL, }, IFS },
131 { 0, "", "${var:+bar}", 0, 0, { NULL, }, IFS },
132 { 0, "foo", "${var:+bar}", 0, 1, { "bar", }, IFS },
133 { 0, "", "${var+bar}", 0, 1, { "bar", }, IFS },
134 { 0, "12345", "${#var}", 0, 1, { "5", }, IFS },
135 { 0, NULL, "${var:-'}'}", 0, 1, { "}", }, IFS },
136 { 0, NULL, "${var-}", 0, 0, { NULL }, IFS },
138 { 0, "pizza", "${var#${var}}", 0, 0, { NULL }, IFS },
139 { 0, "pepperoni", "${var%$(echo oni)}", 0, 1, { "pepper" }, IFS },
140 { 0, "6pack", "${var#$((6))}", 0, 1, { "pack" }, IFS },
141 { 0, "b*witched", "${var##b*}", 0, 0, { NULL }, IFS },
142 { 0, "b*witched", "${var##\"b*\"}", 0, 1, { "witched" }, IFS },
143 { 0, "banana", "${var%na*}", 0, 1, { "bana", }, IFS },
144 { 0, "banana", "${var%%na*}", 0, 1, { "ba", }, IFS },
145 { 0, "borabora-island", "${var#*bora}", 0, 1, { "bora-island", }, IFS },
146 { 0, "borabora-island", "${var##*bora}", 0, 1, { "-island", }, IFS },
147 { 0, "coconut", "${var##\\*co}", 0, 1, { "coconut", }, IFS },
148 { 0, "100%", "${var%0%}", 0, 1, { "10" }, IFS },
150 /* Pathname expansion */
151 { 0, NULL, "???", 0, 2, { "one", "two", }, IFS },
152 { 0, NULL, "[ot]??", 0, 2, { "one", "two", }, IFS },
153 { 0, NULL, "t*", 0, 2, { "three", "two", }, IFS },
154 { 0, NULL, "\"t\"*", 0, 2, { "three", "two", }, IFS },
156 /* Nested constructs */
157 { 0, "one two", "$var", 0, 2, { "one", "two", }, IFS },
158 { 0, "one two three", "$var", 0, 3, { "one", "two", "three", }, IFS },
159 { 0, " \tfoo\t\tbar ", "$var", 0, 2, { "foo", "bar", }, IFS },
160 { 0, " red , white blue", "$var", 0, 3, { "red", "white", "blue", }, ", \n\t" },
161 { 0, " red , white blue", "\"$var\"", 0, 1, { " red , white blue", }, ", \n\t" },
162 { 0, NULL, "\"$(echo hello there)\"", 0, 1, { "hello there", }, IFS },
163 { 0, NULL, "\"$(echo \"hello there\")\"", 0, 1, { "hello there", }, IFS },
164 { 0, NULL, "${var=one two} \"$var\"", 0, 3, { "one", "two", "one two", }, IFS },
165 { 0, "1", "$(( $(echo 3)+$var ))", 0, 1, { "4", }, IFS },
166 { 0, NULL, "\"$(echo \"*\")\"", 0, 1, { "*", }, IFS },
167 { 0, NULL, "\"a\n\n$(echo)b\"", 0, 1, { "a\n\nb", }, IFS },
168 { 0, "foo", "*$var*", 0, 1, { "*foo*", }, IFS },
169 { 0, "o thr", "*$var*", 0, 2, { "two", "three" }, IFS },
171 /* Different IFS values */
172 { 0, "a b\tc\nd ", "$var", 0, 4, { "a", "b", "c", "d" }, NULL /* unset */ },
173 { 0, "a b\tc d ", "$var", 0, 1, { "a b\tc d " }, "" /* `null' */ },
174 { 0, "a,b c\n, d", "$var", 0, 3, { "a", "b c", " d" }, "\t\n," },
176 /* Other things that should succeed */
177 { 0, NULL, "\\*\"|&;<>\"\\(\\)\\{\\}", 0, 1, { "*|&;<>(){}", }, IFS },
178 { 0, "???", "$var", 0, 1, { "???", }, IFS },
179 { 0, NULL, "$var", 0, 0, { NULL, }, IFS },
180 { 0, NULL, "\"\\n\"", 0, 1, { "\\n", }, IFS },
181 { 0, NULL, "", 0, 0, { NULL, }, IFS },
183 /* Flags not already covered (testit() has special handling for these) */
184 { 0, NULL, "one two", WRDE_DOOFFS, 2, { "one", "two", }, IFS },
185 { 0, NULL, "appended", WRDE_APPEND, 3, { "pre1", "pre2", "appended", }, IFS },
186 { 0, NULL, "appended", WRDE_DOOFFS|WRDE_APPEND, 3, { "pre1", "pre2", "appended", }, IFS },
188 /* Things that should fail */
189 { WRDE_BADCHAR, NULL, "new\nline", 0, 0, { NULL, }, "" /* \n not IFS */ },
190 { WRDE_BADCHAR, NULL, "pipe|symbol", 0, 0, { NULL, }, IFS },
191 { WRDE_BADCHAR, NULL, "&ampersand", 0, 0, { NULL, }, IFS },
192 { WRDE_BADCHAR, NULL, "semi;colon", 0, 0, { NULL, }, IFS },
193 { WRDE_BADCHAR, NULL, "<greater", 0, 0, { NULL, }, IFS },
194 { WRDE_BADCHAR, NULL, "less>", 0, 0, { NULL, }, IFS },
195 { WRDE_BADCHAR, NULL, "(open-paren", 0, 0, { NULL, }, IFS },
196 { WRDE_BADCHAR, NULL, "close-paren)", 0, 0, { NULL, }, IFS },
197 { WRDE_BADCHAR, NULL, "{open-brace", 0, 0, { NULL, }, IFS },
198 { WRDE_BADCHAR, NULL, "close-brace}", 0, 0, { NULL, }, IFS },
199 { WRDE_CMDSUB, NULL, "$(ls)", WRDE_NOCMD, 0, { NULL, }, IFS },
200 { WRDE_BADVAL, NULL, "$var", WRDE_UNDEF, 0, { NULL, }, IFS },
201 { WRDE_BADVAL, NULL, "$9", WRDE_UNDEF, 0, { NULL, }, IFS },
202 { WRDE_SYNTAX, NULL, "$[50+20))", 0, 0, { NULL, }, IFS },
203 { WRDE_SYNTAX, NULL, "${%%noparam}", 0, 0, { NULL, }, IFS },
204 { WRDE_SYNTAX, NULL, "${missing-brace", 0, 0, { NULL, }, IFS },
205 { WRDE_SYNTAX, NULL, "$(for i in)", 0, 0, { NULL, }, IFS },
206 { WRDE_SYNTAX, NULL, "$((2+))", 0, 0, { NULL, }, IFS },
207 { WRDE_SYNTAX, NULL, "`", 0, 0, { NULL, }, IFS },
208 { WRDE_SYNTAX, NULL, "$((010+4+))", 0, 0, { NULL }, IFS },
210 { -1, NULL, NULL, 0, 0, { NULL, }, IFS },
213 static int testit (struct test_case_struct *tc);
214 static int tests;
216 static void
217 command_line_test (const char *words)
219 wordexp_t we;
220 int i;
221 int retval = wordexp (words, &we, 0);
222 printf ("wordexp returned %d\n", retval);
223 for (i = 0; i < we.we_wordc; i++)
224 printf ("we_wordv[%d] = \"%s\"\n", i, we.we_wordv[i]);
228 main (int argc, char *argv[])
230 const char *globfile[] = { "one", "two", "three", NULL };
231 char tmpdir[32];
232 struct passwd *pw;
233 const char *cwd;
234 int test;
235 int fail = 0;
236 int i;
237 struct test_case_struct ts;
239 if (argc > 1)
241 command_line_test (argv[1]);
242 return 0;
245 cwd = getcwd (NULL, 0);
247 /* Set up arena for pathname expansion */
248 tmpnam (tmpdir);
249 if (mkdir (tmpdir, S_IRWXU) || chdir (tmpdir))
250 return -1;
251 else
253 int fd;
255 for (i = 0; globfile[i]; ++i)
256 if ((fd = creat (globfile[i], S_IRUSR | S_IWUSR)) == -1
257 || close (fd))
258 return -1;
261 for (test = 0; test_case[test].retval != -1; test++)
262 if (testit (&test_case[test]))
263 ++fail;
265 /* Tilde-expansion tests. */
266 pw = getpwnam ("root");
267 if (pw != NULL)
269 ts.retval = 0;
270 ts.env = NULL;
271 ts.words = "~root ";
272 ts.flags = 0;
273 ts.wordc = 1;
274 ts.wordv[0] = pw->pw_dir;
275 ts.ifs = IFS;
277 if (testit (&ts))
278 ++fail;
280 ts.retval = 0;
281 ts.env = pw->pw_dir;
282 ts.words = "${var#~root}x";
283 ts.flags = 0;
284 ts.wordc = 1;
285 ts.wordv[0] = "x";
286 ts.ifs = IFS;
288 if (testit (&ts))
289 ++fail;
292 /* "~" expands to value of $HOME when HOME is set */
294 setenv ("HOME", "/dummy/home", 1);
295 ts.retval = 0;
296 ts.env = NULL;
297 ts.words = "~ ~/foo";
298 ts.flags = 0;
299 ts.wordc = 2;
300 ts.wordv[0] = "/dummy/home";
301 ts.wordv[1] = "/dummy/home/foo";
302 ts.ifs = IFS;
304 if (testit (&ts))
305 ++fail;
307 /* "~" expands to home dir from passwd file if HOME is not set */
309 pw = getpwuid (getuid ());
310 if (pw != NULL)
312 unsetenv ("HOME");
313 ts.retval = 0;
314 ts.env = NULL;
315 ts.words = "~";
316 ts.flags = 0;
317 ts.wordc = 1;
318 ts.wordv[0] = pw->pw_dir;
319 ts.ifs = IFS;
321 if (testit (&ts))
322 ++fail;
325 puts ("tests completed, now cleaning up");
327 /* Clean up */
328 for (i = 0; globfile[i]; ++i)
329 remove (globfile[i]);
331 if (cwd == NULL)
332 cwd = "..";
334 chdir (cwd);
335 rmdir (tmpdir);
337 printf ("tests failed: %d\n", fail);
339 return fail != 0;
343 static int
344 testit (struct test_case_struct *tc)
346 int retval;
347 wordexp_t we, sav_we;
348 char *dummy;
349 int bzzzt = 0;
350 int start_offs = 0;
351 int i;
353 if (tc->env)
354 setenv ("var", tc->env, 1);
355 else
356 unsetenv ("var");
358 if (tc->ifs)
359 setenv ("IFS", tc->ifs, 1);
360 else
361 unsetenv ("IFS");
363 sav_we.we_wordc = 99;
364 sav_we.we_wordv = &dummy;
365 sav_we.we_offs = 3;
366 we = sav_we;
368 printf ("Test %d (%s): ", ++tests, tc->words);
370 if (tc->flags & WRDE_APPEND)
372 /* initial wordexp() call, to be appended to */
373 if (wordexp ("pre1 pre2", &we, tc->flags & ~WRDE_APPEND) != 0)
375 printf ("FAILED setup\n");
376 return 1;
379 retval = wordexp (tc->words, &we, tc->flags);
381 if (tc->flags & WRDE_DOOFFS)
382 start_offs = sav_we.we_offs;
384 if (retval != tc->retval || (retval == 0 && we.we_wordc != tc->wordc))
385 bzzzt = 1;
386 else if (retval == 0)
388 for (i = 0; i < start_offs; ++i)
389 if (we.we_wordv[i] != NULL)
391 bzzzt = 1;
392 break;
395 for (i = 0; i < we.we_wordc; ++i)
396 if (we.we_wordv[i+start_offs] == NULL ||
397 strcmp (tc->wordv[i], we.we_wordv[i+start_offs]) != 0)
399 bzzzt = 1;
400 break;
404 if (bzzzt)
406 printf ("FAILED\n");
407 printf ("Test words: <%s>, need retval %d, wordc %Zd\n",
408 tc->words, tc->retval, tc->wordc);
409 if (start_offs != 0)
410 printf ("(preceded by %d NULLs)\n", start_offs);
411 printf ("Got retval %d, wordc %Zd: ", retval, we.we_wordc);
412 if (retval == 0 || retval == WRDE_NOSPACE)
414 for (i = 0; i < we.we_wordc + start_offs; ++i)
415 if (we.we_wordv[i] == NULL)
416 printf ("NULL ");
417 else
418 printf ("<%s> ", we.we_wordv[i]);
420 printf ("\n");
422 else if (retval != 0 && retval != WRDE_NOSPACE &&
423 (we.we_wordc != sav_we.we_wordc ||
424 we.we_wordv != sav_we.we_wordv ||
425 we.we_offs != sav_we.we_offs))
427 bzzzt = 1;
428 printf ("FAILED to restore wordexp_t members\n");
430 else
431 printf ("OK\n");
433 if (retval == 0 || retval == WRDE_NOSPACE)
434 wordfree (&we);
436 return bzzzt;