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