glob.c revision 1.1.1.1 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1989 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * Guido van Rossum.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
38 1.1 cgd static char sccsid[] = "@(#)glob.c 5.12 (Berkeley) 6/24/91";
39 1.1 cgd #endif /* LIBC_SCCS and not lint */
40 1.1 cgd
41 1.1 cgd /*
42 1.1 cgd * glob(3) -- a superset of the one defined in POSIX 1003.2.
43 1.1 cgd *
44 1.1 cgd * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
45 1.1 cgd *
46 1.1 cgd * Optional extra services, controlled by flags not defined by POSIX:
47 1.1 cgd *
48 1.1 cgd * GLOB_QUOTE:
49 1.1 cgd * Escaping convention: \ inhibits any special meaning the following
50 1.1 cgd * character might have (except \ at end of string is retained).
51 1.1 cgd * GLOB_MAGCHAR:
52 1.1 cgd * Set in gl_flags if pattern contained a globbing character.
53 1.1 cgd * gl_matchc:
54 1.1 cgd * Number of matches in the current invocation of glob.
55 1.1 cgd */
56 1.1 cgd
57 1.1 cgd #include <sys/cdefs.h>
58 1.1 cgd #include <sys/param.h>
59 1.1 cgd #include <sys/stat.h>
60 1.1 cgd #include <dirent.h>
61 1.1 cgd #include <glob.h>
62 1.1 cgd #include <ctype.h>
63 1.1 cgd #include <errno.h>
64 1.1 cgd #include <string.h>
65 1.1 cgd #include <stdio.h>
66 1.1 cgd #include <stdlib.h>
67 1.1 cgd
68 1.1 cgd #define DOLLAR '$'
69 1.1 cgd #define DOT '.'
70 1.1 cgd #define EOS '\0'
71 1.1 cgd #define LBRACKET '['
72 1.1 cgd #define NOT '!'
73 1.1 cgd #define QUESTION '?'
74 1.1 cgd #define QUOTE '\\'
75 1.1 cgd #define RANGE '-'
76 1.1 cgd #define RBRACKET ']'
77 1.1 cgd #define SEP '/'
78 1.1 cgd #define STAR '*'
79 1.1 cgd #define TILDE '~'
80 1.1 cgd #define UNDERSCORE '_'
81 1.1 cgd
82 1.1 cgd #define M_QUOTE 0x8000
83 1.1 cgd #define M_PROTECT 0x4000
84 1.1 cgd #define M_MASK 0xffff
85 1.1 cgd #define M_ASCII 0x00ff
86 1.1 cgd
87 1.1 cgd #define CHAR(c) ((c)&M_ASCII)
88 1.1 cgd #define META(c) ((c)|M_QUOTE)
89 1.1 cgd #define M_ALL META('*')
90 1.1 cgd #define M_END META(']')
91 1.1 cgd #define M_NOT META('!')
92 1.1 cgd #define M_ONE META('?')
93 1.1 cgd #define M_RNG META('-')
94 1.1 cgd #define M_SET META('[')
95 1.1 cgd #define ismeta(c) (((c)&M_QUOTE) != 0)
96 1.1 cgd
97 1.1 cgd typedef u_short Char;
98 1.1 cgd
99 1.1 cgd static int compare __P((const void *, const void *));
100 1.1 cgd static void g_Ctoc __P((Char *, char *));
101 1.1 cgd static int g_lstat __P((Char *, struct stat *));
102 1.1 cgd static DIR *g_opendir __P((Char *));
103 1.1 cgd static Char *g_strchr __P((Char *, int));
104 1.1 cgd static int g_stat __P((Char *, struct stat *));
105 1.1 cgd static int glob1 __P((Char *, glob_t *));
106 1.1 cgd static int glob2 __P((Char *, Char *, Char *, glob_t *));
107 1.1 cgd static int glob3 __P((Char *, Char *, Char *, Char *, glob_t *));
108 1.1 cgd static int globextend __P((Char *, glob_t *));
109 1.1 cgd static int match __P((Char *, Char *, Char *));
110 1.1 cgd #ifdef DEBUG
111 1.1 cgd static void qprintf __P((Char *));
112 1.1 cgd #endif
113 1.1 cgd
114 1.1 cgd /*
115 1.1 cgd * The main glob() routine: compiles the pattern (optionally processing
116 1.1 cgd * quotes), calls glob1() to do the real pattern matching, and finally
117 1.1 cgd * sorts the list (unless unsorted operation is requested). Returns 0
118 1.1 cgd * if things went well, nonzero if errors occurred. It is not an error
119 1.1 cgd * to find no matches.
120 1.1 cgd */
121 1.1 cgd glob(pattern, flags, errfunc, pglob)
122 1.1 cgd const char *pattern;
123 1.1 cgd int flags, (*errfunc) __P((char *, int));
124 1.1 cgd glob_t *pglob;
125 1.1 cgd {
126 1.1 cgd const u_char *compilepat, *patnext;
127 1.1 cgd int c, err, oldpathc;
128 1.1 cgd Char *bufnext, *bufend, *compilebuf, *qpatnext, patbuf[MAXPATHLEN+1];
129 1.1 cgd
130 1.1 cgd patnext = (u_char *) pattern;
131 1.1 cgd if (!(flags & GLOB_APPEND)) {
132 1.1 cgd pglob->gl_pathc = 0;
133 1.1 cgd pglob->gl_pathv = NULL;
134 1.1 cgd if (!(flags & GLOB_DOOFFS))
135 1.1 cgd pglob->gl_offs = 0;
136 1.1 cgd }
137 1.1 cgd pglob->gl_flags = flags & ~GLOB_MAGCHAR;
138 1.1 cgd pglob->gl_errfunc = errfunc;
139 1.1 cgd oldpathc = pglob->gl_pathc;
140 1.1 cgd pglob->gl_matchc = 0;
141 1.1 cgd
142 1.1 cgd bufnext = patbuf;
143 1.1 cgd bufend = bufnext + MAXPATHLEN;
144 1.1 cgd compilebuf = bufnext;
145 1.1 cgd compilepat = patnext;
146 1.1 cgd if (flags & GLOB_QUOTE) {
147 1.1 cgd /* Protect the quoted characters. */
148 1.1 cgd while (bufnext < bufend && (c = *patnext++) != EOS)
149 1.1 cgd if (c == QUOTE) {
150 1.1 cgd if ((c = *patnext++) == EOS) {
151 1.1 cgd c = QUOTE;
152 1.1 cgd --patnext;
153 1.1 cgd }
154 1.1 cgd *bufnext++ = c | M_PROTECT;
155 1.1 cgd }
156 1.1 cgd else
157 1.1 cgd *bufnext++ = c;
158 1.1 cgd }
159 1.1 cgd else
160 1.1 cgd while (bufnext < bufend && (c = *patnext++) != EOS)
161 1.1 cgd *bufnext++ = c;
162 1.1 cgd *bufnext = EOS;
163 1.1 cgd
164 1.1 cgd bufnext = patbuf;
165 1.1 cgd qpatnext = patbuf;
166 1.1 cgd /* We don't need to check for buffer overflow any more. */
167 1.1 cgd while ((c = *qpatnext++) != EOS) {
168 1.1 cgd switch (c) {
169 1.1 cgd case LBRACKET:
170 1.1 cgd pglob->gl_flags |= GLOB_MAGCHAR;
171 1.1 cgd c = *qpatnext;
172 1.1 cgd if (c == NOT)
173 1.1 cgd ++qpatnext;
174 1.1 cgd if (*qpatnext == EOS ||
175 1.1 cgd g_strchr(qpatnext+1, RBRACKET) == NULL) {
176 1.1 cgd *bufnext++ = LBRACKET;
177 1.1 cgd if (c == NOT)
178 1.1 cgd --qpatnext;
179 1.1 cgd break;
180 1.1 cgd }
181 1.1 cgd *bufnext++ = M_SET;
182 1.1 cgd if (c == NOT)
183 1.1 cgd *bufnext++ = M_NOT;
184 1.1 cgd c = *qpatnext++;
185 1.1 cgd do {
186 1.1 cgd *bufnext++ = CHAR(c);
187 1.1 cgd if (*qpatnext == RANGE &&
188 1.1 cgd (c = qpatnext[1]) != RBRACKET) {
189 1.1 cgd *bufnext++ = M_RNG;
190 1.1 cgd *bufnext++ = CHAR(c);
191 1.1 cgd qpatnext += 2;
192 1.1 cgd }
193 1.1 cgd } while ((c = *qpatnext++) != RBRACKET);
194 1.1 cgd *bufnext++ = M_END;
195 1.1 cgd break;
196 1.1 cgd case QUESTION:
197 1.1 cgd pglob->gl_flags |= GLOB_MAGCHAR;
198 1.1 cgd *bufnext++ = M_ONE;
199 1.1 cgd break;
200 1.1 cgd case STAR:
201 1.1 cgd pglob->gl_flags |= GLOB_MAGCHAR;
202 1.1 cgd *bufnext++ = M_ALL;
203 1.1 cgd break;
204 1.1 cgd default:
205 1.1 cgd *bufnext++ = CHAR(c);
206 1.1 cgd break;
207 1.1 cgd }
208 1.1 cgd }
209 1.1 cgd *bufnext = EOS;
210 1.1 cgd #ifdef DEBUG
211 1.1 cgd qprintf(patbuf);
212 1.1 cgd #endif
213 1.1 cgd
214 1.1 cgd if ((err = glob1(patbuf, pglob)) != 0)
215 1.1 cgd return(err);
216 1.1 cgd
217 1.1 cgd if (pglob->gl_pathc == oldpathc && flags & GLOB_NOCHECK) {
218 1.1 cgd if (!(flags & GLOB_QUOTE)) {
219 1.1 cgd Char *dp = compilebuf;
220 1.1 cgd const u_char *sp = compilepat;
221 1.1 cgd while (*dp++ = *sp++);
222 1.1 cgd }
223 1.1 cgd else {
224 1.1 cgd /*
225 1.1 cgd * Copy pattern, interpreting quotes; this is slightly
226 1.1 cgd * different than the interpretation of quotes above
227 1.1 cgd * -- which should prevail?
228 1.1 cgd */
229 1.1 cgd while (*compilepat != EOS) {
230 1.1 cgd if (*compilepat == QUOTE) {
231 1.1 cgd if (*++compilepat == EOS)
232 1.1 cgd --compilepat;
233 1.1 cgd }
234 1.1 cgd *compilebuf++ = (u_char)*compilepat++;
235 1.1 cgd }
236 1.1 cgd *compilebuf = EOS;
237 1.1 cgd }
238 1.1 cgd return(globextend(patbuf, pglob));
239 1.1 cgd } else if (!(flags & GLOB_NOSORT))
240 1.1 cgd qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
241 1.1 cgd pglob->gl_pathc - oldpathc, sizeof(char *), compare);
242 1.1 cgd return(0);
243 1.1 cgd }
244 1.1 cgd
245 1.1 cgd static int
246 1.1 cgd compare(p, q)
247 1.1 cgd const void *p, *q;
248 1.1 cgd {
249 1.1 cgd return(strcmp(*(char **)p, *(char **)q));
250 1.1 cgd }
251 1.1 cgd
252 1.1 cgd static
253 1.1 cgd glob1(pattern, pglob)
254 1.1 cgd Char *pattern;
255 1.1 cgd glob_t *pglob;
256 1.1 cgd {
257 1.1 cgd Char pathbuf[MAXPATHLEN+1];
258 1.1 cgd
259 1.1 cgd /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
260 1.1 cgd if (*pattern == EOS)
261 1.1 cgd return(0);
262 1.1 cgd return(glob2(pathbuf, pathbuf, pattern, pglob));
263 1.1 cgd }
264 1.1 cgd
265 1.1 cgd /*
266 1.1 cgd * The functions glob2 and glob3 are mutually recursive; there is one level
267 1.1 cgd * of recursion for each segment in the pattern that contains one or more
268 1.1 cgd * meta characters.
269 1.1 cgd */
270 1.1 cgd static
271 1.1 cgd glob2(pathbuf, pathend, pattern, pglob)
272 1.1 cgd Char *pathbuf, *pathend, *pattern;
273 1.1 cgd glob_t *pglob;
274 1.1 cgd {
275 1.1 cgd struct stat sb;
276 1.1 cgd Char *p, *q;
277 1.1 cgd int anymeta;
278 1.1 cgd
279 1.1 cgd /*
280 1.1 cgd * Loop over pattern segments until end of pattern or until
281 1.1 cgd * segment with meta character found.
282 1.1 cgd */
283 1.1 cgd for (anymeta = 0;;) {
284 1.1 cgd if (*pattern == EOS) { /* End of pattern? */
285 1.1 cgd *pathend = EOS;
286 1.1 cgd if (g_stat(pathbuf, &sb))
287 1.1 cgd return(0);
288 1.1 cgd
289 1.1 cgd if (((pglob->gl_flags & GLOB_MARK) &&
290 1.1 cgd pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
291 1.1 cgd || (S_ISLNK(sb.st_mode) &&
292 1.1 cgd (g_stat(pathbuf, &sb) == 0) &&
293 1.1 cgd S_ISDIR(sb.st_mode)))) {
294 1.1 cgd *pathend++ = SEP;
295 1.1 cgd *pathend = EOS;
296 1.1 cgd }
297 1.1 cgd ++pglob->gl_matchc;
298 1.1 cgd return(globextend(pathbuf, pglob));
299 1.1 cgd }
300 1.1 cgd
301 1.1 cgd /* Find end of next segment, copy tentatively to pathend. */
302 1.1 cgd q = pathend;
303 1.1 cgd p = pattern;
304 1.1 cgd while (*p != EOS && *p != SEP) {
305 1.1 cgd if (ismeta(*p))
306 1.1 cgd anymeta = 1;
307 1.1 cgd *q++ = *p++;
308 1.1 cgd }
309 1.1 cgd
310 1.1 cgd if (!anymeta) { /* No expansion, do next segment. */
311 1.1 cgd pathend = q;
312 1.1 cgd pattern = p;
313 1.1 cgd while (*pattern == SEP)
314 1.1 cgd *pathend++ = *pattern++;
315 1.1 cgd } else /* Need expansion, recurse. */
316 1.1 cgd return(glob3(pathbuf, pathend, pattern, p, pglob));
317 1.1 cgd }
318 1.1 cgd /* NOTREACHED */
319 1.1 cgd }
320 1.1 cgd
321 1.1 cgd static
322 1.1 cgd glob3(pathbuf, pathend, pattern, restpattern, pglob)
323 1.1 cgd Char *pathbuf, *pathend, *pattern, *restpattern;
324 1.1 cgd glob_t *pglob;
325 1.1 cgd {
326 1.1 cgd register struct dirent *dp;
327 1.1 cgd DIR *dirp;
328 1.1 cgd int len, err;
329 1.1 cgd
330 1.1 cgd *pathend = EOS;
331 1.1 cgd errno = 0;
332 1.1 cgd
333 1.1 cgd if (!(dirp = g_opendir(pathbuf)))
334 1.1 cgd /* TODO: don't call for ENOENT or ENOTDIR? */
335 1.1 cgd if (pglob->gl_errfunc &&
336 1.1 cgd (*pglob->gl_errfunc)(pathbuf, errno) ||
337 1.1 cgd (pglob->gl_flags & GLOB_ERR))
338 1.1 cgd return(GLOB_ABEND);
339 1.1 cgd else
340 1.1 cgd return(0);
341 1.1 cgd
342 1.1 cgd err = 0;
343 1.1 cgd
344 1.1 cgd /* Search directory for matching names. */
345 1.1 cgd while ((dp = readdir(dirp))) {
346 1.1 cgd register u_char *sc;
347 1.1 cgd register Char *dc;
348 1.1 cgd
349 1.1 cgd /* Initial DOT must be matched literally. */
350 1.1 cgd if (dp->d_name[0] == DOT && *pattern != DOT)
351 1.1 cgd continue;
352 1.1 cgd for (sc = (u_char *) dp->d_name, dc = pathend;
353 1.1 cgd *dc++ = *sc++;);
354 1.1 cgd if (!match(pathend, pattern, restpattern)) {
355 1.1 cgd *pathend = EOS;
356 1.1 cgd continue;
357 1.1 cgd }
358 1.1 cgd err = glob2(pathbuf, --dc, restpattern, pglob);
359 1.1 cgd if (err)
360 1.1 cgd break;
361 1.1 cgd }
362 1.1 cgd
363 1.1 cgd /* TODO: check error from readdir? */
364 1.1 cgd (void)closedir(dirp);
365 1.1 cgd return(err);
366 1.1 cgd }
367 1.1 cgd
368 1.1 cgd
369 1.1 cgd /*
370 1.1 cgd * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
371 1.1 cgd * add the new item, and update gl_pathc.
372 1.1 cgd *
373 1.1 cgd * This assumes the BSD realloc, which only copies the block when its size
374 1.1 cgd * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
375 1.1 cgd * behavior.
376 1.1 cgd *
377 1.1 cgd * Return 0 if new item added, error code if memory couldn't be allocated.
378 1.1 cgd *
379 1.1 cgd * Invariant of the glob_t structure:
380 1.1 cgd * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
381 1.1 cgd * gl_pathv points to (gl_offs + gl_pathc + 1) items.
382 1.1 cgd */
383 1.1 cgd static int
384 1.1 cgd globextend(path, pglob)
385 1.1 cgd Char *path;
386 1.1 cgd glob_t *pglob;
387 1.1 cgd {
388 1.1 cgd register char **pathv;
389 1.1 cgd register int i;
390 1.1 cgd u_int newsize;
391 1.1 cgd char *copy;
392 1.1 cgd Char *p;
393 1.1 cgd
394 1.1 cgd newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
395 1.1 cgd pathv = (char **)realloc((char *)pglob->gl_pathv, newsize);
396 1.1 cgd if (pathv == NULL)
397 1.1 cgd return(GLOB_NOSPACE);
398 1.1 cgd
399 1.1 cgd if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
400 1.1 cgd /* first time around -- clear initial gl_offs items */
401 1.1 cgd pathv += pglob->gl_offs;
402 1.1 cgd for (i = pglob->gl_offs; --i >= 0; )
403 1.1 cgd *--pathv = NULL;
404 1.1 cgd }
405 1.1 cgd pglob->gl_pathv = pathv;
406 1.1 cgd
407 1.1 cgd for (p = path; *p++;);
408 1.1 cgd if ((copy = malloc(p - path)) != NULL) {
409 1.1 cgd g_Ctoc(path, copy);
410 1.1 cgd pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
411 1.1 cgd }
412 1.1 cgd pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
413 1.1 cgd return(copy == NULL ? GLOB_NOSPACE : 0);
414 1.1 cgd }
415 1.1 cgd
416 1.1 cgd
417 1.1 cgd /*
418 1.1 cgd * pattern matching function for filenames. Each occurrence of the *
419 1.1 cgd * pattern causes a recursion level.
420 1.1 cgd */
421 1.1 cgd static
422 1.1 cgd match(name, pat, patend)
423 1.1 cgd register Char *name, *pat, *patend;
424 1.1 cgd {
425 1.1 cgd int ok, negate_range;
426 1.1 cgd Char c, k;
427 1.1 cgd
428 1.1 cgd while (pat < patend) {
429 1.1 cgd c = *pat++;
430 1.1 cgd switch (c & M_MASK) {
431 1.1 cgd case M_ALL:
432 1.1 cgd if (pat == patend)
433 1.1 cgd return(1);
434 1.1 cgd for (; *name != EOS; ++name)
435 1.1 cgd if (match(name, pat, patend))
436 1.1 cgd return(1);
437 1.1 cgd return(0);
438 1.1 cgd case M_ONE:
439 1.1 cgd if (*name++ == EOS)
440 1.1 cgd return(0);
441 1.1 cgd break;
442 1.1 cgd case M_SET:
443 1.1 cgd ok = 0;
444 1.1 cgd k = *name++;
445 1.1 cgd if (negate_range = ((*pat & M_MASK) == M_NOT))
446 1.1 cgd ++pat;
447 1.1 cgd while (((c = *pat++) & M_MASK) != M_END)
448 1.1 cgd if ((*pat & M_MASK) == M_RNG) {
449 1.1 cgd if (c <= k && k <= pat[1])
450 1.1 cgd ok = 1;
451 1.1 cgd pat += 2;
452 1.1 cgd } else if (c == k)
453 1.1 cgd ok = 1;
454 1.1 cgd if (ok == negate_range)
455 1.1 cgd return(0);
456 1.1 cgd break;
457 1.1 cgd default:
458 1.1 cgd if (*name++ != c)
459 1.1 cgd return(0);
460 1.1 cgd break;
461 1.1 cgd }
462 1.1 cgd }
463 1.1 cgd return(*name == EOS);
464 1.1 cgd }
465 1.1 cgd
466 1.1 cgd /* Free allocated data belonging to a glob_t structure. */
467 1.1 cgd void
468 1.1 cgd globfree(pglob)
469 1.1 cgd glob_t *pglob;
470 1.1 cgd {
471 1.1 cgd register int i;
472 1.1 cgd register char **pp;
473 1.1 cgd
474 1.1 cgd if (pglob->gl_pathv != NULL) {
475 1.1 cgd pp = pglob->gl_pathv + pglob->gl_offs;
476 1.1 cgd for (i = pglob->gl_pathc; i--; ++pp)
477 1.1 cgd if (*pp)
478 1.1 cgd free(*pp);
479 1.1 cgd free(pglob->gl_pathv);
480 1.1 cgd }
481 1.1 cgd }
482 1.1 cgd
483 1.1 cgd static DIR *
484 1.1 cgd g_opendir(str)
485 1.1 cgd register Char *str;
486 1.1 cgd {
487 1.1 cgd char buf[MAXPATHLEN];
488 1.1 cgd
489 1.1 cgd if (!*str)
490 1.1 cgd return(opendir("."));
491 1.1 cgd g_Ctoc(str, buf);
492 1.1 cgd return(opendir(buf));
493 1.1 cgd }
494 1.1 cgd
495 1.1 cgd static int
496 1.1 cgd g_lstat(fn, sb)
497 1.1 cgd register Char *fn;
498 1.1 cgd struct stat *sb;
499 1.1 cgd {
500 1.1 cgd char buf[MAXPATHLEN];
501 1.1 cgd
502 1.1 cgd g_Ctoc(fn, buf);
503 1.1 cgd return(lstat(buf, sb));
504 1.1 cgd }
505 1.1 cgd
506 1.1 cgd static int
507 1.1 cgd g_stat(fn, sb)
508 1.1 cgd register Char *fn;
509 1.1 cgd struct stat *sb;
510 1.1 cgd {
511 1.1 cgd char buf[MAXPATHLEN];
512 1.1 cgd
513 1.1 cgd g_Ctoc(fn, buf);
514 1.1 cgd return(stat(buf, sb));
515 1.1 cgd }
516 1.1 cgd
517 1.1 cgd static Char *
518 1.1 cgd g_strchr(str, ch)
519 1.1 cgd Char *str;
520 1.1 cgd int ch;
521 1.1 cgd {
522 1.1 cgd do {
523 1.1 cgd if (*str == ch)
524 1.1 cgd return (str);
525 1.1 cgd } while (*str++);
526 1.1 cgd return (NULL);
527 1.1 cgd }
528 1.1 cgd
529 1.1 cgd static void
530 1.1 cgd g_Ctoc(str, buf)
531 1.1 cgd register Char *str;
532 1.1 cgd char *buf;
533 1.1 cgd {
534 1.1 cgd register char *dc;
535 1.1 cgd
536 1.1 cgd for (dc = buf; *dc++ = *str++;);
537 1.1 cgd }
538 1.1 cgd
539 1.1 cgd #ifdef DEBUG
540 1.1 cgd static void
541 1.1 cgd qprintf(s)
542 1.1 cgd register Char *s;
543 1.1 cgd {
544 1.1 cgd register Char *p;
545 1.1 cgd
546 1.1 cgd for (p = s; *p; p++)
547 1.1 cgd (void)printf("%c", *p & 0xff);
548 1.1 cgd (void)printf("\n");
549 1.1 cgd for (p = s; *p; p++)
550 1.1 cgd (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
551 1.1 cgd (void)printf("\n");
552 1.1 cgd for (p = s; *p; p++)
553 1.1 cgd (void)printf("%c", *p & M_META ? '_' : ' ');
554 1.1 cgd (void)printf("\n");
555 1.1 cgd }
556 1.1 cgd #endif
557