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