78ed81a3 |
1 | /* wldcrd.c |
2 | Expand wildcards. |
3 | |
4 | Copyright (C) 1991, 1992 Ian Lance Taylor |
5 | |
6 | This file is part of the Taylor UUCP package. |
7 | |
8 | This program is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU General Public License as |
10 | published by the Free Software Foundation; either version 2 of the |
11 | License, or (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, but |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
21 | |
22 | The author of the program may be contacted at ian@airs.com or |
23 | c/o Infinity Development Systems, P.O. Box 520, Waltham, MA 02254. |
24 | */ |
25 | |
26 | #include "uucp.h" |
27 | |
28 | #include "uudefs.h" |
29 | #include "sysdep.h" |
30 | #include "system.h" |
31 | |
32 | #include <ctype.h> |
33 | #include <errno.h> |
34 | |
35 | #if HAVE_GLOB && ! HAVE_GLOB_H |
36 | #undef HAVE_GLOB |
37 | #define HAVE_GLOB 0 |
38 | #endif |
39 | |
40 | #if HAVE_GLOB |
41 | #include <glob.h> |
42 | #endif |
43 | \f |
44 | /* Local variables to hold the wildcard in progress. */ |
45 | |
46 | #if HAVE_GLOB |
47 | static glob_t sSglob; |
48 | static int iSglob; |
49 | #else |
50 | static char *zSwildcard_alloc; |
51 | static char *zSwildcard; |
52 | #endif |
53 | \f |
54 | /* Start getting a wildcarded file spec. Use the glob function if it |
55 | is available, and otherwise use the shell. */ |
56 | |
57 | boolean |
58 | fsysdep_wildcard_start (zfile) |
59 | const char *zfile; |
60 | { |
61 | #if HAVE_GLOB |
62 | |
63 | #if DEBUG > 0 |
64 | if (*zfile != '/') |
65 | ulog (LOG_FATAL, "fsysdep_wildcard: %s: Can't happen", zfile); |
66 | #endif |
67 | |
68 | if (glob (zfile, 0, (int (*) ()) NULL, &sSglob) != 0) |
69 | sSglob.gl_pathc = 0; |
70 | iSglob = 0; |
71 | return TRUE; |
72 | |
73 | #else /* ! HAVE_GLOB */ |
74 | |
75 | char *zcmd, *zto; |
76 | const char *zfrom; |
77 | size_t c; |
78 | const char *azargs[4]; |
79 | FILE *e; |
80 | pid_t ipid; |
81 | |
82 | #if DEBUG > 0 |
83 | if (*zfile != '/') |
84 | ulog (LOG_FATAL, "fsysdep_wildcard: %s: Can't happen", zfile); |
85 | #endif |
86 | |
87 | zSwildcard_alloc = NULL; |
88 | zSwildcard = NULL; |
89 | |
90 | zcmd = zbufalc (sizeof ECHO_PROGRAM + sizeof " " + 2 * strlen (zfile)); |
91 | memcpy (zcmd, ECHO_PROGRAM, sizeof ECHO_PROGRAM - 1); |
92 | zto = zcmd + sizeof ECHO_PROGRAM - 1; |
93 | *zto++ = ' '; |
94 | zfrom = zfile; |
95 | while (*zfrom != '\0') |
96 | { |
97 | /* To avoid shell trickery, we quote all characters except |
98 | letters, digits, and wildcard specifiers. We don't quote '/' |
99 | to avoid an Ultrix sh bug. */ |
100 | if (! isalnum (*zfrom) |
101 | && *zfrom != '*' |
102 | && *zfrom != '?' |
103 | && *zfrom != '[' |
104 | && *zfrom != ']' |
105 | && *zfrom != '/') |
106 | *zto++ = '\\'; |
107 | *zto++ = *zfrom++; |
108 | } |
109 | *zto = '\0'; |
110 | |
111 | azargs[0] = "/bin/sh"; |
112 | azargs[1] = "-c"; |
113 | azargs[2] = zcmd; |
114 | azargs[3] = NULL; |
115 | |
116 | ubuffree (zcmd); |
117 | |
118 | e = espopen (azargs, TRUE, &ipid); |
119 | if (e == NULL) |
120 | { |
121 | ulog (LOG_ERROR, "espopen: %s", strerror (errno)); |
122 | return FALSE; |
123 | } |
124 | |
125 | zSwildcard_alloc = NULL; |
126 | c = 0; |
127 | if (getline (&zSwildcard_alloc, &c, e) <= 0) |
128 | { |
129 | xfree ((pointer) zSwildcard_alloc); |
130 | zSwildcard_alloc = NULL; |
131 | } |
132 | |
133 | if (ixswait ((unsigned long) ipid, ECHO_PROGRAM) != 0) |
134 | { |
135 | xfree ((pointer) zSwildcard_alloc); |
136 | return FALSE; |
137 | } |
138 | |
139 | if (zSwildcard_alloc == NULL) |
140 | return FALSE; |
141 | |
142 | DEBUG_MESSAGE1 (DEBUG_EXECUTE, |
143 | "fsysdep_wildcard_start: got \"%s\"", |
144 | zSwildcard_alloc); |
145 | |
146 | zSwildcard = zSwildcard_alloc; |
147 | |
148 | return TRUE; |
149 | |
150 | #endif /* ! HAVE_GLOB */ |
151 | } |
152 | |
153 | /* Get the next wildcard spec. */ |
154 | |
155 | /*ARGSUSED*/ |
156 | char * |
157 | zsysdep_wildcard (zfile) |
158 | const char *zfile; |
159 | { |
160 | #if HAVE_GLOB |
161 | |
162 | char *zret; |
163 | |
164 | if (iSglob >= sSglob.gl_pathc) |
165 | return NULL; |
166 | zret = zbufcpy (sSglob.gl_pathv[iSglob]); |
167 | ++iSglob; |
168 | return zret; |
169 | |
170 | #else /* ! HAVE_GLOB */ |
171 | |
172 | char *zret; |
173 | |
174 | if (zSwildcard_alloc == NULL || zSwildcard == NULL) |
175 | return NULL; |
176 | |
177 | zret = zSwildcard; |
178 | |
179 | while (*zSwildcard != '\0' && ! isspace (BUCHAR (*zSwildcard))) |
180 | ++zSwildcard; |
181 | |
182 | if (*zSwildcard != '\0') |
183 | { |
184 | *zSwildcard = '\0'; |
185 | ++zSwildcard; |
186 | while (*zSwildcard != '\0' && isspace (BUCHAR (*zSwildcard))) |
187 | ++zSwildcard; |
188 | } |
189 | |
190 | if (*zSwildcard == '\0') |
191 | zSwildcard = NULL; |
192 | |
193 | return zbufcpy (zret); |
194 | |
195 | #endif /* ! HAVE_GLOB */ |
196 | } |
197 | |
198 | /* Finish up getting wildcard specs. */ |
199 | |
200 | boolean |
201 | fsysdep_wildcard_end () |
202 | { |
203 | #if HAVE_GLOB |
204 | globfree (&sSglob); |
205 | return TRUE; |
206 | #else /* ! HAVE_GLOB */ |
207 | xfree ((pointer) zSwildcard_alloc); |
208 | zSwildcard_alloc = NULL; |
209 | zSwildcard = NULL; |
210 | return TRUE; |
211 | #endif /* ! HAVE_GLOB */ |
212 | } |