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