Libinfo-78.tar.gz
[apple/libinfo.git] / util.subproj / glob.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved
26 *
27 * Copyright (c) 1989, 1993
28 * The Regents of the University of California. All rights reserved.
29 *
30 * This code is derived from software contributed to Berkeley by
31 * Guido van Rossum.
32 *
33 * The NEXTSTEP Software License Agreement specifies the terms
34 * and conditions for redistribution.
35 *
36 * @(#)glob.c 8.3 (Berkeley) 10/13/93
37 */
38
39
40 /*
41 * glob(3) -- a superset of the one defined in POSIX 1003.2.
42 *
43 * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
44 *
45 * Optional extra services, controlled by flags not defined by POSIX:
46 *
47 * GLOB_QUOTE:
48 * Escaping convention: \ inhibits any special meaning the following
49 * character might have (except \ at end of string is retained).
50 * GLOB_MAGCHAR:
51 * Set in gl_flags if pattern contained a globbing character.
52 * GLOB_NOMAGIC:
53 * Same as GLOB_NOCHECK, but it will only append pattern if it did
54 * not contain any magic characters. [Used in csh style globbing]
55 * GLOB_ALTDIRFUNC:
56 * Use alternately specified directory access functions.
57 * GLOB_TILDE:
58 * expand ~user/foo to the /home/dir/of/user/foo
59 * GLOB_BRACE:
60 * expand {1,2}{a,b} to 1a 1b 2a 2b
61 * gl_matchc:
62 * Number of matches in the current invocation of glob.
63 */
64
65 #include <sys/param.h>
66 #include <sys/stat.h>
67
68 #include <ctype.h>
69 #include <dirent.h>
70 #include <errno.h>
71 #include <glob.h>
72 #include <pwd.h>
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <string.h>
76 #include <unistd.h>
77
78 #define DOLLAR '$'
79 #define DOT '.'
80 #define EOS '\0'
81 #define LBRACKET '['
82 #define NOT '!'
83 #define QUESTION '?'
84 #define QUOTE '\\'
85 #define RANGE '-'
86 #define RBRACKET ']'
87 #define SEP '/'
88 #define STAR '*'
89 #define TILDE '~'
90 #define UNDERSCORE '_'
91 #define LBRACE '{'
92 #define RBRACE '}'
93 #define SLASH '/'
94 #define COMMA ','
95
96 #ifndef DEBUG
97
98 #define M_QUOTE 0x8000
99 #define M_PROTECT 0x4000
100 #define M_MASK 0xffff
101 #define M_ASCII 0x00ff
102
103 typedef u_short Char;
104
105 #else
106
107 #define M_QUOTE 0x80
108 #define M_PROTECT 0x40
109 #define M_MASK 0xff
110 #define M_ASCII 0x7f
111
112 typedef char Char;
113
114 #endif
115
116
117 #define CHAR(c) ((Char)((c)&M_ASCII))
118 #define META(c) ((Char)((c)|M_QUOTE))
119 #define M_ALL META('*')
120 #define M_END META(']')
121 #define M_NOT META('!')
122 #define M_ONE META('?')
123 #define M_RNG META('-')
124 #define M_SET META('[')
125 #define ismeta(c) (((c)&M_QUOTE) != 0)
126
127
128 static int compare __P((const void *, const void *));
129 static void g_Ctoc __P((const Char *, char *));
130 static int g_lstat __P((Char *, struct stat *, glob_t *));
131 static DIR *g_opendir __P((Char *, glob_t *));
132 static Char *g_strchr __P((Char *, int));
133 #ifdef notdef
134 static Char *g_strcat __P((Char *, const Char *));
135 #endif
136 static int g_stat __P((Char *, struct stat *, glob_t *));
137 static int glob0 __P((const Char *, glob_t *));
138 static int glob1 __P((Char *, glob_t *));
139 static int glob2 __P((Char *, Char *, Char *, glob_t *));
140 static int glob3 __P((Char *, Char *, Char *, Char *, glob_t *));
141 static int globextend __P((const Char *, glob_t *));
142 static const Char * globtilde __P((const Char *, Char *, glob_t *));
143 static int globexp1 __P((const Char *, glob_t *));
144 static int globexp2 __P((const Char *, const Char *, glob_t *, int *));
145 static int match __P((Char *, Char *, Char *));
146 #ifdef DEBUG
147 static void qprintf __P((const char *, Char *));
148 #endif
149
150 int
151 glob(pattern, flags, errfunc, pglob)
152 const char *pattern;
153 int flags, (*errfunc) __P((const char *, int));
154 glob_t *pglob;
155 {
156 const u_char *patnext;
157 int c;
158 Char *bufnext, *bufend, patbuf[MAXPATHLEN+1];
159
160 patnext = (u_char *) pattern;
161 if (!(flags & GLOB_APPEND)) {
162 pglob->gl_pathc = 0;
163 pglob->gl_pathv = NULL;
164 if (!(flags & GLOB_DOOFFS))
165 pglob->gl_offs = 0;
166 }
167 pglob->gl_flags = flags & ~GLOB_MAGCHAR;
168 pglob->gl_errfunc = errfunc;
169 pglob->gl_matchc = 0;
170
171 bufnext = patbuf;
172 bufend = bufnext + MAXPATHLEN;
173 if (flags & GLOB_QUOTE) {
174 /* Protect the quoted characters. */
175 while (bufnext < bufend && (c = *patnext++) != EOS)
176 if (c == QUOTE) {
177 if ((c = *patnext++) == EOS) {
178 c = QUOTE;
179 --patnext;
180 }
181 *bufnext++ = c | M_PROTECT;
182 }
183 else
184 *bufnext++ = c;
185 }
186 else
187 while (bufnext < bufend && (c = *patnext++) != EOS)
188 *bufnext++ = c;
189 *bufnext = EOS;
190
191 if (flags & GLOB_BRACE)
192 return globexp1(patbuf, pglob);
193 else
194 return glob0(patbuf, pglob);
195 }
196
197 /*
198 * Expand recursively a glob {} pattern. When there is no more expansion
199 * invoke the standard globbing routine to glob the rest of the magic
200 * characters
201 */
202 static int globexp1(pattern, pglob)
203 const Char *pattern;
204 glob_t *pglob;
205 {
206 const Char* ptr = pattern;
207 int rv;
208
209 /* Protect a single {}, for find(1), like csh */
210 if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
211 return glob0(pattern, pglob);
212
213 while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL)
214 if (!globexp2(ptr, pattern, pglob, &rv))
215 return rv;
216
217 return glob0(pattern, pglob);
218 }
219
220
221 /*
222 * Recursive brace globbing helper. Tries to expand a single brace.
223 * If it succeeds then it invokes globexp1 with the new pattern.
224 * If it fails then it tries to glob the rest of the pattern and returns.
225 */
226 static int globexp2(ptr, pattern, pglob, rv)
227 const Char *ptr, *pattern;
228 glob_t *pglob;
229 int *rv;
230 {
231 int i;
232 Char *lm, *ls;
233 const Char *pe, *pm, *pl;
234 Char patbuf[MAXPATHLEN + 1];
235
236 /* copy part up to the brace */
237 for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
238 continue;
239 ls = lm;
240
241 /* Find the balanced brace */
242 for (i = 0, pe = ++ptr; *pe; pe++)
243 if (*pe == LBRACKET) {
244 /* Ignore everything between [] */
245 for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
246 continue;
247 if (*pe == EOS) {
248 /*
249 * We could not find a matching RBRACKET.
250 * Ignore and just look for RBRACE
251 */
252 pe = pm;
253 }
254 }
255 else if (*pe == LBRACE)
256 i++;
257 else if (*pe == RBRACE) {
258 if (i == 0)
259 break;
260 i--;
261 }
262
263 /* Non matching braces; just glob the pattern */
264 if (i != 0 || *pe == EOS) {
265 *rv = glob0(patbuf, pglob);
266 return 0;
267 }
268
269 for (i = 0, pl = pm = ptr; pm <= pe; pm++)
270 switch (*pm) {
271 case LBRACKET:
272 /* Ignore everything between [] */
273 for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)
274 continue;
275 if (*pm == EOS) {
276 /*
277 * We could not find a matching RBRACKET.
278 * Ignore and just look for RBRACE
279 */
280 pm = pl;
281 }
282 break;
283
284 case LBRACE:
285 i++;
286 break;
287
288 case RBRACE:
289 if (i) {
290 i--;
291 break;
292 }
293 /* FALLTHROUGH */
294 case COMMA:
295 if (i && *pm == COMMA)
296 break;
297 else {
298 /* Append the current string */
299 for (lm = ls; (pl < pm); *lm++ = *pl++)
300 continue;
301 /*
302 * Append the rest of the pattern after the
303 * closing brace
304 */
305 for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
306 continue;
307
308 /* Expand the current pattern */
309 #ifdef DEBUG
310 qprintf("globexp2:", patbuf);
311 #endif
312 *rv = globexp1(patbuf, pglob);
313
314 /* move after the comma, to the next string */
315 pl = pm + 1;
316 }
317 break;
318
319 default:
320 break;
321 }
322 *rv = 0;
323 return 0;
324 }
325
326
327
328 /*
329 * expand tilde from the passwd file.
330 */
331 static const Char *
332 globtilde(pattern, patbuf, pglob)
333 const Char *pattern;
334 Char *patbuf;
335 glob_t *pglob;
336 {
337 struct passwd *pwd;
338 char *h;
339 const Char *p;
340 Char *b;
341
342 if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
343 return pattern;
344
345 /* Copy up to the end of the string or / */
346 for (p = pattern + 1, h = (char *) patbuf; *p && *p != SLASH;
347 *h++ = *p++)
348 continue;
349
350 *h = EOS;
351
352 if (((char *) patbuf)[0] == EOS) {
353 /*
354 * handle a plain ~ or ~/ by expanding $HOME
355 * first and then trying the password file
356 */
357 if ((h = getenv("HOME")) == NULL) {
358 if ((pwd = getpwuid(getuid())) == NULL)
359 return pattern;
360 else
361 h = pwd->pw_dir;
362 }
363 }
364 else {
365 /*
366 * Expand a ~user
367 */
368 if ((pwd = getpwnam((char*) patbuf)) == NULL)
369 return pattern;
370 else
371 h = pwd->pw_dir;
372 }
373
374 /* Copy the home directory */
375 for (b = patbuf; *h; *b++ = *h++)
376 continue;
377
378 /* Append the rest of the pattern */
379 while ((*b++ = *p++) != EOS)
380 continue;
381
382 return patbuf;
383 }
384
385
386 /*
387 * The main glob() routine: compiles the pattern (optionally processing
388 * quotes), calls glob1() to do the real pattern matching, and finally
389 * sorts the list (unless unsorted operation is requested). Returns 0
390 * if things went well, nonzero if errors occurred. It is not an error
391 * to find no matches.
392 */
393 static int
394 glob0(pattern, pglob)
395 const Char *pattern;
396 glob_t *pglob;
397 {
398 const Char *qpatnext;
399 int c, err, oldpathc;
400 Char *bufnext, patbuf[MAXPATHLEN+1];
401
402 qpatnext = globtilde(pattern, patbuf, pglob);
403 oldpathc = pglob->gl_pathc;
404 bufnext = patbuf;
405
406 /* We don't need to check for buffer overflow any more. */
407 while ((c = *qpatnext++) != EOS) {
408 switch (c) {
409 case LBRACKET:
410 c = *qpatnext;
411 if (c == NOT)
412 ++qpatnext;
413 if (*qpatnext == EOS ||
414 g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) {
415 *bufnext++ = LBRACKET;
416 if (c == NOT)
417 --qpatnext;
418 break;
419 }
420 *bufnext++ = M_SET;
421 if (c == NOT)
422 *bufnext++ = M_NOT;
423 c = *qpatnext++;
424 do {
425 *bufnext++ = CHAR(c);
426 if (*qpatnext == RANGE &&
427 (c = qpatnext[1]) != RBRACKET) {
428 *bufnext++ = M_RNG;
429 *bufnext++ = CHAR(c);
430 qpatnext += 2;
431 }
432 } while ((c = *qpatnext++) != RBRACKET);
433 pglob->gl_flags |= GLOB_MAGCHAR;
434 *bufnext++ = M_END;
435 break;
436 case QUESTION:
437 pglob->gl_flags |= GLOB_MAGCHAR;
438 *bufnext++ = M_ONE;
439 break;
440 case STAR:
441 pglob->gl_flags |= GLOB_MAGCHAR;
442 /* collapse adjacent stars to one,
443 * to avoid exponential behavior
444 */
445 if (bufnext == patbuf || bufnext[-1] != M_ALL)
446 *bufnext++ = M_ALL;
447 break;
448 default:
449 *bufnext++ = CHAR(c);
450 break;
451 }
452 }
453 *bufnext = EOS;
454 #ifdef DEBUG
455 qprintf("glob0:", patbuf);
456 #endif
457
458 if ((err = glob1(patbuf, pglob)) != 0)
459 return(err);
460
461 /*
462 * If there was no match we are going to append the pattern
463 * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
464 * and the pattern did not contain any magic characters
465 * GLOB_NOMAGIC is there just for compatibility with csh.
466 */
467 if (pglob->gl_pathc == oldpathc &&
468 ((pglob->gl_flags & GLOB_NOCHECK) ||
469 ((pglob->gl_flags & GLOB_NOMAGIC) &&
470 !(pglob->gl_flags & GLOB_MAGCHAR))))
471 return(globextend(pattern, pglob));
472 else if (!(pglob->gl_flags & GLOB_NOSORT))
473 qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
474 pglob->gl_pathc - oldpathc, sizeof(char *), compare);
475 return(0);
476 }
477
478 static int
479 compare(p, q)
480 const void *p, *q;
481 {
482 return(strcmp(*(char **)p, *(char **)q));
483 }
484
485 static int
486 glob1(pattern, pglob)
487 Char *pattern;
488 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(pathbuf, pathend, pattern, pglob)
505 Char *pathbuf, *pathend, *pattern;
506 glob_t *pglob;
507 {
508 struct stat sb;
509 Char *p, *q;
510 int anymeta;
511
512 /*
513 * Loop over pattern segments until end of pattern or until
514 * segment with meta character found.
515 */
516 for (anymeta = 0;;) {
517 if (*pattern == EOS) { /* End of pattern? */
518 *pathend = EOS;
519 if (g_lstat(pathbuf, &sb, pglob))
520 return(0);
521
522 if (((pglob->gl_flags & GLOB_MARK) &&
523 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
524 || (S_ISLNK(sb.st_mode) &&
525 (g_stat(pathbuf, &sb, pglob) == 0) &&
526 S_ISDIR(sb.st_mode)))) {
527 *pathend++ = SEP;
528 *pathend = EOS;
529 }
530 ++pglob->gl_matchc;
531 return(globextend(pathbuf, pglob));
532 }
533
534 /* Find end of next segment, copy tentatively to pathend. */
535 q = pathend;
536 p = pattern;
537 while (*p != EOS && *p != SEP) {
538 if (ismeta(*p))
539 anymeta = 1;
540 *q++ = *p++;
541 }
542
543 if (!anymeta) { /* No expansion, do next segment. */
544 pathend = q;
545 pattern = p;
546 while (*pattern == SEP)
547 *pathend++ = *pattern++;
548 } else /* Need expansion, recurse. */
549 return(glob3(pathbuf, pathend, pattern, p, pglob));
550 }
551 /* NOTREACHED */
552 }
553
554 static int
555 glob3(pathbuf, pathend, pattern, restpattern, pglob)
556 Char *pathbuf, *pathend, *pattern, *restpattern;
557 glob_t *pglob;
558 {
559 register 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)();
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;
593 while ((dp = (*readdirfunc)(dirp))) {
594 register u_char *sc;
595 register 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(path, pglob)
636 const Char *path;
637 glob_t *pglob;
638 {
639 register char **pathv;
640 register int i;
641 u_int newsize;
642 char *copy;
643 const Char *p;
644
645 newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
646 pathv = pglob->gl_pathv ?
647 realloc((char *)pglob->gl_pathv, newsize) :
648 malloc(newsize);
649 if (pathv == NULL)
650 return(GLOB_NOSPACE);
651
652 if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
653 /* first time around -- clear initial gl_offs items */
654 pathv += pglob->gl_offs;
655 for (i = pglob->gl_offs; --i >= 0; )
656 *--pathv = NULL;
657 }
658 pglob->gl_pathv = pathv;
659
660 for (p = path; *p++;)
661 continue;
662 if ((copy = malloc(p - path)) != NULL) {
663 g_Ctoc(path, copy);
664 pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
665 }
666 pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
667 return(copy == NULL ? GLOB_NOSPACE : 0);
668 }
669
670
671 /*
672 * pattern matching function for filenames. Each occurrence of the *
673 * pattern causes a recursion level.
674 */
675 static int
676 match(name, pat, patend)
677 register Char *name, *pat, *patend;
678 {
679 int ok, negate_range;
680 Char c, k;
681
682 while (pat < patend) {
683 c = *pat++;
684 switch (c & M_MASK) {
685 case M_ALL:
686 if (pat == patend)
687 return(1);
688 do
689 if (match(name, pat, patend))
690 return(1);
691 while (*name++ != EOS);
692 return(0);
693 case M_ONE:
694 if (*name++ == EOS)
695 return(0);
696 break;
697 case M_SET:
698 ok = 0;
699 if ((k = *name++) == EOS)
700 return(0);
701 if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
702 ++pat;
703 while (((c = *pat++) & M_MASK) != M_END)
704 if ((*pat & M_MASK) == M_RNG) {
705 if (c <= k && k <= pat[1])
706 ok = 1;
707 pat += 2;
708 } else if (c == k)
709 ok = 1;
710 if (ok == negate_range)
711 return(0);
712 break;
713 default:
714 if (*name++ != c)
715 return(0);
716 break;
717 }
718 }
719 return(*name == EOS);
720 }
721
722 /* Free allocated data belonging to a glob_t structure. */
723 void
724 globfree(pglob)
725 glob_t *pglob;
726 {
727 register int i;
728 register char **pp;
729
730 if (pglob->gl_pathv != NULL) {
731 pp = pglob->gl_pathv + pglob->gl_offs;
732 for (i = pglob->gl_pathc; i--; ++pp)
733 if (*pp)
734 free(*pp);
735 free(pglob->gl_pathv);
736 }
737 }
738
739 static DIR *
740 g_opendir(str, pglob)
741 register Char *str;
742 glob_t *pglob;
743 {
744 char buf[MAXPATHLEN];
745
746 if (!*str)
747 strcpy(buf, ".");
748 else
749 g_Ctoc(str, buf);
750
751 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
752 return((*pglob->gl_opendir)(buf));
753
754 return(opendir(buf));
755 }
756
757 static int
758 g_lstat(fn, sb, pglob)
759 register Char *fn;
760 struct stat *sb;
761 glob_t *pglob;
762 {
763 char buf[MAXPATHLEN];
764
765 g_Ctoc(fn, buf);
766 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
767 return((*pglob->gl_lstat)(buf, sb));
768 return(lstat(buf, sb));
769 }
770
771 static int
772 g_stat(fn, sb, pglob)
773 register Char *fn;
774 struct stat *sb;
775 glob_t *pglob;
776 {
777 char buf[MAXPATHLEN];
778
779 g_Ctoc(fn, buf);
780 if (pglob->gl_flags & GLOB_ALTDIRFUNC)
781 return((*pglob->gl_stat)(buf, sb));
782 return(stat(buf, sb));
783 }
784
785 static Char *
786 g_strchr(str, ch)
787 Char *str;
788 int ch;
789 {
790 do {
791 if (*str == ch)
792 return (str);
793 } while (*str++);
794 return (NULL);
795 }
796
797 #ifdef notdef
798 static Char *
799 g_strcat(dst, src)
800 Char *dst;
801 const Char* src;
802 {
803 Char *sdst = dst;
804
805 while (*dst++)
806 continue;
807 --dst;
808 while((*dst++ = *src++) != EOS)
809 continue;
810
811 return (sdst);
812 }
813 #endif
814
815 static void
816 g_Ctoc(str, buf)
817 register const Char *str;
818 char *buf;
819 {
820 register char *dc;
821
822 for (dc = buf; (*dc++ = *str++) != EOS;)
823 continue;
824 }
825
826 #ifdef DEBUG
827 static void
828 qprintf(str, s)
829 const char *str;
830 register Char *s;
831 {
832 register Char *p;
833
834 (void)printf("%s:\n", str);
835 for (p = s; *p; p++)
836 (void)printf("%c", CHAR(*p));
837 (void)printf("\n");
838 for (p = s; *p; p++)
839 (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
840 (void)printf("\n");
841 for (p = s; *p; p++)
842 (void)printf("%c", ismeta(*p) ? '_' : ' ');
843 (void)printf("\n");
844 }
845 #endif