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