]>
Commit | Line | Data |
---|---|---|
688903eb | 1 | /* Copyright (C) 1997-2018 Free Software Foundation, Inc. |
61eb22d3 UD |
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 | |
41bdb6e2 AJ |
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. | |
61eb22d3 UD |
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 | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
61eb22d3 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
61eb22d3 | 17 | |
3f62b69a | 18 | #include <getopt.h> |
61eb22d3 | 19 | #include <stdio.h> |
3e1f480e | 20 | #include <stdlib.h> |
650425ce | 21 | #include <unistd.h> |
3f62b69a | 22 | #include <glob.h> |
61eb22d3 UD |
23 | |
24 | int | |
25 | main (int argc, char *argv[]) | |
26 | { | |
3f62b69a UD |
27 | int i, j; |
28 | int glob_flags = 0; | |
29 | glob_t g; | |
30 | int quotes = 1; | |
61eb22d3 | 31 | |
460adbb8 UD |
32 | g.gl_offs = 0; |
33 | ||
34 | while ((i = getopt (argc, argv, "bcdeEgmopqstT")) != -1) | |
3f62b69a UD |
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; | |
460adbb8 UD |
49 | case 'E': |
50 | glob_flags |= GLOB_ERR; | |
51 | break; | |
3f62b69a UD |
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; | |
1bc21e7a UD |
74 | case 'T': |
75 | glob_flags |= GLOB_TILDE_CHECK; | |
76 | break; | |
3f62b69a UD |
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) | |
650425ce | 86 | exit (1); |
61eb22d3 | 87 | |
3f62b69a UD |
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? */ | |
61eb22d3 UD |
97 | if (i == GLOB_NOSPACE) |
98 | puts ("GLOB_NOSPACE"); | |
3f62b69a UD |
99 | else if (i == GLOB_ABORTED) |
100 | puts ("GLOB_ABORTED"); | |
61eb22d3 UD |
101 | else if (i == GLOB_NOMATCH) |
102 | puts ("GLOB_NOMATCH"); | |
103 | ||
460adbb8 UD |
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) | |
3f62b69a | 107 | g.gl_pathv[0] = (char *) "abc"; |
61eb22d3 | 108 | |
3f62b69a UD |
109 | /* Print out the names. Unless otherwise specified, qoute them. */ |
110 | if (g.gl_pathv) | |
61eb22d3 | 111 | { |
460adbb8 UD |
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)", | |
3f62b69a | 115 | quotes ? "'" : ""); |
61eb22d3 UD |
116 | } |
117 | return 0; | |
118 | } |