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