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