Update.
[platform/upstream/glibc.git] / posix / globtest.c
1 /* Copyright (C) 1997, 1998 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 Library General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    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    Library General Public License for more details.
13
14    You should have received a copy of the GNU Library General Public
15    License along with the GNU C Library; see the file COPYING.LIB.  If not,
16    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17    Boston, MA 02111-1307, USA.  */
18
19 #include <getopt.h>
20 #include <stdio.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   while ((i = getopt (argc, argv, "bcdegmopqstT")) != -1)
33     switch(i)
34       {
35       case 'b':
36         glob_flags |= GLOB_BRACE;
37         break;
38       case 'c':
39         glob_flags |= GLOB_NOCHECK;
40         break;
41       case 'd':
42         glob_flags |= GLOB_ONLYDIR;
43         break;
44       case 'e':
45         glob_flags |= GLOB_NOESCAPE;
46         break;
47       case 'g':
48         glob_flags |= GLOB_NOMAGIC;
49         break;
50       case 'm':
51         glob_flags |= GLOB_MARK;
52         break;
53       case 'o':
54         glob_flags |= GLOB_DOOFFS;
55         g.gl_offs = 1;
56         break;
57       case 'p':
58         glob_flags |= GLOB_PERIOD;
59         break;
60       case 'q':
61         quotes = 0;
62         break;
63       case 's':
64         glob_flags |= GLOB_NOSORT;
65         break;
66       case 't':
67         glob_flags |= GLOB_TILDE;
68         break;
69       case 'T':
70         glob_flags |= GLOB_TILDE_CHECK;
71         break;
72       default:
73         exit (-1);
74       }
75
76   if (optind >= argc || chdir (argv[optind]))
77     exit(1);
78
79   j = optind + 1;
80   if (optind + 1 >= argc)
81     exit (1);
82
83   /* Do a glob on each remaining argument.  */
84   for (j = optind + 1; j < argc; j++) {
85     i = glob (argv[j], glob_flags, NULL, &g);
86     if (i != 0)
87       break;
88     glob_flags |= GLOB_APPEND;
89   }
90
91   /* Was there an error? */
92   if (i == GLOB_NOSPACE)
93     puts ("GLOB_NOSPACE");
94   else if (i == GLOB_ABORTED)
95     puts ("GLOB_ABORTED");
96   else if (i == GLOB_NOMATCH)
97     puts ("GLOB_NOMATCH");
98
99   /* If we set an offset, fill in the first field.  */
100   if (glob_flags & GLOB_DOOFFS)
101     g.gl_pathv[0] = (char *) "abc";
102
103   /* Print out the names.  Unless otherwise specified, qoute them.  */
104   if (g.gl_pathv)
105     {
106       for (i = 0; i < g.gl_pathc; ++i)
107         printf ("%s%s%s\n", quotes ? "`" : "", g.gl_pathv[i],
108                 quotes ? "'" : "");
109     }
110   return 0;
111 }