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