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