Annotation of src/lib/libc/gen/glob.c, Revision 1.5.4.2
1.5.4.2 ! jtc 1: /* $NetBSD: glob.c,v 1.5.4.1 1996/09/16 18:40:29 jtc Exp $ */
1.5 cgd 2:
1.1 cgd 3: /*
1.4 cgd 4: * Copyright (c) 1989, 1993
5: * The Regents of the University of California. All rights reserved.
1.1 cgd 6: *
7: * This code is derived from software contributed to Berkeley by
8: * Guido van Rossum.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: * 3. All advertising materials mentioning features or use of this software
19: * must display the following acknowledgement:
20: * This product includes software developed by the University of
21: * California, Berkeley and its contributors.
22: * 4. Neither the name of the University nor the names of its contributors
23: * may be used to endorse or promote products derived from this software
24: * without specific prior written permission.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36: * SUCH DAMAGE.
37: */
38:
39: #if defined(LIBC_SCCS) && !defined(lint)
1.5 cgd 40: #if 0
41: static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93";
42: #else
1.5.4.2 ! jtc 43: static char rcsid[] = "$NetBSD: glob.c,v 1.5.4.1 1996/09/16 18:40:29 jtc Exp $";
1.5 cgd 44: #endif
1.1 cgd 45: #endif /* LIBC_SCCS and not lint */
46:
47: /*
48: * glob(3) -- a superset of the one defined in POSIX 1003.2.
49: *
50: * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
51: *
52: * Optional extra services, controlled by flags not defined by POSIX:
53: *
54: * GLOB_QUOTE:
55: * Escaping convention: \ inhibits any special meaning the following
56: * character might have (except \ at end of string is retained).
57: * GLOB_MAGCHAR:
58: * Set in gl_flags if pattern contained a globbing character.
1.2 mycroft 59: * GLOB_NOMAGIC:
60: * Same as GLOB_NOCHECK, but it will only append pattern if it did
61: * not contain any magic characters. [Used in csh style globbing]
62: * GLOB_ALTDIRFUNC:
63: * Use alternately specified directory access functions.
1.4 cgd 64: * GLOB_TILDE:
65: * expand ~user/foo to the /home/dir/of/user/foo
66: * GLOB_BRACE:
67: * expand {1,2}{a,b} to 1a 1b 2a 2b
1.1 cgd 68: * gl_matchc:
69: * Number of matches in the current invocation of glob.
70: */
71:
1.5.4.1 jtc 72: #include "namespace.h"
1.1 cgd 73: #include <sys/param.h>
74: #include <sys/stat.h>
1.4 cgd 75:
76: #include <ctype.h>
1.1 cgd 77: #include <dirent.h>
1.4 cgd 78: #include <errno.h>
1.1 cgd 79: #include <glob.h>
1.4 cgd 80: #include <pwd.h>
1.1 cgd 81: #include <stdio.h>
82: #include <stdlib.h>
1.4 cgd 83: #include <string.h>
84: #include <unistd.h>
1.5.4.2 ! jtc 85:
! 86: #ifdef __weak_alias
! 87: __weak_alias(glob,_glob);
! 88: __weak_alias(globfree,_globfree);
! 89: #endif
1.1 cgd 90:
91: #define DOLLAR '$'
92: #define DOT '.'
93: #define EOS '\0'
94: #define LBRACKET '['
95: #define NOT '!'
96: #define QUESTION '?'
97: #define QUOTE '\\'
98: #define RANGE '-'
99: #define RBRACKET ']'
100: #define SEP '/'
101: #define STAR '*'
102: #define TILDE '~'
103: #define UNDERSCORE '_'
1.4 cgd 104: #define LBRACE '{'
105: #define RBRACE '}'
106: #define SLASH '/'
107: #define COMMA ','
108:
109: #ifndef DEBUG
1.1 cgd 110:
111: #define M_QUOTE 0x8000
112: #define M_PROTECT 0x4000
113: #define M_MASK 0xffff
114: #define M_ASCII 0x00ff
115:
1.4 cgd 116: typedef u_short Char;
117:
118: #else
119:
120: #define M_QUOTE 0x80
121: #define M_PROTECT 0x40
122: #define M_MASK 0xff
123: #define M_ASCII 0x7f
124:
125: typedef char Char;
126:
127: #endif
128:
129:
130: #define CHAR(c) ((Char)((c)&M_ASCII))
131: #define META(c) ((Char)((c)|M_QUOTE))
1.1 cgd 132: #define M_ALL META('*')
133: #define M_END META(']')
134: #define M_NOT META('!')
135: #define M_ONE META('?')
136: #define M_RNG META('-')
137: #define M_SET META('[')
138: #define ismeta(c) (((c)&M_QUOTE) != 0)
139:
140:
141: static int compare __P((const void *, const void *));
1.4 cgd 142: static void g_Ctoc __P((const Char *, char *));
1.2 mycroft 143: static int g_lstat __P((Char *, struct stat *, glob_t *));
144: static DIR *g_opendir __P((Char *, glob_t *));
1.1 cgd 145: static Char *g_strchr __P((Char *, int));
1.4 cgd 146: #ifdef notdef
147: static Char *g_strcat __P((Char *, const Char *));
148: #endif
1.2 mycroft 149: static int g_stat __P((Char *, struct stat *, glob_t *));
1.4 cgd 150: static int glob0 __P((const Char *, glob_t *));
1.1 cgd 151: static int glob1 __P((Char *, glob_t *));
152: static int glob2 __P((Char *, Char *, Char *, glob_t *));
153: static int glob3 __P((Char *, Char *, Char *, Char *, glob_t *));
1.4 cgd 154: static int globextend __P((const Char *, glob_t *));
155: static const Char * globtilde __P((const Char *, Char *, glob_t *));
156: static int globexp1 __P((const Char *, glob_t *));
157: static int globexp2 __P((const Char *, const Char *, glob_t *, int *));
1.1 cgd 158: static int match __P((Char *, Char *, Char *));
159: #ifdef DEBUG
1.4 cgd 160: static void qprintf __P((const char *, Char *));
1.1 cgd 161: #endif
162:
1.4 cgd 163: int
1.1 cgd 164: glob(pattern, flags, errfunc, pglob)
165: const char *pattern;
1.4 cgd 166: int flags, (*errfunc) __P((const char *, int));
1.1 cgd 167: glob_t *pglob;
168: {
1.4 cgd 169: const u_char *patnext;
170: int c;
171: Char *bufnext, *bufend, patbuf[MAXPATHLEN+1];
1.1 cgd 172:
173: patnext = (u_char *) pattern;
174: if (!(flags & GLOB_APPEND)) {
175: pglob->gl_pathc = 0;
176: pglob->gl_pathv = NULL;
177: if (!(flags & GLOB_DOOFFS))
178: pglob->gl_offs = 0;
179: }
180: pglob->gl_flags = flags & ~GLOB_MAGCHAR;
181: pglob->gl_errfunc = errfunc;
182: pglob->gl_matchc = 0;
183:
184: bufnext = patbuf;
185: bufend = bufnext + MAXPATHLEN;
186: if (flags & GLOB_QUOTE) {
187: /* Protect the quoted characters. */
188: while (bufnext < bufend && (c = *patnext++) != EOS)
189: if (c == QUOTE) {
190: if ((c = *patnext++) == EOS) {
191: c = QUOTE;
192: --patnext;
193: }
194: *bufnext++ = c | M_PROTECT;
195: }
196: else
197: *bufnext++ = c;
198: }
199: else
200: while (bufnext < bufend && (c = *patnext++) != EOS)
201: *bufnext++ = c;
202: *bufnext = EOS;
203:
1.4 cgd 204: if (flags & GLOB_BRACE)
205: return globexp1(patbuf, pglob);
206: else
207: return glob0(patbuf, pglob);
208: }
209:
210: /*
211: * Expand recursively a glob {} pattern. When there is no more expansion
212: * invoke the standard globbing routine to glob the rest of the magic
213: * characters
214: */
215: static int globexp1(pattern, pglob)
216: const Char *pattern;
217: glob_t *pglob;
218: {
219: const Char* ptr = pattern;
220: int rv;
221:
222: /* Protect a single {}, for find(1), like csh */
223: if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
224: return glob0(pattern, pglob);
225:
226: while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL)
227: if (!globexp2(ptr, pattern, pglob, &rv))
228: return rv;
229:
230: return glob0(pattern, pglob);
231: }
232:
233:
234: /*
235: * Recursive brace globbing helper. Tries to expand a single brace.
236: * If it succeeds then it invokes globexp1 with the new pattern.
237: * If it fails then it tries to glob the rest of the pattern and returns.
238: */
239: static int globexp2(ptr, pattern, pglob, rv)
240: const Char *ptr, *pattern;
241: glob_t *pglob;
242: int *rv;
243: {
244: int i;
245: Char *lm, *ls;
246: const Char *pe, *pm, *pl;
247: Char patbuf[MAXPATHLEN + 1];
248:
249: /* copy part up to the brace */
250: for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
251: continue;
252: ls = lm;
253:
254: /* Find the balanced brace */
255: for (i = 0, pe = ++ptr; *pe; pe++)
256: if (*pe == LBRACKET) {
257: /* Ignore everything between [] */
258: for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
259: continue;
260: if (*pe == EOS) {
261: /*
262: * We could not find a matching RBRACKET.
263: * Ignore and just look for RBRACE
264: */
265: pe = pm;
266: }
267: }
268: else if (*pe == LBRACE)
269: i++;
270: else if (*pe == RBRACE) {
271: if (i == 0)
272: break;
273: i--;
274: }
275:
276: /* Non matching braces; just glob the pattern */
277: if (i != 0 || *pe == EOS) {
278: *rv = glob0(patbuf, pglob);
279: return 0;
280: }
281:
282: for (i = 0, pl = pm = ptr; pm <= pe; pm++)
283: switch (*pm) {
284: case LBRACKET:
285: /* Ignore everything between [] */
286: for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)
287: continue;
288: if (*pm == EOS) {
289: /*
290: * We could not find a matching RBRACKET.
291: * Ignore and just look for RBRACE
292: */
293: pm = pl;
294: }
295: break;
296:
297: case LBRACE:
298: i++;
299: break;
300:
301: case RBRACE:
302: if (i) {
303: i--;
304: break;
305: }
306: /* FALLTHROUGH */
307: case COMMA:
308: if (i && *pm == COMMA)
309: break;
310: else {
311: /* Append the current string */
312: for (lm = ls; (pl < pm); *lm++ = *pl++)
313: continue;
314: /*
315: * Append the rest of the pattern after the
316: * closing brace
317: */
318: for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
319: continue;
320:
321: /* Expand the current pattern */
322: #ifdef DEBUG
323: qprintf("globexp2:", patbuf);
324: #endif
325: *rv = globexp1(patbuf, pglob);
326:
327: /* move after the comma, to the next string */
328: pl = pm + 1;
329: }
330: break;
331:
332: default:
333: break;
334: }
335: *rv = 0;
336: return 0;
337: }
338:
339:
340:
341: /*
342: * expand tilde from the passwd file.
343: */
344: static const Char *
345: globtilde(pattern, patbuf, pglob)
346: const Char *pattern;
347: Char *patbuf;
348: glob_t *pglob;
349: {
350: struct passwd *pwd;
351: char *h;
352: const Char *p;
353: Char *b;
354:
355: if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
356: return pattern;
357:
358: /* Copy up to the end of the string or / */
359: for (p = pattern + 1, h = (char *) patbuf; *p && *p != SLASH;
360: *h++ = *p++)
361: continue;
362:
363: *h = EOS;
364:
365: if (((char *) patbuf)[0] == EOS) {
366: /*
367: * handle a plain ~ or ~/ by expanding $HOME
368: * first and then trying the password file
369: */
370: if ((h = getenv("HOME")) == NULL) {
371: if ((pwd = getpwuid(getuid())) == NULL)
372: return pattern;
373: else
374: h = pwd->pw_dir;
375: }
376: }
377: else {
378: /*
379: * Expand a ~user
380: */
381: if ((pwd = getpwnam((char*) patbuf)) == NULL)
382: return pattern;
383: else
384: h = pwd->pw_dir;
385: }
386:
387: /* Copy the home directory */
388: for (b = patbuf; *h; *b++ = *h++)
389: continue;
390:
391: /* Append the rest of the pattern */
392: while ((*b++ = *p++) != EOS)
393: continue;
394:
395: return patbuf;
396: }
397:
398:
399: /*
400: * The main glob() routine: compiles the pattern (optionally processing
401: * quotes), calls glob1() to do the real pattern matching, and finally
402: * sorts the list (unless unsorted operation is requested). Returns 0
403: * if things went well, nonzero if errors occurred. It is not an error
404: * to find no matches.
405: */
406: static int
407: glob0(pattern, pglob)
408: const Char *pattern;
409: glob_t *pglob;
410: {
411: const Char *qpatnext;
412: int c, err, oldpathc;
413: Char *bufnext, patbuf[MAXPATHLEN+1];
414:
415: qpatnext = globtilde(pattern, patbuf, pglob);
416: oldpathc = pglob->gl_pathc;
1.1 cgd 417: bufnext = patbuf;
1.4 cgd 418:
1.1 cgd 419: /* We don't need to check for buffer overflow any more. */
420: while ((c = *qpatnext++) != EOS) {
421: switch (c) {
422: case LBRACKET:
423: c = *qpatnext;
424: if (c == NOT)
425: ++qpatnext;
426: if (*qpatnext == EOS ||
1.4 cgd 427: g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) {
1.1 cgd 428: *bufnext++ = LBRACKET;
429: if (c == NOT)
430: --qpatnext;
431: break;
432: }
433: *bufnext++ = M_SET;
434: if (c == NOT)
435: *bufnext++ = M_NOT;
436: c = *qpatnext++;
437: do {
438: *bufnext++ = CHAR(c);
439: if (*qpatnext == RANGE &&
440: (c = qpatnext[1]) != RBRACKET) {
441: *bufnext++ = M_RNG;
442: *bufnext++ = CHAR(c);
443: qpatnext += 2;
444: }
445: } while ((c = *qpatnext++) != RBRACKET);
1.2 mycroft 446: pglob->gl_flags |= GLOB_MAGCHAR;
1.1 cgd 447: *bufnext++ = M_END;
448: break;
449: case QUESTION:
450: pglob->gl_flags |= GLOB_MAGCHAR;
451: *bufnext++ = M_ONE;
452: break;
453: case STAR:
454: pglob->gl_flags |= GLOB_MAGCHAR;
1.2 mycroft 455: /* collapse adjacent stars to one,
456: * to avoid exponential behavior
457: */
458: if (bufnext == patbuf || bufnext[-1] != M_ALL)
459: *bufnext++ = M_ALL;
1.1 cgd 460: break;
461: default:
462: *bufnext++ = CHAR(c);
463: break;
464: }
465: }
466: *bufnext = EOS;
467: #ifdef DEBUG
1.4 cgd 468: qprintf("glob0:", patbuf);
1.1 cgd 469: #endif
470:
471: if ((err = glob1(patbuf, pglob)) != 0)
472: return(err);
473:
1.2 mycroft 474: /*
475: * If there was no match we are going to append the pattern
476: * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
477: * and the pattern did not contain any magic characters
478: * GLOB_NOMAGIC is there just for compatibility with csh.
479: */
480: if (pglob->gl_pathc == oldpathc &&
1.4 cgd 481: ((pglob->gl_flags & GLOB_NOCHECK) ||
482: ((pglob->gl_flags & GLOB_NOMAGIC) &&
483: !(pglob->gl_flags & GLOB_MAGCHAR))))
484: return(globextend(pattern, pglob));
485: else if (!(pglob->gl_flags & GLOB_NOSORT))
1.1 cgd 486: qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
487: pglob->gl_pathc - oldpathc, sizeof(char *), compare);
488: return(0);
489: }
490:
491: static int
492: compare(p, q)
493: const void *p, *q;
494: {
495: return(strcmp(*(char **)p, *(char **)q));
496: }
497:
1.4 cgd 498: static int
1.1 cgd 499: glob1(pattern, pglob)
500: Char *pattern;
501: glob_t *pglob;
502: {
503: Char pathbuf[MAXPATHLEN+1];
504:
505: /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
506: if (*pattern == EOS)
507: return(0);
508: return(glob2(pathbuf, pathbuf, pattern, pglob));
509: }
510:
511: /*
512: * The functions glob2 and glob3 are mutually recursive; there is one level
513: * of recursion for each segment in the pattern that contains one or more
514: * meta characters.
515: */
1.4 cgd 516: static int
1.1 cgd 517: glob2(pathbuf, pathend, pattern, pglob)
518: Char *pathbuf, *pathend, *pattern;
519: glob_t *pglob;
520: {
521: struct stat sb;
522: Char *p, *q;
523: int anymeta;
524:
525: /*
526: * Loop over pattern segments until end of pattern or until
527: * segment with meta character found.
528: */
529: for (anymeta = 0;;) {
530: if (*pattern == EOS) { /* End of pattern? */
531: *pathend = EOS;
1.2 mycroft 532: if (g_lstat(pathbuf, &sb, pglob))
1.1 cgd 533: return(0);
534:
535: if (((pglob->gl_flags & GLOB_MARK) &&
536: pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
537: || (S_ISLNK(sb.st_mode) &&
1.2 mycroft 538: (g_stat(pathbuf, &sb, pglob) == 0) &&
1.1 cgd 539: S_ISDIR(sb.st_mode)))) {
540: *pathend++ = SEP;
541: *pathend = EOS;
542: }
543: ++pglob->gl_matchc;
544: return(globextend(pathbuf, pglob));
545: }
546:
547: /* Find end of next segment, copy tentatively to pathend. */
548: q = pathend;
549: p = pattern;
550: while (*p != EOS && *p != SEP) {
551: if (ismeta(*p))
552: anymeta = 1;
553: *q++ = *p++;
554: }
555:
556: if (!anymeta) { /* No expansion, do next segment. */
557: pathend = q;
558: pattern = p;
559: while (*pattern == SEP)
560: *pathend++ = *pattern++;
561: } else /* Need expansion, recurse. */
562: return(glob3(pathbuf, pathend, pattern, p, pglob));
563: }
564: /* NOTREACHED */
565: }
566:
1.4 cgd 567: static int
1.1 cgd 568: glob3(pathbuf, pathend, pattern, restpattern, pglob)
569: Char *pathbuf, *pathend, *pattern, *restpattern;
570: glob_t *pglob;
571: {
572: register struct dirent *dp;
573: DIR *dirp;
1.4 cgd 574: int err;
1.2 mycroft 575: char buf[MAXPATHLEN];
1.1 cgd 576:
1.4 cgd 577: /*
578: * The readdirfunc declaration can't be prototyped, because it is
579: * assigned, below, to two functions which are prototyped in glob.h
580: * and dirent.h as taking pointers to differently typed opaque
581: * structures.
582: */
583: struct dirent *(*readdirfunc)();
584:
1.1 cgd 585: *pathend = EOS;
586: errno = 0;
587:
1.2 mycroft 588: if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
1.1 cgd 589: /* TODO: don't call for ENOENT or ENOTDIR? */
1.2 mycroft 590: if (pglob->gl_errfunc) {
591: g_Ctoc(pathbuf, buf);
592: if (pglob->gl_errfunc(buf, errno) ||
593: pglob->gl_flags & GLOB_ERR)
594: return (GLOB_ABEND);
595: }
596: return(0);
597: }
1.1 cgd 598:
599: err = 0;
600:
601: /* Search directory for matching names. */
1.2 mycroft 602: if (pglob->gl_flags & GLOB_ALTDIRFUNC)
603: readdirfunc = pglob->gl_readdir;
604: else
605: readdirfunc = readdir;
606: while ((dp = (*readdirfunc)(dirp))) {
1.1 cgd 607: register u_char *sc;
608: register Char *dc;
609:
610: /* Initial DOT must be matched literally. */
611: if (dp->d_name[0] == DOT && *pattern != DOT)
612: continue;
613: for (sc = (u_char *) dp->d_name, dc = pathend;
1.4 cgd 614: (*dc++ = *sc++) != EOS;)
615: continue;
1.1 cgd 616: if (!match(pathend, pattern, restpattern)) {
617: *pathend = EOS;
618: continue;
619: }
620: err = glob2(pathbuf, --dc, restpattern, pglob);
621: if (err)
622: break;
623: }
624:
1.2 mycroft 625: if (pglob->gl_flags & GLOB_ALTDIRFUNC)
626: (*pglob->gl_closedir)(dirp);
627: else
628: closedir(dirp);
1.1 cgd 629: return(err);
630: }
631:
632:
633: /*
634: * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
635: * add the new item, and update gl_pathc.
636: *
637: * This assumes the BSD realloc, which only copies the block when its size
638: * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
639: * behavior.
640: *
641: * Return 0 if new item added, error code if memory couldn't be allocated.
642: *
643: * Invariant of the glob_t structure:
644: * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
645: * gl_pathv points to (gl_offs + gl_pathc + 1) items.
646: */
647: static int
648: globextend(path, pglob)
1.4 cgd 649: const Char *path;
1.1 cgd 650: glob_t *pglob;
651: {
652: register char **pathv;
653: register int i;
654: u_int newsize;
655: char *copy;
1.4 cgd 656: const Char *p;
1.1 cgd 657:
658: newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
1.4 cgd 659: pathv = pglob->gl_pathv ?
660: realloc((char *)pglob->gl_pathv, newsize) :
661: malloc(newsize);
1.1 cgd 662: if (pathv == NULL)
663: return(GLOB_NOSPACE);
664:
665: if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
666: /* first time around -- clear initial gl_offs items */
667: pathv += pglob->gl_offs;
668: for (i = pglob->gl_offs; --i >= 0; )
669: *--pathv = NULL;
670: }
671: pglob->gl_pathv = pathv;
672:
1.4 cgd 673: for (p = path; *p++;)
674: continue;
1.1 cgd 675: if ((copy = malloc(p - path)) != NULL) {
676: g_Ctoc(path, copy);
677: pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
678: }
679: pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
680: return(copy == NULL ? GLOB_NOSPACE : 0);
681: }
682:
683:
684: /*
685: * pattern matching function for filenames. Each occurrence of the *
686: * pattern causes a recursion level.
687: */
1.4 cgd 688: static int
1.1 cgd 689: match(name, pat, patend)
690: register Char *name, *pat, *patend;
691: {
692: int ok, negate_range;
693: Char c, k;
694:
695: while (pat < patend) {
696: c = *pat++;
697: switch (c & M_MASK) {
698: case M_ALL:
699: if (pat == patend)
700: return(1);
1.2 mycroft 701: do
702: if (match(name, pat, patend))
703: return(1);
704: while (*name++ != EOS);
1.1 cgd 705: return(0);
706: case M_ONE:
707: if (*name++ == EOS)
708: return(0);
709: break;
710: case M_SET:
711: ok = 0;
1.2 mycroft 712: if ((k = *name++) == EOS)
713: return(0);
1.4 cgd 714: if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
1.1 cgd 715: ++pat;
716: while (((c = *pat++) & M_MASK) != M_END)
717: if ((*pat & M_MASK) == M_RNG) {
718: if (c <= k && k <= pat[1])
719: ok = 1;
720: pat += 2;
721: } else if (c == k)
722: ok = 1;
723: if (ok == negate_range)
724: return(0);
725: break;
726: default:
727: if (*name++ != c)
728: return(0);
729: break;
730: }
731: }
732: return(*name == EOS);
733: }
734:
735: /* Free allocated data belonging to a glob_t structure. */
736: void
737: globfree(pglob)
738: glob_t *pglob;
739: {
740: register int i;
741: register char **pp;
742:
743: if (pglob->gl_pathv != NULL) {
744: pp = pglob->gl_pathv + pglob->gl_offs;
745: for (i = pglob->gl_pathc; i--; ++pp)
746: if (*pp)
747: free(*pp);
748: free(pglob->gl_pathv);
749: }
750: }
751:
752: static DIR *
1.2 mycroft 753: g_opendir(str, pglob)
1.1 cgd 754: register Char *str;
1.2 mycroft 755: glob_t *pglob;
1.1 cgd 756: {
757: char buf[MAXPATHLEN];
758:
759: if (!*str)
1.2 mycroft 760: strcpy(buf, ".");
761: else
762: g_Ctoc(str, buf);
1.4 cgd 763:
1.2 mycroft 764: if (pglob->gl_flags & GLOB_ALTDIRFUNC)
765: return((*pglob->gl_opendir)(buf));
1.4 cgd 766:
1.1 cgd 767: return(opendir(buf));
768: }
769:
770: static int
1.2 mycroft 771: g_lstat(fn, sb, pglob)
1.1 cgd 772: register Char *fn;
773: struct stat *sb;
1.2 mycroft 774: glob_t *pglob;
1.1 cgd 775: {
776: char buf[MAXPATHLEN];
777:
778: g_Ctoc(fn, buf);
1.2 mycroft 779: if (pglob->gl_flags & GLOB_ALTDIRFUNC)
780: return((*pglob->gl_lstat)(buf, sb));
1.1 cgd 781: return(lstat(buf, sb));
782: }
783:
784: static int
1.2 mycroft 785: g_stat(fn, sb, pglob)
1.1 cgd 786: register Char *fn;
787: struct stat *sb;
1.2 mycroft 788: glob_t *pglob;
1.1 cgd 789: {
790: char buf[MAXPATHLEN];
791:
792: g_Ctoc(fn, buf);
1.2 mycroft 793: if (pglob->gl_flags & GLOB_ALTDIRFUNC)
794: return((*pglob->gl_stat)(buf, sb));
1.1 cgd 795: return(stat(buf, sb));
796: }
797:
798: static Char *
799: g_strchr(str, ch)
800: Char *str;
801: int ch;
802: {
803: do {
804: if (*str == ch)
805: return (str);
806: } while (*str++);
807: return (NULL);
808: }
809:
1.4 cgd 810: #ifdef notdef
811: static Char *
812: g_strcat(dst, src)
813: Char *dst;
814: const Char* src;
815: {
816: Char *sdst = dst;
817:
818: while (*dst++)
819: continue;
820: --dst;
821: while((*dst++ = *src++) != EOS)
822: continue;
823:
824: return (sdst);
825: }
826: #endif
827:
1.1 cgd 828: static void
829: g_Ctoc(str, buf)
1.4 cgd 830: register const Char *str;
1.1 cgd 831: char *buf;
832: {
833: register char *dc;
834:
1.4 cgd 835: for (dc = buf; (*dc++ = *str++) != EOS;)
836: continue;
1.1 cgd 837: }
838:
839: #ifdef DEBUG
840: static void
1.4 cgd 841: qprintf(str, s)
842: const char *str;
1.1 cgd 843: register Char *s;
844: {
845: register Char *p;
846:
1.4 cgd 847: (void)printf("%s:\n", str);
1.1 cgd 848: for (p = s; *p; p++)
1.2 mycroft 849: (void)printf("%c", CHAR(*p));
1.1 cgd 850: (void)printf("\n");
851: for (p = s; *p; p++)
852: (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
853: (void)printf("\n");
854: for (p = s; *p; p++)
1.2 mycroft 855: (void)printf("%c", ismeta(*p) ? '_' : ' ');
1.1 cgd 856: (void)printf("\n");
857: }
858: #endif
CVSweb <webmaster@jp.NetBSD.org>