glob.c revision 1.2 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1989 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * Guido van Rossum.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
38 1.2 mycroft /*static char sccsid[] = "from: @(#)glob.c 5.18 (Berkeley) 12/4/92";*/
39 1.2 mycroft static char rcsid[] = "$Id: glob.c,v 1.2 1993/07/30 07:57:52 mycroft Exp $";
40 1.1 cgd #endif /* LIBC_SCCS and not lint */
41 1.1 cgd
42 1.1 cgd /*
43 1.1 cgd * glob(3) -- a superset of the one defined in POSIX 1003.2.
44 1.1 cgd *
45 1.1 cgd * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
46 1.1 cgd *
47 1.1 cgd * Optional extra services, controlled by flags not defined by POSIX:
48 1.1 cgd *
49 1.1 cgd * GLOB_QUOTE:
50 1.1 cgd * Escaping convention: \ inhibits any special meaning the following
51 1.1 cgd * character might have (except \ at end of string is retained).
52 1.1 cgd * GLOB_MAGCHAR:
53 1.1 cgd * Set in gl_flags if pattern contained a globbing character.
54 1.2 mycroft * GLOB_NOMAGIC:
55 1.2 mycroft * Same as GLOB_NOCHECK, but it will only append pattern if it did
56 1.2 mycroft * not contain any magic characters. [Used in csh style globbing]
57 1.2 mycroft * GLOB_ALTDIRFUNC:
58 1.2 mycroft * Use alternately specified directory access functions.
59 1.1 cgd * gl_matchc:
60 1.1 cgd * Number of matches in the current invocation of glob.
61 1.1 cgd */
62 1.1 cgd
63 1.1 cgd #include <sys/param.h>
64 1.1 cgd #include <sys/stat.h>
65 1.1 cgd #include <dirent.h>
66 1.1 cgd #include <glob.h>
67 1.1 cgd #include <ctype.h>
68 1.1 cgd #include <errno.h>
69 1.1 cgd #include <string.h>
70 1.1 cgd #include <stdio.h>
71 1.1 cgd #include <stdlib.h>
72 1.1 cgd
73 1.1 cgd #define DOLLAR '$'
74 1.1 cgd #define DOT '.'
75 1.1 cgd #define EOS '\0'
76 1.1 cgd #define LBRACKET '['
77 1.1 cgd #define NOT '!'
78 1.1 cgd #define QUESTION '?'
79 1.1 cgd #define QUOTE '\\'
80 1.1 cgd #define RANGE '-'
81 1.1 cgd #define RBRACKET ']'
82 1.1 cgd #define SEP '/'
83 1.1 cgd #define STAR '*'
84 1.1 cgd #define TILDE '~'
85 1.1 cgd #define UNDERSCORE '_'
86 1.1 cgd
87 1.1 cgd #define M_QUOTE 0x8000
88 1.1 cgd #define M_PROTECT 0x4000
89 1.1 cgd #define M_MASK 0xffff
90 1.1 cgd #define M_ASCII 0x00ff
91 1.1 cgd
92 1.1 cgd #define CHAR(c) ((c)&M_ASCII)
93 1.1 cgd #define META(c) ((c)|M_QUOTE)
94 1.1 cgd #define M_ALL META('*')
95 1.1 cgd #define M_END META(']')
96 1.1 cgd #define M_NOT META('!')
97 1.1 cgd #define M_ONE META('?')
98 1.1 cgd #define M_RNG META('-')
99 1.1 cgd #define M_SET META('[')
100 1.1 cgd #define ismeta(c) (((c)&M_QUOTE) != 0)
101 1.1 cgd
102 1.1 cgd typedef u_short Char;
103 1.1 cgd
104 1.1 cgd static int compare __P((const void *, const void *));
105 1.1 cgd static void g_Ctoc __P((Char *, char *));
106 1.2 mycroft static int g_lstat __P((Char *, struct stat *, glob_t *));
107 1.2 mycroft static DIR *g_opendir __P((Char *, glob_t *));
108 1.1 cgd static Char *g_strchr __P((Char *, int));
109 1.2 mycroft static int g_stat __P((Char *, struct stat *, glob_t *));
110 1.1 cgd static int glob1 __P((Char *, glob_t *));
111 1.1 cgd static int glob2 __P((Char *, Char *, Char *, glob_t *));
112 1.1 cgd static int glob3 __P((Char *, Char *, Char *, Char *, glob_t *));
113 1.1 cgd static int globextend __P((Char *, glob_t *));
114 1.1 cgd static int match __P((Char *, Char *, Char *));
115 1.1 cgd #ifdef DEBUG
116 1.1 cgd static void qprintf __P((Char *));
117 1.1 cgd #endif
118 1.1 cgd
119 1.1 cgd /*
120 1.1 cgd * The main glob() routine: compiles the pattern (optionally processing
121 1.1 cgd * quotes), calls glob1() to do the real pattern matching, and finally
122 1.1 cgd * sorts the list (unless unsorted operation is requested). Returns 0
123 1.1 cgd * if things went well, nonzero if errors occurred. It is not an error
124 1.1 cgd * to find no matches.
125 1.1 cgd */
126 1.1 cgd glob(pattern, flags, errfunc, pglob)
127 1.1 cgd const char *pattern;
128 1.1 cgd int flags, (*errfunc) __P((char *, int));
129 1.1 cgd glob_t *pglob;
130 1.1 cgd {
131 1.1 cgd const u_char *compilepat, *patnext;
132 1.1 cgd int c, err, oldpathc;
133 1.1 cgd Char *bufnext, *bufend, *compilebuf, *qpatnext, patbuf[MAXPATHLEN+1];
134 1.1 cgd
135 1.1 cgd patnext = (u_char *) pattern;
136 1.1 cgd if (!(flags & GLOB_APPEND)) {
137 1.1 cgd pglob->gl_pathc = 0;
138 1.1 cgd pglob->gl_pathv = NULL;
139 1.1 cgd if (!(flags & GLOB_DOOFFS))
140 1.1 cgd pglob->gl_offs = 0;
141 1.1 cgd }
142 1.1 cgd pglob->gl_flags = flags & ~GLOB_MAGCHAR;
143 1.1 cgd pglob->gl_errfunc = errfunc;
144 1.1 cgd oldpathc = pglob->gl_pathc;
145 1.1 cgd pglob->gl_matchc = 0;
146 1.1 cgd
147 1.1 cgd bufnext = patbuf;
148 1.1 cgd bufend = bufnext + MAXPATHLEN;
149 1.1 cgd compilebuf = bufnext;
150 1.1 cgd compilepat = patnext;
151 1.1 cgd if (flags & GLOB_QUOTE) {
152 1.1 cgd /* Protect the quoted characters. */
153 1.1 cgd while (bufnext < bufend && (c = *patnext++) != EOS)
154 1.1 cgd if (c == QUOTE) {
155 1.1 cgd if ((c = *patnext++) == EOS) {
156 1.1 cgd c = QUOTE;
157 1.1 cgd --patnext;
158 1.1 cgd }
159 1.1 cgd *bufnext++ = c | M_PROTECT;
160 1.1 cgd }
161 1.1 cgd else
162 1.1 cgd *bufnext++ = c;
163 1.1 cgd }
164 1.1 cgd else
165 1.1 cgd while (bufnext < bufend && (c = *patnext++) != EOS)
166 1.1 cgd *bufnext++ = c;
167 1.1 cgd *bufnext = EOS;
168 1.1 cgd
169 1.1 cgd bufnext = patbuf;
170 1.1 cgd qpatnext = patbuf;
171 1.1 cgd /* We don't need to check for buffer overflow any more. */
172 1.1 cgd while ((c = *qpatnext++) != EOS) {
173 1.1 cgd switch (c) {
174 1.1 cgd case LBRACKET:
175 1.1 cgd c = *qpatnext;
176 1.1 cgd if (c == NOT)
177 1.1 cgd ++qpatnext;
178 1.1 cgd if (*qpatnext == EOS ||
179 1.1 cgd g_strchr(qpatnext+1, RBRACKET) == NULL) {
180 1.1 cgd *bufnext++ = LBRACKET;
181 1.1 cgd if (c == NOT)
182 1.1 cgd --qpatnext;
183 1.1 cgd break;
184 1.1 cgd }
185 1.1 cgd *bufnext++ = M_SET;
186 1.1 cgd if (c == NOT)
187 1.1 cgd *bufnext++ = M_NOT;
188 1.1 cgd c = *qpatnext++;
189 1.1 cgd do {
190 1.1 cgd *bufnext++ = CHAR(c);
191 1.1 cgd if (*qpatnext == RANGE &&
192 1.1 cgd (c = qpatnext[1]) != RBRACKET) {
193 1.1 cgd *bufnext++ = M_RNG;
194 1.1 cgd *bufnext++ = CHAR(c);
195 1.1 cgd qpatnext += 2;
196 1.1 cgd }
197 1.1 cgd } while ((c = *qpatnext++) != RBRACKET);
198 1.2 mycroft pglob->gl_flags |= GLOB_MAGCHAR;
199 1.1 cgd *bufnext++ = M_END;
200 1.1 cgd break;
201 1.1 cgd case QUESTION:
202 1.1 cgd pglob->gl_flags |= GLOB_MAGCHAR;
203 1.1 cgd *bufnext++ = M_ONE;
204 1.1 cgd break;
205 1.1 cgd case STAR:
206 1.1 cgd pglob->gl_flags |= GLOB_MAGCHAR;
207 1.2 mycroft /* collapse adjacent stars to one,
208 1.2 mycroft * to avoid exponential behavior
209 1.2 mycroft */
210 1.2 mycroft if (bufnext == patbuf || bufnext[-1] != M_ALL)
211 1.2 mycroft *bufnext++ = M_ALL;
212 1.1 cgd break;
213 1.1 cgd default:
214 1.1 cgd *bufnext++ = CHAR(c);
215 1.1 cgd break;
216 1.1 cgd }
217 1.1 cgd }
218 1.1 cgd *bufnext = EOS;
219 1.1 cgd #ifdef DEBUG
220 1.1 cgd qprintf(patbuf);
221 1.1 cgd #endif
222 1.1 cgd
223 1.1 cgd if ((err = glob1(patbuf, pglob)) != 0)
224 1.1 cgd return(err);
225 1.1 cgd
226 1.2 mycroft /*
227 1.2 mycroft * If there was no match we are going to append the pattern
228 1.2 mycroft * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
229 1.2 mycroft * and the pattern did not contain any magic characters
230 1.2 mycroft * GLOB_NOMAGIC is there just for compatibility with csh.
231 1.2 mycroft */
232 1.2 mycroft if (pglob->gl_pathc == oldpathc &&
233 1.2 mycroft ((flags & GLOB_NOCHECK) ||
234 1.2 mycroft ((flags & GLOB_NOMAGIC) && !(pglob->gl_flags & GLOB_MAGCHAR)))) {
235 1.1 cgd if (!(flags & GLOB_QUOTE)) {
236 1.1 cgd Char *dp = compilebuf;
237 1.1 cgd const u_char *sp = compilepat;
238 1.1 cgd while (*dp++ = *sp++);
239 1.1 cgd }
240 1.1 cgd else {
241 1.1 cgd /*
242 1.1 cgd * Copy pattern, interpreting quotes; this is slightly
243 1.1 cgd * different than the interpretation of quotes above
244 1.1 cgd * -- which should prevail?
245 1.1 cgd */
246 1.1 cgd while (*compilepat != EOS) {
247 1.1 cgd if (*compilepat == QUOTE) {
248 1.1 cgd if (*++compilepat == EOS)
249 1.1 cgd --compilepat;
250 1.1 cgd }
251 1.1 cgd *compilebuf++ = (u_char)*compilepat++;
252 1.1 cgd }
253 1.1 cgd *compilebuf = EOS;
254 1.1 cgd }
255 1.1 cgd return(globextend(patbuf, pglob));
256 1.1 cgd } else if (!(flags & GLOB_NOSORT))
257 1.1 cgd qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
258 1.1 cgd pglob->gl_pathc - oldpathc, sizeof(char *), compare);
259 1.1 cgd return(0);
260 1.1 cgd }
261 1.1 cgd
262 1.1 cgd static int
263 1.1 cgd compare(p, q)
264 1.1 cgd const void *p, *q;
265 1.1 cgd {
266 1.1 cgd return(strcmp(*(char **)p, *(char **)q));
267 1.1 cgd }
268 1.1 cgd
269 1.1 cgd static
270 1.1 cgd glob1(pattern, pglob)
271 1.1 cgd Char *pattern;
272 1.1 cgd glob_t *pglob;
273 1.1 cgd {
274 1.1 cgd Char pathbuf[MAXPATHLEN+1];
275 1.1 cgd
276 1.1 cgd /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
277 1.1 cgd if (*pattern == EOS)
278 1.1 cgd return(0);
279 1.1 cgd return(glob2(pathbuf, pathbuf, pattern, pglob));
280 1.1 cgd }
281 1.1 cgd
282 1.1 cgd /*
283 1.1 cgd * The functions glob2 and glob3 are mutually recursive; there is one level
284 1.1 cgd * of recursion for each segment in the pattern that contains one or more
285 1.1 cgd * meta characters.
286 1.1 cgd */
287 1.1 cgd static
288 1.1 cgd glob2(pathbuf, pathend, pattern, pglob)
289 1.1 cgd Char *pathbuf, *pathend, *pattern;
290 1.1 cgd glob_t *pglob;
291 1.1 cgd {
292 1.1 cgd struct stat sb;
293 1.1 cgd Char *p, *q;
294 1.1 cgd int anymeta;
295 1.1 cgd
296 1.1 cgd /*
297 1.1 cgd * Loop over pattern segments until end of pattern or until
298 1.1 cgd * segment with meta character found.
299 1.1 cgd */
300 1.1 cgd for (anymeta = 0;;) {
301 1.1 cgd if (*pattern == EOS) { /* End of pattern? */
302 1.1 cgd *pathend = EOS;
303 1.2 mycroft if (g_lstat(pathbuf, &sb, pglob))
304 1.1 cgd return(0);
305 1.1 cgd
306 1.1 cgd if (((pglob->gl_flags & GLOB_MARK) &&
307 1.1 cgd pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
308 1.1 cgd || (S_ISLNK(sb.st_mode) &&
309 1.2 mycroft (g_stat(pathbuf, &sb, pglob) == 0) &&
310 1.1 cgd S_ISDIR(sb.st_mode)))) {
311 1.1 cgd *pathend++ = SEP;
312 1.1 cgd *pathend = EOS;
313 1.1 cgd }
314 1.1 cgd ++pglob->gl_matchc;
315 1.1 cgd return(globextend(pathbuf, pglob));
316 1.1 cgd }
317 1.1 cgd
318 1.1 cgd /* Find end of next segment, copy tentatively to pathend. */
319 1.1 cgd q = pathend;
320 1.1 cgd p = pattern;
321 1.1 cgd while (*p != EOS && *p != SEP) {
322 1.1 cgd if (ismeta(*p))
323 1.1 cgd anymeta = 1;
324 1.1 cgd *q++ = *p++;
325 1.1 cgd }
326 1.1 cgd
327 1.1 cgd if (!anymeta) { /* No expansion, do next segment. */
328 1.1 cgd pathend = q;
329 1.1 cgd pattern = p;
330 1.1 cgd while (*pattern == SEP)
331 1.1 cgd *pathend++ = *pattern++;
332 1.1 cgd } else /* Need expansion, recurse. */
333 1.1 cgd return(glob3(pathbuf, pathend, pattern, p, pglob));
334 1.1 cgd }
335 1.1 cgd /* NOTREACHED */
336 1.1 cgd }
337 1.1 cgd
338 1.1 cgd static
339 1.1 cgd glob3(pathbuf, pathend, pattern, restpattern, pglob)
340 1.1 cgd Char *pathbuf, *pathend, *pattern, *restpattern;
341 1.1 cgd glob_t *pglob;
342 1.1 cgd {
343 1.1 cgd register struct dirent *dp;
344 1.2 mycroft struct dirent *(*readdirfunc)();
345 1.1 cgd DIR *dirp;
346 1.1 cgd int len, err;
347 1.2 mycroft char buf[MAXPATHLEN];
348 1.1 cgd
349 1.1 cgd *pathend = EOS;
350 1.1 cgd errno = 0;
351 1.1 cgd
352 1.2 mycroft if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
353 1.1 cgd /* TODO: don't call for ENOENT or ENOTDIR? */
354 1.2 mycroft if (pglob->gl_errfunc) {
355 1.2 mycroft g_Ctoc(pathbuf, buf);
356 1.2 mycroft if (pglob->gl_errfunc(buf, errno) ||
357 1.2 mycroft pglob->gl_flags & GLOB_ERR)
358 1.2 mycroft return (GLOB_ABEND);
359 1.2 mycroft }
360 1.2 mycroft return(0);
361 1.2 mycroft }
362 1.1 cgd
363 1.1 cgd err = 0;
364 1.1 cgd
365 1.1 cgd /* Search directory for matching names. */
366 1.2 mycroft if (pglob->gl_flags & GLOB_ALTDIRFUNC)
367 1.2 mycroft readdirfunc = pglob->gl_readdir;
368 1.2 mycroft else
369 1.2 mycroft readdirfunc = readdir;
370 1.2 mycroft while ((dp = (*readdirfunc)(dirp))) {
371 1.1 cgd register u_char *sc;
372 1.1 cgd register Char *dc;
373 1.1 cgd
374 1.1 cgd /* Initial DOT must be matched literally. */
375 1.1 cgd if (dp->d_name[0] == DOT && *pattern != DOT)
376 1.1 cgd continue;
377 1.1 cgd for (sc = (u_char *) dp->d_name, dc = pathend;
378 1.1 cgd *dc++ = *sc++;);
379 1.1 cgd if (!match(pathend, pattern, restpattern)) {
380 1.1 cgd *pathend = EOS;
381 1.1 cgd continue;
382 1.1 cgd }
383 1.1 cgd err = glob2(pathbuf, --dc, restpattern, pglob);
384 1.1 cgd if (err)
385 1.1 cgd break;
386 1.1 cgd }
387 1.1 cgd
388 1.2 mycroft if (pglob->gl_flags & GLOB_ALTDIRFUNC)
389 1.2 mycroft (*pglob->gl_closedir)(dirp);
390 1.2 mycroft else
391 1.2 mycroft closedir(dirp);
392 1.1 cgd return(err);
393 1.1 cgd }
394 1.1 cgd
395 1.1 cgd
396 1.1 cgd /*
397 1.1 cgd * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
398 1.1 cgd * add the new item, and update gl_pathc.
399 1.1 cgd *
400 1.1 cgd * This assumes the BSD realloc, which only copies the block when its size
401 1.1 cgd * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
402 1.1 cgd * behavior.
403 1.1 cgd *
404 1.1 cgd * Return 0 if new item added, error code if memory couldn't be allocated.
405 1.1 cgd *
406 1.1 cgd * Invariant of the glob_t structure:
407 1.1 cgd * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
408 1.1 cgd * gl_pathv points to (gl_offs + gl_pathc + 1) items.
409 1.1 cgd */
410 1.1 cgd static int
411 1.1 cgd globextend(path, pglob)
412 1.1 cgd Char *path;
413 1.1 cgd glob_t *pglob;
414 1.1 cgd {
415 1.1 cgd register char **pathv;
416 1.1 cgd register int i;
417 1.1 cgd u_int newsize;
418 1.1 cgd char *copy;
419 1.1 cgd Char *p;
420 1.1 cgd
421 1.1 cgd newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
422 1.1 cgd pathv = (char **)realloc((char *)pglob->gl_pathv, newsize);
423 1.1 cgd if (pathv == NULL)
424 1.1 cgd return(GLOB_NOSPACE);
425 1.1 cgd
426 1.1 cgd if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
427 1.1 cgd /* first time around -- clear initial gl_offs items */
428 1.1 cgd pathv += pglob->gl_offs;
429 1.1 cgd for (i = pglob->gl_offs; --i >= 0; )
430 1.1 cgd *--pathv = NULL;
431 1.1 cgd }
432 1.1 cgd pglob->gl_pathv = pathv;
433 1.1 cgd
434 1.1 cgd for (p = path; *p++;);
435 1.1 cgd if ((copy = malloc(p - path)) != NULL) {
436 1.1 cgd g_Ctoc(path, copy);
437 1.1 cgd pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
438 1.1 cgd }
439 1.1 cgd pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
440 1.1 cgd return(copy == NULL ? GLOB_NOSPACE : 0);
441 1.1 cgd }
442 1.1 cgd
443 1.1 cgd
444 1.1 cgd /*
445 1.1 cgd * pattern matching function for filenames. Each occurrence of the *
446 1.1 cgd * pattern causes a recursion level.
447 1.1 cgd */
448 1.1 cgd static
449 1.1 cgd match(name, pat, patend)
450 1.1 cgd register Char *name, *pat, *patend;
451 1.1 cgd {
452 1.1 cgd int ok, negate_range;
453 1.1 cgd Char c, k;
454 1.1 cgd
455 1.1 cgd while (pat < patend) {
456 1.1 cgd c = *pat++;
457 1.1 cgd switch (c & M_MASK) {
458 1.1 cgd case M_ALL:
459 1.1 cgd if (pat == patend)
460 1.1 cgd return(1);
461 1.2 mycroft do
462 1.2 mycroft if (match(name, pat, patend))
463 1.2 mycroft return(1);
464 1.2 mycroft while (*name++ != EOS);
465 1.1 cgd return(0);
466 1.1 cgd case M_ONE:
467 1.1 cgd if (*name++ == EOS)
468 1.1 cgd return(0);
469 1.1 cgd break;
470 1.1 cgd case M_SET:
471 1.1 cgd ok = 0;
472 1.2 mycroft if ((k = *name++) == EOS)
473 1.2 mycroft return(0);
474 1.1 cgd if (negate_range = ((*pat & M_MASK) == M_NOT))
475 1.1 cgd ++pat;
476 1.1 cgd while (((c = *pat++) & M_MASK) != M_END)
477 1.1 cgd if ((*pat & M_MASK) == M_RNG) {
478 1.1 cgd if (c <= k && k <= pat[1])
479 1.1 cgd ok = 1;
480 1.1 cgd pat += 2;
481 1.1 cgd } else if (c == k)
482 1.1 cgd ok = 1;
483 1.1 cgd if (ok == negate_range)
484 1.1 cgd return(0);
485 1.1 cgd break;
486 1.1 cgd default:
487 1.1 cgd if (*name++ != c)
488 1.1 cgd return(0);
489 1.1 cgd break;
490 1.1 cgd }
491 1.1 cgd }
492 1.1 cgd return(*name == EOS);
493 1.1 cgd }
494 1.1 cgd
495 1.1 cgd /* Free allocated data belonging to a glob_t structure. */
496 1.1 cgd void
497 1.1 cgd globfree(pglob)
498 1.1 cgd glob_t *pglob;
499 1.1 cgd {
500 1.1 cgd register int i;
501 1.1 cgd register char **pp;
502 1.1 cgd
503 1.1 cgd if (pglob->gl_pathv != NULL) {
504 1.1 cgd pp = pglob->gl_pathv + pglob->gl_offs;
505 1.1 cgd for (i = pglob->gl_pathc; i--; ++pp)
506 1.1 cgd if (*pp)
507 1.1 cgd free(*pp);
508 1.1 cgd free(pglob->gl_pathv);
509 1.1 cgd }
510 1.1 cgd }
511 1.1 cgd
512 1.1 cgd static DIR *
513 1.2 mycroft g_opendir(str, pglob)
514 1.1 cgd register Char *str;
515 1.2 mycroft glob_t *pglob;
516 1.1 cgd {
517 1.1 cgd char buf[MAXPATHLEN];
518 1.2 mycroft char *dirname;
519 1.1 cgd
520 1.1 cgd if (!*str)
521 1.2 mycroft strcpy(buf, ".");
522 1.2 mycroft else
523 1.2 mycroft g_Ctoc(str, buf);
524 1.2 mycroft if (pglob->gl_flags & GLOB_ALTDIRFUNC)
525 1.2 mycroft return((*pglob->gl_opendir)(buf));
526 1.1 cgd return(opendir(buf));
527 1.1 cgd }
528 1.1 cgd
529 1.1 cgd static int
530 1.2 mycroft g_lstat(fn, sb, pglob)
531 1.1 cgd register Char *fn;
532 1.1 cgd struct stat *sb;
533 1.2 mycroft glob_t *pglob;
534 1.1 cgd {
535 1.1 cgd char buf[MAXPATHLEN];
536 1.1 cgd
537 1.1 cgd g_Ctoc(fn, buf);
538 1.2 mycroft if (pglob->gl_flags & GLOB_ALTDIRFUNC)
539 1.2 mycroft return((*pglob->gl_lstat)(buf, sb));
540 1.1 cgd return(lstat(buf, sb));
541 1.1 cgd }
542 1.1 cgd
543 1.1 cgd static int
544 1.2 mycroft g_stat(fn, sb, pglob)
545 1.1 cgd register Char *fn;
546 1.1 cgd struct stat *sb;
547 1.2 mycroft glob_t *pglob;
548 1.1 cgd {
549 1.1 cgd char buf[MAXPATHLEN];
550 1.1 cgd
551 1.1 cgd g_Ctoc(fn, buf);
552 1.2 mycroft if (pglob->gl_flags & GLOB_ALTDIRFUNC)
553 1.2 mycroft return((*pglob->gl_stat)(buf, sb));
554 1.1 cgd return(stat(buf, sb));
555 1.1 cgd }
556 1.1 cgd
557 1.1 cgd static Char *
558 1.1 cgd g_strchr(str, ch)
559 1.1 cgd Char *str;
560 1.1 cgd int ch;
561 1.1 cgd {
562 1.1 cgd do {
563 1.1 cgd if (*str == ch)
564 1.1 cgd return (str);
565 1.1 cgd } while (*str++);
566 1.1 cgd return (NULL);
567 1.1 cgd }
568 1.1 cgd
569 1.1 cgd static void
570 1.1 cgd g_Ctoc(str, buf)
571 1.1 cgd register Char *str;
572 1.1 cgd char *buf;
573 1.1 cgd {
574 1.1 cgd register char *dc;
575 1.1 cgd
576 1.1 cgd for (dc = buf; *dc++ = *str++;);
577 1.1 cgd }
578 1.1 cgd
579 1.1 cgd #ifdef DEBUG
580 1.1 cgd static void
581 1.1 cgd qprintf(s)
582 1.1 cgd register Char *s;
583 1.1 cgd {
584 1.1 cgd register Char *p;
585 1.1 cgd
586 1.1 cgd for (p = s; *p; p++)
587 1.2 mycroft (void)printf("%c", CHAR(*p));
588 1.1 cgd (void)printf("\n");
589 1.1 cgd for (p = s; *p; p++)
590 1.1 cgd (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
591 1.1 cgd (void)printf("\n");
592 1.1 cgd for (p = s; *p; p++)
593 1.2 mycroft (void)printf("%c", ismeta(*p) ? '_' : ' ');
594 1.1 cgd (void)printf("\n");
595 1.1 cgd }
596 1.1 cgd #endif
597