Debian Codegraph @

compat/lib/compatglob.c

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