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