Don't crash when dependencies are missing
[glibc.git] / posix / globtest.c
blob5f1ffb74f48be5156f096cb93d60ce742a20aadc
1 /* Copyright (C) 1997, 1998, 2000 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 <getopt.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <glob.h>
25 int
26 main (int argc, char *argv[])
28 int i, j;
29 int glob_flags = 0;
30 glob_t g;
31 int quotes = 1;
33 g.gl_offs = 0;
35 while ((i = getopt (argc, argv, "bcdeEgmopqstT")) != -1)
36 switch(i)
38 case 'b':
39 glob_flags |= GLOB_BRACE;
40 break;
41 case 'c':
42 glob_flags |= GLOB_NOCHECK;
43 break;
44 case 'd':
45 glob_flags |= GLOB_ONLYDIR;
46 break;
47 case 'e':
48 glob_flags |= GLOB_NOESCAPE;
49 break;
50 case 'E':
51 glob_flags |= GLOB_ERR;
52 break;
53 case 'g':
54 glob_flags |= GLOB_NOMAGIC;
55 break;
56 case 'm':
57 glob_flags |= GLOB_MARK;
58 break;
59 case 'o':
60 glob_flags |= GLOB_DOOFFS;
61 g.gl_offs = 1;
62 break;
63 case 'p':
64 glob_flags |= GLOB_PERIOD;
65 break;
66 case 'q':
67 quotes = 0;
68 break;
69 case 's':
70 glob_flags |= GLOB_NOSORT;
71 break;
72 case 't':
73 glob_flags |= GLOB_TILDE;
74 break;
75 case 'T':
76 glob_flags |= GLOB_TILDE_CHECK;
77 break;
78 default:
79 exit (-1);
82 if (optind >= argc || chdir (argv[optind]))
83 exit(1);
85 j = optind + 1;
86 if (optind + 1 >= argc)
87 exit (1);
89 /* Do a glob on each remaining argument. */
90 for (j = optind + 1; j < argc; j++) {
91 i = glob (argv[j], glob_flags, NULL, &g);
92 if (i != 0)
93 break;
94 glob_flags |= GLOB_APPEND;
97 /* Was there an error? */
98 if (i == GLOB_NOSPACE)
99 puts ("GLOB_NOSPACE");
100 else if (i == GLOB_ABORTED)
101 puts ("GLOB_ABORTED");
102 else if (i == GLOB_NOMATCH)
103 puts ("GLOB_NOMATCH");
105 /* If we set an offset, fill in the first field.
106 (Unless glob() has filled it in already - which is an error) */
107 if ((glob_flags & GLOB_DOOFFS) && g.gl_pathv[0] == NULL)
108 g.gl_pathv[0] = (char *) "abc";
110 /* Print out the names. Unless otherwise specified, qoute them. */
111 if (g.gl_pathv)
113 for (i = 0; i < g.gl_offs + g.gl_pathc; ++i)
114 printf ("%s%s%s\n", quotes ? "`" : "",
115 g.gl_pathv[i] ? g.gl_pathv[i] : "(null)",
116 quotes ? "'" : "");
118 return 0;