140087Sbostic /*
261111Sbostic * Copyright (c) 1989, 1993
361111Sbostic * The Regents of the University of California. All rights reserved.
440087Sbostic *
540087Sbostic * This code is derived from software contributed to Berkeley by
640087Sbostic * Guido van Rossum.
740087Sbostic *
842625Sbostic * %sccs.include.redist.c%
940087Sbostic */
1040087Sbostic
1140087Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*64713Sbostic static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93";
1340087Sbostic #endif /* LIBC_SCCS and not lint */
1440087Sbostic
1540087Sbostic /*
1650050Sbostic * glob(3) -- a superset of the one defined in POSIX 1003.2.
1740087Sbostic *
1840087Sbostic * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
1940087Sbostic *
2040087Sbostic * Optional extra services, controlled by flags not defined by POSIX:
2147590Sbostic *
2247590Sbostic * GLOB_QUOTE:
2347590Sbostic * Escaping convention: \ inhibits any special meaning the following
2447590Sbostic * character might have (except \ at end of string is retained).
2547590Sbostic * GLOB_MAGCHAR:
2648540Sbostic * Set in gl_flags if pattern contained a globbing character.
2750420Sbostic * GLOB_NOMAGIC:
2850420Sbostic * Same as GLOB_NOCHECK, but it will only append pattern if it did
2950420Sbostic * not contain any magic characters. [Used in csh style globbing]
3056942Smckusick * GLOB_ALTDIRFUNC:
3156942Smckusick * Use alternately specified directory access functions.
3257897Sbostic * GLOB_TILDE:
3357897Sbostic * expand ~user/foo to the /home/dir/of/user/foo
3457897Sbostic * GLOB_BRACE:
3557897Sbostic * expand {1,2}{a,b} to 1a 1b 2a 2b
3647590Sbostic * gl_matchc:
3747590Sbostic * Number of matches in the current invocation of glob.
3840087Sbostic */
3940087Sbostic
4040087Sbostic #include <sys/param.h>
4140087Sbostic #include <sys/stat.h>
4257897Sbostic
4357897Sbostic #include <ctype.h>
4440087Sbostic #include <dirent.h>
4557897Sbostic #include <errno.h>
4640087Sbostic #include <glob.h>
4757897Sbostic #include <pwd.h>
4840087Sbostic #include <stdio.h>
4946597Sdonn #include <stdlib.h>
5057897Sbostic #include <string.h>
5157897Sbostic #include <unistd.h>
5240087Sbostic
5340087Sbostic #define DOLLAR '$'
5440087Sbostic #define DOT '.'
5540087Sbostic #define EOS '\0'
5640087Sbostic #define LBRACKET '['
5740087Sbostic #define NOT '!'
5840087Sbostic #define QUESTION '?'
5940087Sbostic #define QUOTE '\\'
6040087Sbostic #define RANGE '-'
6140087Sbostic #define RBRACKET ']'
6240087Sbostic #define SEP '/'
6340087Sbostic #define STAR '*'
6440087Sbostic #define TILDE '~'
6540087Sbostic #define UNDERSCORE '_'
6657897Sbostic #define LBRACE '{'
6757897Sbostic #define RBRACE '}'
6857897Sbostic #define SLASH '/'
6957897Sbostic #define COMMA ','
7040087Sbostic
7157897Sbostic #ifndef DEBUG
7257897Sbostic
7350049Sbostic #define M_QUOTE 0x8000
7450049Sbostic #define M_PROTECT 0x4000
7548540Sbostic #define M_MASK 0xffff
7650121Sbostic #define M_ASCII 0x00ff
7748540Sbostic
7857897Sbostic typedef u_short Char;
7957897Sbostic
8057897Sbostic #else
8157897Sbostic
8257897Sbostic #define M_QUOTE 0x80
8357897Sbostic #define M_PROTECT 0x40
8457897Sbostic #define M_MASK 0xff
8557897Sbostic #define M_ASCII 0x7f
8657897Sbostic
8757897Sbostic typedef char Char;
8857897Sbostic
8957897Sbostic #endif
9057897Sbostic
9157897Sbostic
9257897Sbostic #define CHAR(c) ((Char)((c)&M_ASCII))
9357897Sbostic #define META(c) ((Char)((c)|M_QUOTE))
9440087Sbostic #define M_ALL META('*')
9540087Sbostic #define M_END META(']')
9640087Sbostic #define M_NOT META('!')
9740087Sbostic #define M_ONE META('?')
9840087Sbostic #define M_RNG META('-')
9940087Sbostic #define M_SET META('[')
10050049Sbostic #define ismeta(c) (((c)&M_QUOTE) != 0)
10140087Sbostic
10250049Sbostic
10350050Sbostic static int compare __P((const void *, const void *));
10457897Sbostic static void g_Ctoc __P((const Char *, char *));
10556942Smckusick static int g_lstat __P((Char *, struct stat *, glob_t *));
10656942Smckusick static DIR *g_opendir __P((Char *, glob_t *));
10750050Sbostic static Char *g_strchr __P((Char *, int));
10857897Sbostic #ifdef notdef
10957897Sbostic static Char *g_strcat __P((Char *, const Char *));
11057897Sbostic #endif
11156942Smckusick static int g_stat __P((Char *, struct stat *, glob_t *));
11257897Sbostic static int glob0 __P((const Char *, glob_t *));
11350050Sbostic static int glob1 __P((Char *, glob_t *));
11450050Sbostic static int glob2 __P((Char *, Char *, Char *, glob_t *));
11550050Sbostic static int glob3 __P((Char *, Char *, Char *, Char *, glob_t *));
11657897Sbostic static int globextend __P((const Char *, glob_t *));
11757897Sbostic static const Char * globtilde __P((const Char *, Char *, glob_t *));
11857897Sbostic static int globexp1 __P((const Char *, glob_t *));
11957897Sbostic static int globexp2 __P((const Char *, const Char *, glob_t *, int *));
12050050Sbostic static int match __P((Char *, Char *, Char *));
12150049Sbostic #ifdef DEBUG
12257897Sbostic static void qprintf __P((const char *, Char *));
12350049Sbostic #endif
12450049Sbostic
12557897Sbostic int
glob(pattern,flags,errfunc,pglob)12640087Sbostic glob(pattern, flags, errfunc, pglob)
12746597Sdonn const char *pattern;
12857897Sbostic int flags, (*errfunc) __P((const char *, int));
12940087Sbostic glob_t *pglob;
13040087Sbostic {
13157897Sbostic const u_char *patnext;
13257897Sbostic int c;
13357897Sbostic Char *bufnext, *bufend, patbuf[MAXPATHLEN+1];
13440087Sbostic
13550121Sbostic patnext = (u_char *) pattern;
13640087Sbostic if (!(flags & GLOB_APPEND)) {
13740087Sbostic pglob->gl_pathc = 0;
13840087Sbostic pglob->gl_pathv = NULL;
13940087Sbostic if (!(flags & GLOB_DOOFFS))
14040087Sbostic pglob->gl_offs = 0;
14140087Sbostic }
14247590Sbostic pglob->gl_flags = flags & ~GLOB_MAGCHAR;
14340087Sbostic pglob->gl_errfunc = errfunc;
14447590Sbostic pglob->gl_matchc = 0;
14540087Sbostic
14640087Sbostic bufnext = patbuf;
14750049Sbostic bufend = bufnext + MAXPATHLEN;
14850049Sbostic if (flags & GLOB_QUOTE) {
14950050Sbostic /* Protect the quoted characters. */
15050049Sbostic while (bufnext < bufend && (c = *patnext++) != EOS)
15150049Sbostic if (c == QUOTE) {
15250049Sbostic if ((c = *patnext++) == EOS) {
15350049Sbostic c = QUOTE;
15450049Sbostic --patnext;
15550049Sbostic }
15650049Sbostic *bufnext++ = c | M_PROTECT;
15750049Sbostic }
15850049Sbostic else
15950049Sbostic *bufnext++ = c;
16050049Sbostic }
16150049Sbostic else
16250049Sbostic while (bufnext < bufend && (c = *patnext++) != EOS)
16350049Sbostic *bufnext++ = c;
16450049Sbostic *bufnext = EOS;
16540087Sbostic
16657897Sbostic if (flags & GLOB_BRACE)
16757897Sbostic return globexp1(patbuf, pglob);
16857897Sbostic else
16957897Sbostic return glob0(patbuf, pglob);
17057897Sbostic }
17157897Sbostic
17257897Sbostic /*
17357897Sbostic * Expand recursively a glob {} pattern. When there is no more expansion
17457897Sbostic * invoke the standard globbing routine to glob the rest of the magic
17557897Sbostic * characters
17657897Sbostic */
globexp1(pattern,pglob)17757897Sbostic static int globexp1(pattern, pglob)
17857897Sbostic const Char *pattern;
17957897Sbostic glob_t *pglob;
18057897Sbostic {
18157897Sbostic const Char* ptr = pattern;
18257897Sbostic int rv;
18357897Sbostic
18457897Sbostic /* Protect a single {}, for find(1), like csh */
18557897Sbostic if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS)
18657897Sbostic return glob0(pattern, pglob);
18757897Sbostic
18857897Sbostic while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL)
18957897Sbostic if (!globexp2(ptr, pattern, pglob, &rv))
19057897Sbostic return rv;
19157897Sbostic
19257897Sbostic return glob0(pattern, pglob);
19357897Sbostic }
19457897Sbostic
19557897Sbostic
19657897Sbostic /*
19757897Sbostic * Recursive brace globbing helper. Tries to expand a single brace.
19857897Sbostic * If it succeeds then it invokes globexp1 with the new pattern.
19957897Sbostic * If it fails then it tries to glob the rest of the pattern and returns.
20057897Sbostic */
globexp2(ptr,pattern,pglob,rv)20157897Sbostic static int globexp2(ptr, pattern, pglob, rv)
20257897Sbostic const Char *ptr, *pattern;
20357897Sbostic glob_t *pglob;
20457897Sbostic int *rv;
20557897Sbostic {
20657897Sbostic int i;
20757897Sbostic Char *lm, *ls;
20857897Sbostic const Char *pe, *pm, *pl;
20957897Sbostic Char patbuf[MAXPATHLEN + 1];
21057897Sbostic
21157897Sbostic /* copy part up to the brace */
21257897Sbostic for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++)
21357897Sbostic continue;
21457897Sbostic ls = lm;
21557897Sbostic
21657897Sbostic /* Find the balanced brace */
21757897Sbostic for (i = 0, pe = ++ptr; *pe; pe++)
21857897Sbostic if (*pe == LBRACKET) {
21957897Sbostic /* Ignore everything between [] */
22057897Sbostic for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++)
22157897Sbostic continue;
22257897Sbostic if (*pe == EOS) {
22357897Sbostic /*
22457897Sbostic * We could not find a matching RBRACKET.
22557897Sbostic * Ignore and just look for RBRACE
22657897Sbostic */
22757897Sbostic pe = pm;
22857897Sbostic }
22957897Sbostic }
23057897Sbostic else if (*pe == LBRACE)
23157897Sbostic i++;
23257897Sbostic else if (*pe == RBRACE) {
23357897Sbostic if (i == 0)
23457897Sbostic break;
23557897Sbostic i--;
23657897Sbostic }
23757897Sbostic
23857897Sbostic /* Non matching braces; just glob the pattern */
23957897Sbostic if (i != 0 || *pe == EOS) {
24057897Sbostic *rv = glob0(patbuf, pglob);
24157897Sbostic return 0;
24257897Sbostic }
24357897Sbostic
24457897Sbostic for (i = 0, pl = pm = ptr; pm <= pe; pm++)
24557897Sbostic switch (*pm) {
24657897Sbostic case LBRACKET:
24757897Sbostic /* Ignore everything between [] */
24857897Sbostic for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++)
24957897Sbostic continue;
25057897Sbostic if (*pm == EOS) {
25157897Sbostic /*
25257897Sbostic * We could not find a matching RBRACKET.
25357897Sbostic * Ignore and just look for RBRACE
25457897Sbostic */
25557897Sbostic pm = pl;
25657897Sbostic }
25757897Sbostic break;
25857897Sbostic
25957897Sbostic case LBRACE:
26057897Sbostic i++;
26157897Sbostic break;
26257897Sbostic
26357897Sbostic case RBRACE:
26457897Sbostic if (i) {
26557897Sbostic i--;
26657897Sbostic break;
26757897Sbostic }
26857897Sbostic /* FALLTHROUGH */
26957897Sbostic case COMMA:
27057897Sbostic if (i && *pm == COMMA)
27157897Sbostic break;
27257897Sbostic else {
27357897Sbostic /* Append the current string */
27457897Sbostic for (lm = ls; (pl < pm); *lm++ = *pl++)
27557897Sbostic continue;
27657897Sbostic /*
27757897Sbostic * Append the rest of the pattern after the
27857897Sbostic * closing brace
27957897Sbostic */
28057897Sbostic for (pl = pe + 1; (*lm++ = *pl++) != EOS;)
28157897Sbostic continue;
28257897Sbostic
28357897Sbostic /* Expand the current pattern */
28457897Sbostic #ifdef DEBUG
28557897Sbostic qprintf("globexp2:", patbuf);
28657897Sbostic #endif
28757897Sbostic *rv = globexp1(patbuf, pglob);
28857897Sbostic
28957897Sbostic /* move after the comma, to the next string */
29057897Sbostic pl = pm + 1;
29157897Sbostic }
29257897Sbostic break;
29357897Sbostic
29457897Sbostic default:
29557897Sbostic break;
29657897Sbostic }
29757897Sbostic *rv = 0;
29857897Sbostic return 0;
29957897Sbostic }
30057897Sbostic
30157897Sbostic
30257897Sbostic
30357897Sbostic /*
30457897Sbostic * expand tilde from the passwd file.
30557897Sbostic */
30657897Sbostic static const Char *
globtilde(pattern,patbuf,pglob)30757897Sbostic globtilde(pattern, patbuf, pglob)
30857897Sbostic const Char *pattern;
30957897Sbostic Char *patbuf;
31057897Sbostic glob_t *pglob;
31157897Sbostic {
31257897Sbostic struct passwd *pwd;
31357897Sbostic char *h;
31457897Sbostic const Char *p;
31557897Sbostic Char *b;
31657897Sbostic
31757897Sbostic if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
31857897Sbostic return pattern;
31957897Sbostic
32057897Sbostic /* Copy up to the end of the string or / */
32157897Sbostic for (p = pattern + 1, h = (char *) patbuf; *p && *p != SLASH;
32257897Sbostic *h++ = *p++)
32357897Sbostic continue;
32457897Sbostic
32557897Sbostic *h = EOS;
32657897Sbostic
32757897Sbostic if (((char *) patbuf)[0] == EOS) {
32857897Sbostic /*
32957897Sbostic * handle a plain ~ or ~/ by expanding $HOME
33057897Sbostic * first and then trying the password file
33157897Sbostic */
33257897Sbostic if ((h = getenv("HOME")) == NULL) {
33357897Sbostic if ((pwd = getpwuid(getuid())) == NULL)
33457897Sbostic return pattern;
33557897Sbostic else
33657897Sbostic h = pwd->pw_dir;
33757897Sbostic }
33857897Sbostic }
33957897Sbostic else {
34057897Sbostic /*
34157897Sbostic * Expand a ~user
34257897Sbostic */
34357897Sbostic if ((pwd = getpwnam((char*) patbuf)) == NULL)
34457897Sbostic return pattern;
34557897Sbostic else
34657897Sbostic h = pwd->pw_dir;
34757897Sbostic }
34857897Sbostic
34957897Sbostic /* Copy the home directory */
35057897Sbostic for (b = patbuf; *h; *b++ = *h++)
35157897Sbostic continue;
35257897Sbostic
35357897Sbostic /* Append the rest of the pattern */
35457897Sbostic while ((*b++ = *p++) != EOS)
35557897Sbostic continue;
35657897Sbostic
35757897Sbostic return patbuf;
35857897Sbostic }
35957897Sbostic
36057897Sbostic
36157897Sbostic /*
36257897Sbostic * The main glob() routine: compiles the pattern (optionally processing
36357897Sbostic * quotes), calls glob1() to do the real pattern matching, and finally
36457897Sbostic * sorts the list (unless unsorted operation is requested). Returns 0
36557897Sbostic * if things went well, nonzero if errors occurred. It is not an error
36657897Sbostic * to find no matches.
36757897Sbostic */
36857897Sbostic static int
glob0(pattern,pglob)36957897Sbostic glob0(pattern, pglob)
37057897Sbostic const Char *pattern;
37157897Sbostic glob_t *pglob;
37257897Sbostic {
37357897Sbostic const Char *qpatnext;
37457897Sbostic int c, err, oldpathc;
37558077Sbostic Char *bufnext, patbuf[MAXPATHLEN+1];
37657897Sbostic
37757897Sbostic qpatnext = globtilde(pattern, patbuf, pglob);
37857897Sbostic oldpathc = pglob->gl_pathc;
37958077Sbostic bufnext = patbuf;
38057897Sbostic
38150050Sbostic /* We don't need to check for buffer overflow any more. */
38250049Sbostic while ((c = *qpatnext++) != EOS) {
38340087Sbostic switch (c) {
38440087Sbostic case LBRACKET:
38550049Sbostic c = *qpatnext;
38640087Sbostic if (c == NOT)
38750049Sbostic ++qpatnext;
38850049Sbostic if (*qpatnext == EOS ||
38957897Sbostic g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) {
39040087Sbostic *bufnext++ = LBRACKET;
39140087Sbostic if (c == NOT)
39250049Sbostic --qpatnext;
39340087Sbostic break;
39440087Sbostic }
39540087Sbostic *bufnext++ = M_SET;
39640087Sbostic if (c == NOT)
39740087Sbostic *bufnext++ = M_NOT;
39850049Sbostic c = *qpatnext++;
39940087Sbostic do {
40050121Sbostic *bufnext++ = CHAR(c);
40150049Sbostic if (*qpatnext == RANGE &&
40250049Sbostic (c = qpatnext[1]) != RBRACKET) {
40340087Sbostic *bufnext++ = M_RNG;
40450121Sbostic *bufnext++ = CHAR(c);
40550049Sbostic qpatnext += 2;
40640087Sbostic }
40750049Sbostic } while ((c = *qpatnext++) != RBRACKET);
40850420Sbostic pglob->gl_flags |= GLOB_MAGCHAR;
40940087Sbostic *bufnext++ = M_END;
41040087Sbostic break;
41140087Sbostic case QUESTION:
41247590Sbostic pglob->gl_flags |= GLOB_MAGCHAR;
41340087Sbostic *bufnext++ = M_ONE;
41440087Sbostic break;
41540087Sbostic case STAR:
41647590Sbostic pglob->gl_flags |= GLOB_MAGCHAR;
41756373Sbostic /* collapse adjacent stars to one,
41856373Sbostic * to avoid exponential behavior
41956373Sbostic */
42056373Sbostic if (bufnext == patbuf || bufnext[-1] != M_ALL)
42156373Sbostic *bufnext++ = M_ALL;
42240087Sbostic break;
42340087Sbostic default:
42450121Sbostic *bufnext++ = CHAR(c);
42540087Sbostic break;
42640087Sbostic }
42740087Sbostic }
42840087Sbostic *bufnext = EOS;
42950049Sbostic #ifdef DEBUG
43057897Sbostic qprintf("glob0:", patbuf);
43150049Sbostic #endif
43240087Sbostic
43340087Sbostic if ((err = glob1(patbuf, pglob)) != 0)
43440087Sbostic return(err);
43540087Sbostic
43650420Sbostic /*
43750420Sbostic * If there was no match we are going to append the pattern
43850420Sbostic * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified
43950420Sbostic * and the pattern did not contain any magic characters
44050420Sbostic * GLOB_NOMAGIC is there just for compatibility with csh.
44150420Sbostic */
44250420Sbostic if (pglob->gl_pathc == oldpathc &&
44357897Sbostic ((pglob->gl_flags & GLOB_NOCHECK) ||
444*64713Sbostic ((pglob->gl_flags & GLOB_NOMAGIC) &&
44557897Sbostic !(pglob->gl_flags & GLOB_MAGCHAR))))
44657897Sbostic return(globextend(pattern, pglob));
44757897Sbostic else if (!(pglob->gl_flags & GLOB_NOSORT))
44850050Sbostic qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
44950050Sbostic pglob->gl_pathc - oldpathc, sizeof(char *), compare);
45040087Sbostic return(0);
45140087Sbostic }
45240087Sbostic
45350050Sbostic static int
compare(p,q)45450050Sbostic compare(p, q)
45550050Sbostic const void *p, *q;
45650050Sbostic {
45750050Sbostic return(strcmp(*(char **)p, *(char **)q));
45850050Sbostic }
45950050Sbostic
46057897Sbostic static int
glob1(pattern,pglob)46140087Sbostic glob1(pattern, pglob)
46250050Sbostic Char *pattern;
46340087Sbostic glob_t *pglob;
46440087Sbostic {
46550050Sbostic Char pathbuf[MAXPATHLEN+1];
46640087Sbostic
46750050Sbostic /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
46840087Sbostic if (*pattern == EOS)
46940087Sbostic return(0);
47040087Sbostic return(glob2(pathbuf, pathbuf, pattern, pglob));
47140087Sbostic }
47240087Sbostic
47340087Sbostic /*
47450050Sbostic * The functions glob2 and glob3 are mutually recursive; there is one level
47550050Sbostic * of recursion for each segment in the pattern that contains one or more
47650050Sbostic * meta characters.
47740087Sbostic */
47857897Sbostic static int
glob2(pathbuf,pathend,pattern,pglob)47940087Sbostic glob2(pathbuf, pathend, pattern, pglob)
48050050Sbostic Char *pathbuf, *pathend, *pattern;
48140087Sbostic glob_t *pglob;
48240087Sbostic {
48350050Sbostic struct stat sb;
48450050Sbostic Char *p, *q;
48548540Sbostic int anymeta;
48640087Sbostic
48740087Sbostic /*
48850050Sbostic * Loop over pattern segments until end of pattern or until
48940087Sbostic * segment with meta character found.
49040087Sbostic */
49148357Sbostic for (anymeta = 0;;) {
49250050Sbostic if (*pattern == EOS) { /* End of pattern? */
49340087Sbostic *pathend = EOS;
49456942Smckusick if (g_lstat(pathbuf, &sb, pglob))
49548357Sbostic return(0);
49648540Sbostic
49748540Sbostic if (((pglob->gl_flags & GLOB_MARK) &&
49850050Sbostic pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
49950050Sbostic || (S_ISLNK(sb.st_mode) &&
50056942Smckusick (g_stat(pathbuf, &sb, pglob) == 0) &&
50150050Sbostic S_ISDIR(sb.st_mode)))) {
50240087Sbostic *pathend++ = SEP;
50340087Sbostic *pathend = EOS;
50440087Sbostic }
50547590Sbostic ++pglob->gl_matchc;
50640087Sbostic return(globextend(pathbuf, pglob));
50740087Sbostic }
50840087Sbostic
50950050Sbostic /* Find end of next segment, copy tentatively to pathend. */
51040087Sbostic q = pathend;
51140087Sbostic p = pattern;
51240087Sbostic while (*p != EOS && *p != SEP) {
51340087Sbostic if (ismeta(*p))
51440087Sbostic anymeta = 1;
51540087Sbostic *q++ = *p++;
51640087Sbostic }
51740087Sbostic
51850050Sbostic if (!anymeta) { /* No expansion, do next segment. */
51940087Sbostic pathend = q;
52040087Sbostic pattern = p;
52140087Sbostic while (*pattern == SEP)
52240087Sbostic *pathend++ = *pattern++;
52350050Sbostic } else /* Need expansion, recurse. */
52440087Sbostic return(glob3(pathbuf, pathend, pattern, p, pglob));
52540087Sbostic }
52640087Sbostic /* NOTREACHED */
52740087Sbostic }
52840087Sbostic
52957897Sbostic static int
glob3(pathbuf,pathend,pattern,restpattern,pglob)53040087Sbostic glob3(pathbuf, pathend, pattern, restpattern, pglob)
53150050Sbostic Char *pathbuf, *pathend, *pattern, *restpattern;
53240087Sbostic glob_t *pglob;
53340087Sbostic {
53450050Sbostic register struct dirent *dp;
53540087Sbostic DIR *dirp;
53657897Sbostic int err;
53757006Sbostic char buf[MAXPATHLEN];
53840087Sbostic
53957897Sbostic /*
54057897Sbostic * The readdirfunc declaration can't be prototyped, because it is
54157897Sbostic * assigned, below, to two functions which are prototyped in glob.h
54257897Sbostic * and dirent.h as taking pointers to differently typed opaque
54357897Sbostic * structures.
54457897Sbostic */
54557897Sbostic struct dirent *(*readdirfunc)();
54657897Sbostic
54740087Sbostic *pathend = EOS;
54840087Sbostic errno = 0;
54948540Sbostic
55057006Sbostic if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
55150050Sbostic /* TODO: don't call for ENOENT or ENOTDIR? */
55257006Sbostic if (pglob->gl_errfunc) {
55357006Sbostic g_Ctoc(pathbuf, buf);
55457006Sbostic if (pglob->gl_errfunc(buf, errno) ||
55557006Sbostic pglob->gl_flags & GLOB_ERR)
55657006Sbostic return (GLOB_ABEND);
55757006Sbostic }
55857006Sbostic return(0);
55957006Sbostic }
56040087Sbostic
56140087Sbostic err = 0;
56240087Sbostic
56350050Sbostic /* Search directory for matching names. */
56456942Smckusick if (pglob->gl_flags & GLOB_ALTDIRFUNC)
56556942Smckusick readdirfunc = pglob->gl_readdir;
56656942Smckusick else
56756942Smckusick readdirfunc = readdir;
56856942Smckusick while ((dp = (*readdirfunc)(dirp))) {
56950121Sbostic register u_char *sc;
57050050Sbostic register Char *dc;
57150050Sbostic
57250050Sbostic /* Initial DOT must be matched literally. */
57340087Sbostic if (dp->d_name[0] == DOT && *pattern != DOT)
57440087Sbostic continue;
57550121Sbostic for (sc = (u_char *) dp->d_name, dc = pathend;
57657897Sbostic (*dc++ = *sc++) != EOS;)
57757897Sbostic continue;
57848540Sbostic if (!match(pathend, pattern, restpattern)) {
57948540Sbostic *pathend = EOS;
58040087Sbostic continue;
58148540Sbostic }
58248540Sbostic err = glob2(pathbuf, --dc, restpattern, pglob);
58340087Sbostic if (err)
58440087Sbostic break;
58540087Sbostic }
58650050Sbostic
58756942Smckusick if (pglob->gl_flags & GLOB_ALTDIRFUNC)
58856942Smckusick (*pglob->gl_closedir)(dirp);
58956942Smckusick else
59056942Smckusick closedir(dirp);
59140087Sbostic return(err);
59240087Sbostic }
59340087Sbostic
59440087Sbostic
59540087Sbostic /*
59640087Sbostic * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
59740087Sbostic * add the new item, and update gl_pathc.
59840087Sbostic *
59940087Sbostic * This assumes the BSD realloc, which only copies the block when its size
60040087Sbostic * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
60140087Sbostic * behavior.
60240087Sbostic *
60340087Sbostic * Return 0 if new item added, error code if memory couldn't be allocated.
60440087Sbostic *
60540087Sbostic * Invariant of the glob_t structure:
60640087Sbostic * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
60750050Sbostic * gl_pathv points to (gl_offs + gl_pathc + 1) items.
60840087Sbostic */
60948540Sbostic static int
globextend(path,pglob)61040087Sbostic globextend(path, pglob)
61157897Sbostic const Char *path;
61240087Sbostic glob_t *pglob;
61340087Sbostic {
61440087Sbostic register char **pathv;
61540087Sbostic register int i;
61648540Sbostic u_int newsize;
61740087Sbostic char *copy;
61857897Sbostic const Char *p;
61940087Sbostic
62040087Sbostic newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
62157897Sbostic pathv = pglob->gl_pathv ?
62257897Sbostic realloc((char *)pglob->gl_pathv, newsize) :
62357897Sbostic malloc(newsize);
62440087Sbostic if (pathv == NULL)
62540087Sbostic return(GLOB_NOSPACE);
62640087Sbostic
62740087Sbostic if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
62840087Sbostic /* first time around -- clear initial gl_offs items */
62940087Sbostic pathv += pglob->gl_offs;
63040087Sbostic for (i = pglob->gl_offs; --i >= 0; )
63140087Sbostic *--pathv = NULL;
63240087Sbostic }
63340087Sbostic pglob->gl_pathv = pathv;
63440087Sbostic
63557897Sbostic for (p = path; *p++;)
63657897Sbostic continue;
63748540Sbostic if ((copy = malloc(p - path)) != NULL) {
63850050Sbostic g_Ctoc(path, copy);
63940087Sbostic pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
64040087Sbostic }
64140087Sbostic pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
64250050Sbostic return(copy == NULL ? GLOB_NOSPACE : 0);
64340087Sbostic }
64440087Sbostic
64540087Sbostic
64640087Sbostic /*
64740087Sbostic * pattern matching function for filenames. Each occurrence of the *
64840087Sbostic * pattern causes a recursion level.
64940087Sbostic */
65057897Sbostic static int
match(name,pat,patend)65140087Sbostic match(name, pat, patend)
65250050Sbostic register Char *name, *pat, *patend;
65340087Sbostic {
65448540Sbostic int ok, negate_range;
65550050Sbostic Char c, k;
65640087Sbostic
65740087Sbostic while (pat < patend) {
65840087Sbostic c = *pat++;
65948540Sbostic switch (c & M_MASK) {
66040087Sbostic case M_ALL:
66140087Sbostic if (pat == patend)
66240087Sbostic return(1);
66356373Sbostic do
66456373Sbostic if (match(name, pat, patend))
66556373Sbostic return(1);
66656373Sbostic while (*name++ != EOS);
66740087Sbostic return(0);
66840087Sbostic case M_ONE:
66940087Sbostic if (*name++ == EOS)
67040087Sbostic return(0);
67140087Sbostic break;
67240087Sbostic case M_SET:
67340087Sbostic ok = 0;
67452339Sbostic if ((k = *name++) == EOS)
67552339Sbostic return(0);
67657897Sbostic if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS)
67740087Sbostic ++pat;
67850050Sbostic while (((c = *pat++) & M_MASK) != M_END)
67948540Sbostic if ((*pat & M_MASK) == M_RNG) {
68040087Sbostic if (c <= k && k <= pat[1])
68140087Sbostic ok = 1;
68240087Sbostic pat += 2;
68350050Sbostic } else if (c == k)
68440087Sbostic ok = 1;
68540087Sbostic if (ok == negate_range)
68640087Sbostic return(0);
68740087Sbostic break;
68840087Sbostic default:
68940087Sbostic if (*name++ != c)
69040087Sbostic return(0);
69140087Sbostic break;
69240087Sbostic }
69340087Sbostic }
69440087Sbostic return(*name == EOS);
69540087Sbostic }
69640087Sbostic
69750050Sbostic /* Free allocated data belonging to a glob_t structure. */
69840087Sbostic void
globfree(pglob)69940087Sbostic globfree(pglob)
70040087Sbostic glob_t *pglob;
70140087Sbostic {
70240087Sbostic register int i;
70340087Sbostic register char **pp;
70440087Sbostic
70540087Sbostic if (pglob->gl_pathv != NULL) {
70640087Sbostic pp = pglob->gl_pathv + pglob->gl_offs;
70740087Sbostic for (i = pglob->gl_pathc; i--; ++pp)
70840087Sbostic if (*pp)
70950050Sbostic free(*pp);
71050049Sbostic free(pglob->gl_pathv);
71140087Sbostic }
71240087Sbostic }
71350050Sbostic
71450050Sbostic static DIR *
g_opendir(str,pglob)71556942Smckusick g_opendir(str, pglob)
71650050Sbostic register Char *str;
71756942Smckusick glob_t *pglob;
71850050Sbostic {
71950050Sbostic char buf[MAXPATHLEN];
72050050Sbostic
72150050Sbostic if (!*str)
72256942Smckusick strcpy(buf, ".");
72356942Smckusick else
72456942Smckusick g_Ctoc(str, buf);
72557897Sbostic
72656942Smckusick if (pglob->gl_flags & GLOB_ALTDIRFUNC)
72756942Smckusick return((*pglob->gl_opendir)(buf));
72857897Sbostic
72950050Sbostic return(opendir(buf));
73050050Sbostic }
73150050Sbostic
73250050Sbostic static int
g_lstat(fn,sb,pglob)73356942Smckusick g_lstat(fn, sb, pglob)
73450050Sbostic register Char *fn;
73550050Sbostic struct stat *sb;
73656942Smckusick glob_t *pglob;
73750050Sbostic {
73850050Sbostic char buf[MAXPATHLEN];
73950050Sbostic
74050050Sbostic g_Ctoc(fn, buf);
74156942Smckusick if (pglob->gl_flags & GLOB_ALTDIRFUNC)
74256942Smckusick return((*pglob->gl_lstat)(buf, sb));
74350050Sbostic return(lstat(buf, sb));
74450050Sbostic }
74550050Sbostic
74650050Sbostic static int
g_stat(fn,sb,pglob)74756942Smckusick g_stat(fn, sb, pglob)
74850050Sbostic register Char *fn;
74950050Sbostic struct stat *sb;
75056942Smckusick glob_t *pglob;
75150050Sbostic {
75250050Sbostic char buf[MAXPATHLEN];
75350050Sbostic
75450050Sbostic g_Ctoc(fn, buf);
75556942Smckusick if (pglob->gl_flags & GLOB_ALTDIRFUNC)
75656942Smckusick return((*pglob->gl_stat)(buf, sb));
75750050Sbostic return(stat(buf, sb));
75850050Sbostic }
75950050Sbostic
76050050Sbostic static Char *
g_strchr(str,ch)76150050Sbostic g_strchr(str, ch)
76250050Sbostic Char *str;
76350050Sbostic int ch;
76450050Sbostic {
76550050Sbostic do {
76650050Sbostic if (*str == ch)
76750050Sbostic return (str);
76850050Sbostic } while (*str++);
76950050Sbostic return (NULL);
77050050Sbostic }
77150050Sbostic
77257897Sbostic #ifdef notdef
77357897Sbostic static Char *
g_strcat(dst,src)77457897Sbostic g_strcat(dst, src)
77557897Sbostic Char *dst;
77657897Sbostic const Char* src;
77757897Sbostic {
77857897Sbostic Char *sdst = dst;
77957897Sbostic
78057897Sbostic while (*dst++)
78157897Sbostic continue;
78257897Sbostic --dst;
78357897Sbostic while((*dst++ = *src++) != EOS)
78457897Sbostic continue;
78557897Sbostic
78657897Sbostic return (sdst);
78757897Sbostic }
78857897Sbostic #endif
78957897Sbostic
79050050Sbostic static void
g_Ctoc(str,buf)79150050Sbostic g_Ctoc(str, buf)
79257897Sbostic register const Char *str;
79350050Sbostic char *buf;
79450050Sbostic {
79550050Sbostic register char *dc;
79650050Sbostic
79757897Sbostic for (dc = buf; (*dc++ = *str++) != EOS;)
79857897Sbostic continue;
79950050Sbostic }
80050050Sbostic
80150050Sbostic #ifdef DEBUG
80250050Sbostic static void
qprintf(str,s)80357897Sbostic qprintf(str, s)
80457897Sbostic const char *str;
80550050Sbostic register Char *s;
80650050Sbostic {
80750050Sbostic register Char *p;
80850050Sbostic
80957897Sbostic (void)printf("%s:\n", str);
81050050Sbostic for (p = s; *p; p++)
81157006Sbostic (void)printf("%c", CHAR(*p));
81250050Sbostic (void)printf("\n");
81350050Sbostic for (p = s; *p; p++)
81450050Sbostic (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
81550050Sbostic (void)printf("\n");
81650050Sbostic for (p = s; *p; p++)
81757006Sbostic (void)printf("%c", ismeta(*p) ? '_' : ' ');
81850050Sbostic (void)printf("\n");
81950050Sbostic }
82050050Sbostic #endif
821