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