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