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