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