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