Support inline syscall with six arguments.
[glibc/pb-stable.git] / posix / wordexp-test.c
blobaca95777a87aca67f0e5c02eb63184e4d76ae8b7
1 /* Copyright (C) 1997, 1998, 2000, 2001 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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <pwd.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <wordexp.h>
29 #define IFS " \n\t"
31 struct test_case_struct
33 int retval;
34 const char *env;
35 const char *words;
36 int flags;
37 size_t wordc;
38 const char *wordv[10];
39 const char *ifs;
40 } test_case[] =
42 /* Simple word- and field-splitting */
43 { 0, NULL, "one", 0, 1, { "one", }, IFS },
44 { 0, NULL, "one two", 0, 2, { "one", "two", }, IFS },
45 { 0, NULL, "one two three", 0, 3, { "one", "two", "three", }, IFS },
46 { 0, NULL, " \tfoo\t\tbar ", 0, 2, { "foo", "bar", }, IFS },
47 { 0, NULL, "red , white blue", 0, 4, { "red", ",", "white", "blue", }, " ," },
48 { 0, NULL, "one two three", 0, 3, { "one", "two", "three", }, "" },
49 { 0, NULL, "one \"two three\"", 0, 2, { "one", "two three", }, IFS },
50 { 0, NULL, "one \"two three\"", 0, 2, { "one", "two three", }, "" },
51 { 0, "two three", "one \"$var\"", 0, 2, { "one", "two three", }, IFS },
52 { 0, "two three", "one $var", 0, 3, { "one", "two", "three", }, IFS },
53 { 0, "two three", "one \"$var\"", 0, 2, { "one", "two three", }, "" },
54 { 0, "two three", "one $var", 0, 2, { "one", "two three", }, "" },
56 /* The non-whitespace IFS char at the end delimits the second field
57 * but does NOT start a new field. */
58 { 0, ":abc:", "$var", 0, 2, { "", "abc", }, ":" },
60 { 0, NULL, "$(echo :abc:)", 0, 2, { "", "abc", }, ":" },
61 { 0, NULL, "$(echo :abc:\\ )", 0, 2, { "", "abc", }, ": " },
62 { 0, NULL, "$(echo :abc\\ )", 0, 2, { "", "abc", }, ": " },
63 { 0, ":abc:", "$(echo $var)", 0, 2, { "", "abc", }, ":" },
64 { 0, NULL, ":abc:", 0, 1, { ":abc:", }, ":" },
65 { 0, NULL, "$(echo :abc:)def", 0, 3, { "", "abc", "def", },
66 ":" },
67 { 0, NULL, "$(echo abc:de)f", 0, 2, { "abc", "def", }, ":" },
68 { 0, NULL, "$(echo abc:de)f:ghi", 0, 2, { "abc", "def:ghi", },
69 ":" },
70 { 0, NULL, "abc:d$(echo ef:ghi)", 0, 2, { "abc:def", "ghi", },
71 ":" },
72 { 0, "abc:", "$var$(echo def:ghi)", 0, 3, { "abc", "def",
73 "ghi", }, ":" },
74 { 0, "abc:d", "$var$(echo ef:ghi)", 0, 3, { "abc", "def",
75 "ghi", }, ":" },
76 { 0, "def:ghi", "$(echo abc:)$var", 0, 3, { "abc", "def",
77 "ghi", }, ":" },
78 { 0, "ef:ghi", "$(echo abc:d)$var", 0, 3, { "abc", "def",
79 "ghi", }, ":" },
81 /* Simple parameter expansion */
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 },
86 { 0, "foo", "-$var-", 0, 1, { "-foo-", }, IFS },
88 /* Simple quote removal */
89 { 0, NULL, "\"quoted\"", 0, 1, { "quoted", }, IFS },
90 { 0, "foo", "\"$var\"\"$var\"", 0, 1, { "foofoo", }, IFS },
91 { 0, NULL, "'singly-quoted'", 0, 1, { "singly-quoted", }, IFS },
92 { 0, NULL, "contin\\\nuation", 0, 1, { "continuation", }, IFS },
93 { 0, NULL, "explicit ''", 0, 2, { "explicit", "", }, IFS },
94 { 0, NULL, "explicit \"\"", 0, 2, { "explicit", "", }, IFS },
95 { 0, NULL, "explicit ``", 0, 1, { "explicit", }, IFS },
97 /* Simple command substitution */
98 { 0, NULL, "$(echo hello)", 0, 1, { "hello", }, IFS },
99 { 0, NULL, "$( (echo hello) )", 0, 1, { "hello", }, IFS },
100 { 0, NULL, "$((echo hello);(echo there))", 0, 2, { "hello", "there", }, IFS },
101 { 0, NULL, "`echo one two`", 0, 2, { "one", "two", }, IFS },
102 { 0, NULL, "$(echo ')')", 0, 1, { ")" }, IFS },
103 { 0, NULL, "$(echo hello; echo)", 0, 1, { "hello", }, IFS },
104 { 0, NULL, "a$(echo b)c", 0, 1, { "abc", }, IFS },
106 /* Simple arithmetic expansion */
107 { 0, NULL, "$((1 + 1))", 0, 1, { "2", }, IFS },
108 { 0, NULL, "$((2-3))", 0, 1, { "-1", }, IFS },
109 { 0, NULL, "$((-1))", 0, 1, { "-1", }, IFS },
110 { 0, NULL, "$[50+20]", 0, 1, { "70", }, IFS },
111 { 0, NULL, "$(((2+3)*(4+5)))", 0, 1, { "45", }, IFS },
113 /* Advanced parameter expansion */
114 { 0, NULL, "${var:-bar}", 0, 1, { "bar", }, IFS },
115 { 0, NULL, "${var-bar}", 0, 1, { "bar", }, IFS },
116 { 0, "", "${var:-bar}", 0, 1, { "bar", }, IFS },
117 { 0, "foo", "${var:-bar}", 0, 1, { "foo", }, IFS },
118 { 0, "", "${var-bar}", 0, 0, { NULL, }, IFS },
119 { 0, NULL, "${var:=bar}", 0, 1, { "bar", }, IFS },
120 { 0, NULL, "${var=bar}", 0, 1, { "bar", }, IFS },
121 { 0, "", "${var:=bar}", 0, 1, { "bar", }, IFS },
122 { 0, "foo", "${var:=bar}", 0, 1, { "foo", }, IFS },
123 { 0, "", "${var=bar}", 0, 0, { NULL, }, IFS },
124 { 0, "foo", "${var:?bar}", 0, 1, { "foo", }, IFS },
125 { 0, NULL, "${var:+bar}", 0, 0, { NULL, }, IFS },
126 { 0, NULL, "${var+bar}", 0, 0, { NULL, }, IFS },
127 { 0, "", "${var:+bar}", 0, 0, { NULL, }, IFS },
128 { 0, "foo", "${var:+bar}", 0, 1, { "bar", }, IFS },
129 { 0, "", "${var+bar}", 0, 1, { "bar", }, IFS },
130 { 0, "12345", "${#var}", 0, 1, { "5", }, IFS },
131 { 0, NULL, "${var:-'}'}", 0, 1, { "}", }, IFS },
132 { 0, NULL, "${var-}", 0, 0, { NULL }, IFS },
134 { 0, "pizza", "${var#${var}}", 0, 0, { NULL }, IFS },
135 { 0, "pepperoni", "${var%$(echo oni)}", 0, 1, { "pepper" }, IFS },
136 { 0, "6pack", "${var#$((6))}", 0, 1, { "pack" }, IFS },
137 { 0, "b*witched", "${var##b*}", 0, 0, { NULL }, IFS },
138 { 0, "b*witched", "${var##\"b*\"}", 0, 1, { "witched" }, IFS },
139 { 0, "banana", "${var%na*}", 0, 1, { "bana", }, IFS },
140 { 0, "banana", "${var%%na*}", 0, 1, { "ba", }, IFS },
141 { 0, "borabora-island", "${var#*bora}", 0, 1, { "bora-island", }, IFS },
142 { 0, "borabora-island", "${var##*bora}", 0, 1, { "-island", }, IFS },
143 { 0, "coconut", "${var##\\*co}", 0, 1, { "coconut", }, IFS },
144 { 0, "100%", "${var%0%}", 0, 1, { "10" }, IFS },
146 /* Pathname expansion */
147 { 0, NULL, "???", 0, 2, { "one", "two", }, IFS },
148 { 0, NULL, "[ot]??", 0, 2, { "one", "two", }, IFS },
149 { 0, NULL, "t*", 0, 2, { "three", "two", }, IFS },
150 { 0, NULL, "\"t\"*", 0, 2, { "three", "two", }, IFS },
152 /* Nested constructs */
153 { 0, "one two", "$var", 0, 2, { "one", "two", }, IFS },
154 { 0, "one two three", "$var", 0, 3, { "one", "two", "three", }, IFS },
155 { 0, " \tfoo\t\tbar ", "$var", 0, 2, { "foo", "bar", }, IFS },
156 { 0, " red , white blue", "$var", 0, 3, { "red", "white", "blue", }, ", \n\t" },
157 { 0, " red , white blue", "\"$var\"", 0, 1, { " red , white blue", }, ", \n\t" },
158 { 0, NULL, "\"$(echo hello there)\"", 0, 1, { "hello there", }, IFS },
159 { 0, NULL, "\"$(echo \"hello there\")\"", 0, 1, { "hello there", }, IFS },
160 { 0, NULL, "${var=one two} \"$var\"", 0, 3, { "one", "two", "one two", }, IFS },
161 { 0, "1", "$(( $(echo 3)+$var ))", 0, 1, { "4", }, IFS },
162 { 0, NULL, "\"$(echo \"*\")\"", 0, 1, { "*", }, IFS },
163 { 0, NULL, "\"a\n\n$(echo)b\"", 0, 1, { "a\n\nb", }, IFS },
164 { 0, "foo", "*$var*", 0, 1, { "*foo*", }, IFS },
165 { 0, "o thr", "*$var*", 0, 2, { "two", "three" }, IFS },
167 /* Different IFS values */
168 { 0, "a b\tc\nd ", "$var", 0, 4, { "a", "b", "c", "d" }, NULL /* unset */ },
169 { 0, "a b\tc d ", "$var", 0, 1, { "a b\tc d " }, "" /* `null' */ },
170 { 0, "a,b c\n, d", "$var", 0, 3, { "a", "b c", " d" }, "\t\n," },
172 /* Other things that should succeed */
173 { 0, NULL, "\\*\"|&;<>\"\\(\\)\\{\\}", 0, 1, { "*|&;<>(){}", }, IFS },
174 { 0, "???", "$var", 0, 1, { "???", }, IFS },
175 { 0, NULL, "$var", 0, 0, { NULL, }, IFS },
176 { 0, NULL, "\"\\n\"", 0, 1, { "\\n", }, IFS },
177 { 0, NULL, "", 0, 0, { NULL, }, IFS },
179 /* Flags not already covered (testit() has special handling for these) */
180 { 0, NULL, "one two", WRDE_DOOFFS, 2, { "one", "two", }, IFS },
181 { 0, NULL, "appended", WRDE_APPEND, 3, { "pre1", "pre2", "appended", }, IFS },
182 { 0, NULL, "appended", WRDE_DOOFFS|WRDE_APPEND, 3, { "pre1", "pre2", "appended", }, IFS },
184 /* Things that should fail */
185 { WRDE_BADCHAR, NULL, "new\nline", 0, 0, { NULL, }, "" /* \n not IFS */ },
186 { WRDE_BADCHAR, NULL, "pipe|symbol", 0, 0, { NULL, }, IFS },
187 { WRDE_BADCHAR, NULL, "&ampersand", 0, 0, { NULL, }, IFS },
188 { WRDE_BADCHAR, NULL, "semi;colon", 0, 0, { NULL, }, IFS },
189 { WRDE_BADCHAR, NULL, "<greater", 0, 0, { NULL, }, IFS },
190 { WRDE_BADCHAR, NULL, "less>", 0, 0, { NULL, }, IFS },
191 { WRDE_BADCHAR, NULL, "(open-paren", 0, 0, { NULL, }, IFS },
192 { WRDE_BADCHAR, NULL, "close-paren)", 0, 0, { NULL, }, IFS },
193 { WRDE_BADCHAR, NULL, "{open-brace", 0, 0, { NULL, }, IFS },
194 { WRDE_BADCHAR, NULL, "close-brace}", 0, 0, { NULL, }, IFS },
195 { WRDE_CMDSUB, NULL, "$(ls)", WRDE_NOCMD, 0, { NULL, }, IFS },
196 { WRDE_BADVAL, NULL, "$var", WRDE_UNDEF, 0, { NULL, }, IFS },
197 { WRDE_BADVAL, NULL, "$9", WRDE_UNDEF, 0, { NULL, }, IFS },
198 { WRDE_SYNTAX, NULL, "$[50+20))", 0, 0, { NULL, }, IFS },
199 { WRDE_SYNTAX, NULL, "${%%noparam}", 0, 0, { NULL, }, IFS },
200 { WRDE_SYNTAX, NULL, "${missing-brace", 0, 0, { NULL, }, IFS },
201 { WRDE_SYNTAX, NULL, "$(for i in)", 0, 0, { NULL, }, IFS },
202 { WRDE_SYNTAX, NULL, "$((2+))", 0, 0, { NULL, }, IFS },
203 { WRDE_SYNTAX, NULL, "`", 0, 0, { NULL, }, IFS },
205 { -1, NULL, NULL, 0, 0, { NULL, }, IFS },
208 static int testit (struct test_case_struct *tc);
209 static int tests;
211 static void
212 command_line_test (const char *words)
214 wordexp_t we;
215 int i;
216 int retval = wordexp (words, &we, 0);
217 printf ("wordexp returned %d\n", retval);
218 for (i = 0; i < we.we_wordc; i++)
219 printf ("we_wordv[%d] = \"%s\"\n", i, we.we_wordv[i]);
223 main (int argc, char *argv[])
225 const char *globfile[] = { "one", "two", "three", NULL };
226 char tmpdir[32];
227 struct passwd *pw;
228 const char *cwd;
229 int test;
230 int fail = 0;
231 int i;
232 struct test_case_struct ts;
234 if (argc > 1)
236 command_line_test (argv[1]);
237 return 0;
240 cwd = getcwd (NULL, 0);
242 /* Set up arena for pathname expansion */
243 tmpnam (tmpdir);
244 if (mkdir (tmpdir, S_IRWXU) || chdir (tmpdir))
245 return -1;
246 else
248 int fd;
250 for (i = 0; globfile[i]; ++i)
251 if ((fd = creat (globfile[i], S_IRUSR | S_IWUSR)) == -1
252 || close (fd))
253 return -1;
256 for (test = 0; test_case[test].retval != -1; test++)
257 if (testit (&test_case[test]))
258 ++fail;
260 /* Tilde-expansion tests. */
261 pw = getpwnam ("root");
262 if (pw != NULL)
264 ts.retval = 0;
265 ts.env = NULL;
266 ts.words = "~root ";
267 ts.flags = 0;
268 ts.wordc = 1;
269 ts.wordv[0] = pw->pw_dir;
270 ts.ifs = IFS;
272 if (testit (&ts))
273 ++fail;
275 ts.retval = 0;
276 ts.env = pw->pw_dir;
277 ts.words = "${var#~root}x";
278 ts.flags = 0;
279 ts.wordc = 1;
280 ts.wordv[0] = "x";
281 ts.ifs = IFS;
283 if (testit (&ts))
284 ++fail;
287 /* "~" expands to value of $HOME when HOME is set */
289 setenv ("HOME", "/dummy/home", 1);
290 ts.retval = 0;
291 ts.env = NULL;
292 ts.words = "~ ~/foo";
293 ts.flags = 0;
294 ts.wordc = 2;
295 ts.wordv[0] = "/dummy/home";
296 ts.wordv[1] = "/dummy/home/foo";
297 ts.ifs = IFS;
299 if (testit (&ts))
300 ++fail;
302 /* "~" expands to home dir from passwd file if HOME is not set */
304 pw = getpwuid (getuid ());
305 if (pw != NULL)
307 unsetenv ("HOME");
308 ts.retval = 0;
309 ts.env = NULL;
310 ts.words = "~";
311 ts.flags = 0;
312 ts.wordc = 1;
313 ts.wordv[0] = pw->pw_dir;
314 ts.ifs = IFS;
316 if (testit (&ts))
317 ++fail;
320 puts ("tests completed, now cleaning up");
322 /* Clean up */
323 for (i = 0; globfile[i]; ++i)
324 remove (globfile[i]);
326 if (cwd == NULL)
327 cwd = "..";
329 chdir (cwd);
330 rmdir (tmpdir);
332 printf ("tests failed: %d\n", fail);
334 return fail != 0;
338 static int
339 testit (struct test_case_struct *tc)
341 int retval;
342 wordexp_t we, sav_we;
343 char *dummy;
344 int bzzzt = 0;
345 int start_offs = 0;
346 int i;
348 if (tc->env)
349 setenv ("var", tc->env, 1);
350 else
351 unsetenv ("var");
353 if (tc->ifs)
354 setenv ("IFS", tc->ifs, 1);
355 else
356 unsetenv ("IFS");
358 sav_we.we_wordc = 99;
359 sav_we.we_wordv = &dummy;
360 sav_we.we_offs = 3;
361 we = sav_we;
363 printf ("Test %d (%s): ", ++tests, tc->words);
365 if (tc->flags & WRDE_APPEND)
367 /* initial wordexp() call, to be appended to */
368 if (wordexp ("pre1 pre2", &we, tc->flags & ~WRDE_APPEND) != 0)
370 printf ("FAILED setup\n");
371 return 1;
374 retval = wordexp (tc->words, &we, tc->flags);
376 if (tc->flags & WRDE_DOOFFS)
377 start_offs = sav_we.we_offs;
379 if (retval != tc->retval || (retval == 0 && we.we_wordc != tc->wordc))
380 bzzzt = 1;
381 else if (retval == 0)
383 for (i = 0; i < start_offs; ++i)
384 if (we.we_wordv[i] != NULL)
386 bzzzt = 1;
387 break;
390 for (i = 0; i < we.we_wordc; ++i)
391 if (we.we_wordv[i+start_offs] == NULL ||
392 strcmp (tc->wordv[i], we.we_wordv[i+start_offs]) != 0)
394 bzzzt = 1;
395 break;
399 if (bzzzt)
401 printf ("FAILED\n");
402 printf ("Test words: <%s>, need retval %d, wordc %Zd\n",
403 tc->words, tc->retval, tc->wordc);
404 if (start_offs != 0)
405 printf ("(preceded by %d NULLs)\n", start_offs);
406 printf ("Got retval %d, wordc %Zd: ", retval, we.we_wordc);
407 if (retval == 0 || retval == WRDE_NOSPACE)
409 for (i = 0; i < we.we_wordc + start_offs; ++i)
410 if (we.we_wordv[i] == NULL)
411 printf ("NULL ");
412 else
413 printf ("<%s> ", we.we_wordv[i]);
415 printf ("\n");
417 else if (retval != 0 && retval != WRDE_NOSPACE &&
418 (we.we_wordc != sav_we.we_wordc ||
419 we.we_wordv != sav_we.we_wordv ||
420 we.we_offs != sav_we.we_offs))
422 bzzzt = 1;
423 printf ("FAILED to restore wordexp_t members\n");
425 else
426 printf ("OK\n");
428 if (retval == 0 || retval == WRDE_NOSPACE)
429 wordfree (&we);
431 return bzzzt;