]> andersk Git - openssh.git/blob - openbsd-compat/glob.c
- (djm) [acss.c auth-krb5.c auth-options.c auth-pam.c auth-shadow.c]
[openssh.git] / openbsd-compat / glob.c
1 /*      $OpenBSD: glob.c,v 1.25 2005/08/08 08:05:34 espie Exp $ */
2 /*
3  * Copyright (c) 1989, 1993
4  *      The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Guido van Rossum.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 /* OPENBSD ORIGINAL: lib/libc/gen/glob.c */
35
36 #include "includes.h"
37
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <dirent.h>
41 #include <ctype.h>
42 #include <errno.h>
43 #include <pwd.h>
44 #include <string.h>
45 #include <unistd.h>
46
47 static long
48 get_arg_max(void)
49 {
50 #ifdef ARG_MAX
51         return(ARG_MAX);
52 #elif defined(HAVE_SYSCONF) && defined(_SC_ARG_MAX)
53         return(sysconf(_SC_ARG_MAX));
54 #else
55         return(256); /* XXX: arbitrary */
56 #endif
57 }
58
59 #if !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) || \
60     !defined(GLOB_HAS_GL_MATCHC)
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
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 #undef TILDE                    /* Some platforms may already define it */
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
114 typedef 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
123 typedef 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
139 static int       compare(const void *, const void *);
140 static int       g_Ctoc(const Char *, char *, u_int);
141 static int       g_lstat(Char *, struct stat *, glob_t *);
142 static DIR      *g_opendir(Char *, glob_t *);
143 static Char     *g_strchr(Char *, int);
144 static int       g_stat(Char *, struct stat *, glob_t *);
145 static int       glob0(const Char *, glob_t *);
146 static int       glob1(Char *, Char *, glob_t *, size_t *);
147 static int       glob2(Char *, Char *, Char *, Char *, Char *, Char *,
148                     glob_t *, size_t *);
149 static int       glob3(Char *, Char *, Char *, Char *, Char *, Char *,
150                     Char *, Char *, glob_t *, size_t *);
151 static int       globextend(const Char *, glob_t *, size_t *);
152 static const Char *
153                  globtilde(const Char *, Char *, size_t, glob_t *);
154 static int       globexp1(const Char *, glob_t *);
155 static int       globexp2(const Char *, const Char *, glob_t *, int *);
156 static int       match(Char *, Char *, Char *);
157 #ifdef DEBUG
158 static void      qprintf(const char *, Char *);
159 #endif
160
161 int
162 glob(const char *pattern, int flags, int (*errfunc)(const char *, int),
163     glob_t *pglob)
164 {
165         const u_char *patnext;
166         int c;
167         Char *bufnext, *bufend, patbuf[MAXPATHLEN];
168
169         patnext = (u_char *) pattern;
170         if (!(flags & GLOB_APPEND)) {
171                 pglob->gl_pathc = 0;
172                 pglob->gl_pathv = NULL;
173                 if (!(flags & GLOB_DOOFFS))
174                         pglob->gl_offs = 0;
175         }
176         pglob->gl_flags = flags & ~GLOB_MAGCHAR;
177         pglob->gl_errfunc = errfunc;
178         pglob->gl_matchc = 0;
179
180         bufnext = patbuf;
181         bufend = bufnext + MAXPATHLEN - 1;
182         if (flags & GLOB_NOESCAPE)
183                 while (bufnext < bufend && (c = *patnext++) != EOS)
184                         *bufnext++ = c;
185         else {
186                 /* Protect the quoted characters. */
187                 while (bufnext < bufend && (c = *patnext++) != EOS)
188                         if (c == QUOTE) {
189                                 if ((c = *patnext++) == EOS) {
190                                         c = QUOTE;
191                                         --patnext;
192                                 }
193                                 *bufnext++ = c | M_PROTECT;
194                         } else
195                                 *bufnext++ = c;
196         }
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  */
210 static int
211 globexp1(const Char *pattern, glob_t *pglob)
212 {
213         const Char* ptr = pattern;
214         int rv;
215
216         /* Protect a single {}, for find(1), like csh */
217         if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
218                 return glob0(pattern, pglob);
219
220         while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL)
221                 if (!globexp2(ptr, pattern, pglob, &rv))
222                         return rv;
223
224         return glob0(pattern, pglob);
225 }
226
227
228 /*
229  * Recursive brace globbing helper. Tries to expand a single brace.
230  * If it succeeds then it invokes globexp1 with the new pattern.
231  * If it fails then it tries to glob the rest of the pattern and returns.
232  */
233 static int
234 globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, int *rv)
235 {
236         int     i;
237         Char   *lm, *ls;
238         const Char *pe, *pm, *pl;
239         Char    patbuf[MAXPATHLEN];
240
241         /* copy part up to the brace */
242         for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
243                 ;
244         *lm = EOS;
245         ls = lm;
246
247         /* Find the balanced brace */
248         for (i = 0, pe = ++ptr; *pe; pe++)
249                 if (*pe == LBRACKET) {
250                         /* Ignore everything between [] */
251                         for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
252                                 ;
253                         if (*pe == EOS) {
254                                 /*
255                                  * We could not find a matching RBRACKET.
256                                  * Ignore and just look for RBRACE
257                                  */
258                                 pe = pm;
259                         }
260                 } else if (*pe == LBRACE)
261                         i++;
262                 else if (*pe == RBRACE) {
263                         if (i == 0)
264                                 break;
265                         i--;
266                 }
267
268         /* Non matching braces; just glob the pattern */
269         if (i != 0 || *pe == EOS) {
270                 *rv = glob0(patbuf, pglob);
271                 return 0;
272         }
273
274         for (i = 0, pl = pm = ptr; pm <= pe; pm++) {
275                 switch (*pm) {
276                 case LBRACKET:
277                         /* Ignore everything between [] */
278                         for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)
279                                 ;
280                         if (*pm == EOS) {
281                                 /*
282                                  * We could not find a matching RBRACKET.
283                                  * Ignore and just look for RBRACE
284                                  */
285                                 pm = pl;
286                         }
287                         break;
288
289                 case LBRACE:
290                         i++;
291                         break;
292
293                 case RBRACE:
294                         if (i) {
295                                 i--;
296                                 break;
297                         }
298                         /* FALLTHROUGH */
299                 case COMMA:
300                         if (i && *pm == COMMA)
301                                 break;
302                         else {
303                                 /* Append the current string */
304                                 for (lm = ls; (pl < pm); *lm++ = *pl++)
305                                         ;
306
307                                 /*
308                                  * Append the rest of the pattern after the
309                                  * closing brace
310                                  */
311                                 for (pl = pe + 1; (*lm++ = *pl++) != EOS; )
312                                         ;
313
314                                 /* Expand the current pattern */
315 #ifdef DEBUG
316                                 qprintf("globexp2:", patbuf);
317 #endif
318                                 *rv = globexp1(patbuf, pglob);
319
320                                 /* move after the comma, to the next string */
321                                 pl = pm + 1;
322                         }
323                         break;
324
325                 default:
326                         break;
327                 }
328         }
329         *rv = 0;
330         return 0;
331 }
332
333
334
335 /*
336  * expand tilde from the passwd file.
337  */
338 static const Char *
339 globtilde(const Char *pattern, Char *patbuf, size_t patbuf_len, glob_t *pglob)
340 {
341         struct passwd *pwd;
342         char *h;
343         const Char *p;
344         Char *b, *eb;
345
346         if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
347                 return pattern;
348
349         /* Copy up to the end of the string or / */
350         eb = &patbuf[patbuf_len - 1];
351         for (p = pattern + 1, h = (char *) patbuf;
352             h < (char *)eb && *p && *p != SLASH; *h++ = *p++)
353                 ;
354
355         *h = EOS;
356
357 #if 0
358         if (h == (char *)eb)
359                 return what;
360 #endif
361
362         if (((char *) patbuf)[0] == EOS) {
363                 /*
364                  * handle a plain ~ or ~/ by expanding $HOME
365                  * first and then trying the password file
366                  */
367 #if 0
368                 if (issetugid() != 0 || (h = getenv("HOME")) == NULL) {
369 #endif
370                 if ((getuid() != geteuid()) || (h = getenv("HOME")) == NULL) {
371                         if ((pwd = getpwuid(getuid())) == NULL)
372                                 return pattern;
373                         else
374                                 h = pwd->pw_dir;
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 */
387         for (b = patbuf; b < eb && *h; *b++ = *h++)
388                 ;
389
390         /* Append the rest of the pattern */
391         while (b < eb && (*b++ = *p++) != EOS)
392                 ;
393         *b = EOS;
394
395         return patbuf;
396 }
397
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  */
406 static int
407 glob0(const Char *pattern, glob_t *pglob)
408 {
409         const Char *qpatnext;
410         int c, err, oldpathc;
411         Char *bufnext, patbuf[MAXPATHLEN];
412         size_t limit = 0;
413
414         qpatnext = globtilde(pattern, patbuf, MAXPATHLEN, pglob);
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;
454                         /* collapse adjacent stars to one,
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, patbuf+MAXPATHLEN-1, pglob, &limit)) != 0)
471                 return(err);
472
473         /*
474          * If there was no match we are going to append the pattern
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          */
479         if (pglob->gl_pathc == oldpathc) {
480                 if ((pglob->gl_flags & GLOB_NOCHECK) ||
481                     ((pglob->gl_flags & GLOB_NOMAGIC) &&
482                     !(pglob->gl_flags & GLOB_MAGCHAR)))
483                         return(globextend(pattern, pglob, &limit));
484                 else
485                         return(GLOB_NOMATCH);
486         }
487         if (!(pglob->gl_flags & GLOB_NOSORT))
488                 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
489                     pglob->gl_pathc - oldpathc, sizeof(char *), compare);
490         return(0);
491 }
492
493 static int
494 compare(const void *p, const void *q)
495 {
496         return(strcmp(*(char **)p, *(char **)q));
497 }
498
499 static int
500 glob1(Char *pattern, Char *pattern_last, glob_t *pglob, size_t *limitp)
501 {
502         Char pathbuf[MAXPATHLEN];
503
504         /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
505         if (*pattern == EOS)
506                 return(0);
507         return(glob2(pathbuf, pathbuf+MAXPATHLEN-1,
508             pathbuf, pathbuf+MAXPATHLEN-1,
509             pattern, pattern_last, pglob, limitp));
510 }
511
512 /*
513  * The functions glob2 and glob3 are mutually recursive; there is one level
514  * of recursion for each segment in the pattern that contains one or more
515  * meta characters.
516  */
517 static int
518 glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
519     Char *pattern, Char *pattern_last, glob_t *pglob, size_t *limitp)
520 {
521         struct stat sb;
522         Char *p, *q;
523         int anymeta;
524
525         /*
526          * Loop over pattern segments until end of pattern or until
527          * segment with meta character found.
528          */
529         for (anymeta = 0;;) {
530                 if (*pattern == EOS) {          /* End of pattern? */
531                         *pathend = EOS;
532                         if (g_lstat(pathbuf, &sb, pglob))
533                                 return(0);
534
535                         if (((pglob->gl_flags & GLOB_MARK) &&
536                             pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) ||
537                             (S_ISLNK(sb.st_mode) &&
538                             (g_stat(pathbuf, &sb, pglob) == 0) &&
539                             S_ISDIR(sb.st_mode)))) {
540                                 if (pathend+1 > pathend_last)
541                                         return (1);
542                                 *pathend++ = SEP;
543                                 *pathend = EOS;
544                         }
545                         ++pglob->gl_matchc;
546                         return(globextend(pathbuf, pglob, limitp));
547                 }
548
549                 /* Find end of next segment, copy tentatively to pathend. */
550                 q = pathend;
551                 p = pattern;
552                 while (*p != EOS && *p != SEP) {
553                         if (ismeta(*p))
554                                 anymeta = 1;
555                         if (q+1 > pathend_last)
556                                 return (1);
557                         *q++ = *p++;
558                 }
559
560                 if (!anymeta) {         /* No expansion, do next segment. */
561                         pathend = q;
562                         pattern = p;
563                         while (*pattern == SEP) {
564                                 if (pathend+1 > pathend_last)
565                                         return (1);
566                                 *pathend++ = *pattern++;
567                         }
568                 } else
569                         /* Need expansion, recurse. */
570                         return(glob3(pathbuf, pathbuf_last, pathend,
571                             pathend_last, pattern, pattern_last,
572                             p, pattern_last, pglob, limitp));
573         }
574         /* NOTREACHED */
575 }
576
577 static int
578 glob3(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last,
579     Char *pattern, Char *pattern_last, Char *restpattern,
580     Char *restpattern_last, glob_t *pglob, size_t *limitp)
581 {
582         struct dirent *dp;
583         DIR *dirp;
584         int err;
585         char buf[MAXPATHLEN];
586
587         /*
588          * The readdirfunc declaration can't be prototyped, because it is
589          * assigned, below, to two functions which are prototyped in glob.h
590          * and dirent.h as taking pointers to differently typed opaque
591          * structures.
592          */
593         struct dirent *(*readdirfunc)(void *);
594
595         if (pathend > pathend_last)
596                 return (1);
597         *pathend = EOS;
598         errno = 0;
599
600         if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
601                 /* TODO: don't call for ENOENT or ENOTDIR? */
602                 if (pglob->gl_errfunc) {
603                         if (g_Ctoc(pathbuf, buf, sizeof(buf)))
604                                 return(GLOB_ABORTED);
605                         if (pglob->gl_errfunc(buf, errno) ||
606                             pglob->gl_flags & GLOB_ERR)
607                                 return(GLOB_ABORTED);
608                 }
609                 return(0);
610         }
611
612         err = 0;
613
614         /* Search directory for matching names. */
615         if (pglob->gl_flags & GLOB_ALTDIRFUNC)
616                 readdirfunc = pglob->gl_readdir;
617         else
618                 readdirfunc = (struct dirent *(*)(void *))readdir;
619         while ((dp = (*readdirfunc)(dirp))) {
620                 u_char *sc;
621                 Char *dc;
622
623                 /* Initial DOT must be matched literally. */
624                 if (dp->d_name[0] == DOT && *pattern != DOT)
625                         continue;
626                 dc = pathend;
627                 sc = (u_char *) dp->d_name;
628                 while (dc < pathend_last && (*dc++ = *sc++) != EOS)
629                         ;
630                 if (dc >= pathend_last) {
631                         *dc = EOS;
632                         err = 1;
633                         break;
634                 }
635
636                 if (!match(pathend, pattern, restpattern)) {
637                         *pathend = EOS;
638                         continue;
639                 }
640                 err = glob2(pathbuf, pathbuf_last, --dc, pathend_last,
641                     restpattern, restpattern_last, pglob, limitp);
642                 if (err)
643                         break;
644         }
645
646         if (pglob->gl_flags & GLOB_ALTDIRFUNC)
647                 (*pglob->gl_closedir)(dirp);
648         else
649                 closedir(dirp);
650         return(err);
651 }
652
653
654 /*
655  * Extend the gl_pathv member of a glob_t structure to accommodate a new item,
656  * add the new item, and update gl_pathc.
657  *
658  * This assumes the BSD realloc, which only copies the block when its size
659  * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
660  * behavior.
661  *
662  * Return 0 if new item added, error code if memory couldn't be allocated.
663  *
664  * Invariant of the glob_t structure:
665  *      Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
666  *      gl_pathv points to (gl_offs + gl_pathc + 1) items.
667  */
668 static int
669 globextend(const Char *path, glob_t *pglob, size_t *limitp)
670 {
671         char **pathv;
672         int i;
673         u_int newsize, len;
674         char *copy;
675         const Char *p;
676
677         newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
678         pathv = pglob->gl_pathv ? realloc((char *)pglob->gl_pathv, newsize) :
679             malloc(newsize);
680         if (pathv == NULL) {
681                 if (pglob->gl_pathv) {
682                         free(pglob->gl_pathv);
683                         pglob->gl_pathv = NULL;
684                 }
685                 return(GLOB_NOSPACE);
686         }
687
688         if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
689                 /* first time around -- clear initial gl_offs items */
690                 pathv += pglob->gl_offs;
691                 for (i = pglob->gl_offs; --i >= 0; )
692                         *--pathv = NULL;
693         }
694         pglob->gl_pathv = pathv;
695
696         for (p = path; *p++;)
697                 ;
698         len = (size_t)(p - path);
699         *limitp += len;
700         if ((copy = malloc(len)) != NULL) {
701                 if (g_Ctoc(path, copy, len)) {
702                         free(copy);
703                         return(GLOB_NOSPACE);
704                 }
705                 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
706         }
707         pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
708
709         if ((pglob->gl_flags & GLOB_LIMIT) &&
710             newsize + *limitp >= (u_int) get_arg_max()) {
711                 errno = 0;
712                 return(GLOB_NOSPACE);
713         }
714
715         return(copy == NULL ? GLOB_NOSPACE : 0);
716 }
717
718
719 /*
720  * pattern matching function for filenames.  Each occurrence of the *
721  * pattern causes a recursion level.
722  */
723 static int
724 match(Char *name, Char *pat, Char *patend)
725 {
726         int ok, negate_range;
727         Char c, k;
728
729         while (pat < patend) {
730                 c = *pat++;
731                 switch (c & M_MASK) {
732                 case M_ALL:
733                         if (pat == patend)
734                                 return(1);
735                         do {
736                             if (match(name, pat, patend))
737                                     return(1);
738                         } while (*name++ != EOS);
739                         return(0);
740                 case M_ONE:
741                         if (*name++ == EOS)
742                                 return(0);
743                         break;
744                 case M_SET:
745                         ok = 0;
746                         if ((k = *name++) == EOS)
747                                 return(0);
748                         if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
749                                 ++pat;
750                         while (((c = *pat++) & M_MASK) != M_END)
751                                 if ((*pat & M_MASK) == M_RNG) {
752                                         if (c <= k && k <= pat[1])
753                                                 ok = 1;
754                                         pat += 2;
755                                 } else if (c == k)
756                                         ok = 1;
757                         if (ok == negate_range)
758                                 return(0);
759                         break;
760                 default:
761                         if (*name++ != c)
762                                 return(0);
763                         break;
764                 }
765         }
766         return(*name == EOS);
767 }
768
769 /* Free allocated data belonging to a glob_t structure. */
770 void
771 globfree(glob_t *pglob)
772 {
773         int i;
774         char **pp;
775
776         if (pglob->gl_pathv != NULL) {
777                 pp = pglob->gl_pathv + pglob->gl_offs;
778                 for (i = pglob->gl_pathc; i--; ++pp)
779                         if (*pp)
780                                 free(*pp);
781                 free(pglob->gl_pathv);
782                 pglob->gl_pathv = NULL;
783         }
784 }
785
786 static DIR *
787 g_opendir(Char *str, glob_t *pglob)
788 {
789         char buf[MAXPATHLEN];
790
791         if (!*str)
792                 strlcpy(buf, ".", sizeof buf);
793         else {
794                 if (g_Ctoc(str, buf, sizeof(buf)))
795                         return(NULL);
796         }
797
798         if (pglob->gl_flags & GLOB_ALTDIRFUNC)
799                 return((*pglob->gl_opendir)(buf));
800
801         return(opendir(buf));
802 }
803
804 static int
805 g_lstat(Char *fn, struct stat *sb, glob_t *pglob)
806 {
807         char buf[MAXPATHLEN];
808
809         if (g_Ctoc(fn, buf, sizeof(buf)))
810                 return(-1);
811         if (pglob->gl_flags & GLOB_ALTDIRFUNC)
812                 return((*pglob->gl_lstat)(buf, sb));
813         return(lstat(buf, sb));
814 }
815
816 static int
817 g_stat(Char *fn, struct stat *sb, glob_t *pglob)
818 {
819         char buf[MAXPATHLEN];
820
821         if (g_Ctoc(fn, buf, sizeof(buf)))
822                 return(-1);
823         if (pglob->gl_flags & GLOB_ALTDIRFUNC)
824                 return((*pglob->gl_stat)(buf, sb));
825         return(stat(buf, sb));
826 }
827
828 static Char *
829 g_strchr(Char *str, int ch)
830 {
831         do {
832                 if (*str == ch)
833                         return (str);
834         } while (*str++);
835         return (NULL);
836 }
837
838 static int
839 g_Ctoc(const Char *str, char *buf, u_int len)
840 {
841
842         while (len--) {
843                 if ((*buf++ = *str++) == EOS)
844                         return (0);
845         }
846         return (1);
847 }
848
849 #ifdef DEBUG
850 static void
851 qprintf(const char *str, Char *s)
852 {
853         Char *p;
854
855         (void)printf("%s:\n", str);
856         for (p = s; *p; p++)
857                 (void)printf("%c", CHAR(*p));
858         (void)printf("\n");
859         for (p = s; *p; p++)
860                 (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
861         (void)printf("\n");
862         for (p = s; *p; p++)
863                 (void)printf("%c", ismeta(*p) ? '_' : ' ');
864         (void)printf("\n");
865 }
866 #endif
867
868 #endif /* !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) ||
869           !defined(GLOB_HAS_GL_MATCHC) */
870
This page took 0.107351 seconds and 5 git commands to generate.