140087Sbostic /* 2*61111Sbostic * Copyright (c) 1989, 1993 3*61111Sbostic * 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*61111Sbostic static char sccsid[] = "@(#)glob.c 8.1 (Berkeley) 06/04/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 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 */ 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 */ 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 * 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 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 37957897Sbostic oldpathc = pglob->gl_pathc; 38057897Sbostic pglob->gl_matchc = 0; 38157897Sbostic 38258077Sbostic bufnext = patbuf; 38357897Sbostic 38450050Sbostic /* We don't need to check for buffer overflow any more. */ 38550049Sbostic while ((c = *qpatnext++) != EOS) { 38640087Sbostic switch (c) { 38740087Sbostic case LBRACKET: 38850049Sbostic c = *qpatnext; 38940087Sbostic if (c == NOT) 39050049Sbostic ++qpatnext; 39150049Sbostic if (*qpatnext == EOS || 39257897Sbostic g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) { 39340087Sbostic *bufnext++ = LBRACKET; 39440087Sbostic if (c == NOT) 39550049Sbostic --qpatnext; 39640087Sbostic break; 39740087Sbostic } 39840087Sbostic *bufnext++ = M_SET; 39940087Sbostic if (c == NOT) 40040087Sbostic *bufnext++ = M_NOT; 40150049Sbostic c = *qpatnext++; 40240087Sbostic do { 40350121Sbostic *bufnext++ = CHAR(c); 40450049Sbostic if (*qpatnext == RANGE && 40550049Sbostic (c = qpatnext[1]) != RBRACKET) { 40640087Sbostic *bufnext++ = M_RNG; 40750121Sbostic *bufnext++ = CHAR(c); 40850049Sbostic qpatnext += 2; 40940087Sbostic } 41050049Sbostic } while ((c = *qpatnext++) != RBRACKET); 41150420Sbostic pglob->gl_flags |= GLOB_MAGCHAR; 41240087Sbostic *bufnext++ = M_END; 41340087Sbostic break; 41440087Sbostic case QUESTION: 41547590Sbostic pglob->gl_flags |= GLOB_MAGCHAR; 41640087Sbostic *bufnext++ = M_ONE; 41740087Sbostic break; 41840087Sbostic case STAR: 41947590Sbostic pglob->gl_flags |= GLOB_MAGCHAR; 42056373Sbostic /* collapse adjacent stars to one, 42156373Sbostic * to avoid exponential behavior 42256373Sbostic */ 42356373Sbostic if (bufnext == patbuf || bufnext[-1] != M_ALL) 42456373Sbostic *bufnext++ = M_ALL; 42540087Sbostic break; 42640087Sbostic default: 42750121Sbostic *bufnext++ = CHAR(c); 42840087Sbostic break; 42940087Sbostic } 43040087Sbostic } 43140087Sbostic *bufnext = EOS; 43250049Sbostic #ifdef DEBUG 43357897Sbostic qprintf("glob0:", patbuf); 43450049Sbostic #endif 43540087Sbostic 43640087Sbostic if ((err = glob1(patbuf, pglob)) != 0) 43740087Sbostic return(err); 43840087Sbostic 43950420Sbostic /* 44050420Sbostic * If there was no match we are going to append the pattern 44150420Sbostic * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified 44250420Sbostic * and the pattern did not contain any magic characters 44350420Sbostic * GLOB_NOMAGIC is there just for compatibility with csh. 44450420Sbostic */ 44550420Sbostic if (pglob->gl_pathc == oldpathc && 44657897Sbostic ((pglob->gl_flags & GLOB_NOCHECK) || 44757897Sbostic ((pglob->gl_flags & (GLOB_NOMAGIC|GLOB_BRACE|GLOB_TILDE)) || 44857897Sbostic !(pglob->gl_flags & GLOB_MAGCHAR)))) 44957897Sbostic return(globextend(pattern, pglob)); 45057897Sbostic else if (!(pglob->gl_flags & GLOB_NOSORT)) 45150050Sbostic qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, 45250050Sbostic pglob->gl_pathc - oldpathc, sizeof(char *), compare); 45340087Sbostic return(0); 45440087Sbostic } 45540087Sbostic 45650050Sbostic static int 45750050Sbostic compare(p, q) 45850050Sbostic const void *p, *q; 45950050Sbostic { 46050050Sbostic return(strcmp(*(char **)p, *(char **)q)); 46150050Sbostic } 46250050Sbostic 46357897Sbostic static int 46440087Sbostic glob1(pattern, pglob) 46550050Sbostic Char *pattern; 46640087Sbostic glob_t *pglob; 46740087Sbostic { 46850050Sbostic Char pathbuf[MAXPATHLEN+1]; 46940087Sbostic 47050050Sbostic /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ 47140087Sbostic if (*pattern == EOS) 47240087Sbostic return(0); 47340087Sbostic return(glob2(pathbuf, pathbuf, pattern, pglob)); 47440087Sbostic } 47540087Sbostic 47640087Sbostic /* 47750050Sbostic * The functions glob2 and glob3 are mutually recursive; there is one level 47850050Sbostic * of recursion for each segment in the pattern that contains one or more 47950050Sbostic * meta characters. 48040087Sbostic */ 48157897Sbostic static int 48240087Sbostic glob2(pathbuf, pathend, pattern, pglob) 48350050Sbostic Char *pathbuf, *pathend, *pattern; 48440087Sbostic glob_t *pglob; 48540087Sbostic { 48650050Sbostic struct stat sb; 48750050Sbostic Char *p, *q; 48848540Sbostic int anymeta; 48940087Sbostic 49040087Sbostic /* 49150050Sbostic * Loop over pattern segments until end of pattern or until 49240087Sbostic * segment with meta character found. 49340087Sbostic */ 49448357Sbostic for (anymeta = 0;;) { 49550050Sbostic if (*pattern == EOS) { /* End of pattern? */ 49640087Sbostic *pathend = EOS; 49756942Smckusick if (g_lstat(pathbuf, &sb, pglob)) 49848357Sbostic return(0); 49948540Sbostic 50048540Sbostic if (((pglob->gl_flags & GLOB_MARK) && 50150050Sbostic pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) 50250050Sbostic || (S_ISLNK(sb.st_mode) && 50356942Smckusick (g_stat(pathbuf, &sb, pglob) == 0) && 50450050Sbostic S_ISDIR(sb.st_mode)))) { 50540087Sbostic *pathend++ = SEP; 50640087Sbostic *pathend = EOS; 50740087Sbostic } 50847590Sbostic ++pglob->gl_matchc; 50940087Sbostic return(globextend(pathbuf, pglob)); 51040087Sbostic } 51140087Sbostic 51250050Sbostic /* Find end of next segment, copy tentatively to pathend. */ 51340087Sbostic q = pathend; 51440087Sbostic p = pattern; 51540087Sbostic while (*p != EOS && *p != SEP) { 51640087Sbostic if (ismeta(*p)) 51740087Sbostic anymeta = 1; 51840087Sbostic *q++ = *p++; 51940087Sbostic } 52040087Sbostic 52150050Sbostic if (!anymeta) { /* No expansion, do next segment. */ 52240087Sbostic pathend = q; 52340087Sbostic pattern = p; 52440087Sbostic while (*pattern == SEP) 52540087Sbostic *pathend++ = *pattern++; 52650050Sbostic } else /* Need expansion, recurse. */ 52740087Sbostic return(glob3(pathbuf, pathend, pattern, p, pglob)); 52840087Sbostic } 52940087Sbostic /* NOTREACHED */ 53040087Sbostic } 53140087Sbostic 53257897Sbostic static int 53340087Sbostic glob3(pathbuf, pathend, pattern, restpattern, pglob) 53450050Sbostic Char *pathbuf, *pathend, *pattern, *restpattern; 53540087Sbostic glob_t *pglob; 53640087Sbostic { 53750050Sbostic register struct dirent *dp; 53840087Sbostic DIR *dirp; 53957897Sbostic int err; 54057006Sbostic char buf[MAXPATHLEN]; 54140087Sbostic 54257897Sbostic /* 54357897Sbostic * The readdirfunc declaration can't be prototyped, because it is 54457897Sbostic * assigned, below, to two functions which are prototyped in glob.h 54557897Sbostic * and dirent.h as taking pointers to differently typed opaque 54657897Sbostic * structures. 54757897Sbostic */ 54857897Sbostic struct dirent *(*readdirfunc)(); 54957897Sbostic 55040087Sbostic *pathend = EOS; 55140087Sbostic errno = 0; 55248540Sbostic 55357006Sbostic if ((dirp = g_opendir(pathbuf, pglob)) == NULL) { 55450050Sbostic /* TODO: don't call for ENOENT or ENOTDIR? */ 55557006Sbostic if (pglob->gl_errfunc) { 55657006Sbostic g_Ctoc(pathbuf, buf); 55757006Sbostic if (pglob->gl_errfunc(buf, errno) || 55857006Sbostic pglob->gl_flags & GLOB_ERR) 55957006Sbostic return (GLOB_ABEND); 56057006Sbostic } 56157006Sbostic return(0); 56257006Sbostic } 56340087Sbostic 56440087Sbostic err = 0; 56540087Sbostic 56650050Sbostic /* Search directory for matching names. */ 56756942Smckusick if (pglob->gl_flags & GLOB_ALTDIRFUNC) 56856942Smckusick readdirfunc = pglob->gl_readdir; 56956942Smckusick else 57056942Smckusick readdirfunc = readdir; 57156942Smckusick while ((dp = (*readdirfunc)(dirp))) { 57250121Sbostic register u_char *sc; 57350050Sbostic register Char *dc; 57450050Sbostic 57550050Sbostic /* Initial DOT must be matched literally. */ 57640087Sbostic if (dp->d_name[0] == DOT && *pattern != DOT) 57740087Sbostic continue; 57850121Sbostic for (sc = (u_char *) dp->d_name, dc = pathend; 57957897Sbostic (*dc++ = *sc++) != EOS;) 58057897Sbostic continue; 58148540Sbostic if (!match(pathend, pattern, restpattern)) { 58248540Sbostic *pathend = EOS; 58340087Sbostic continue; 58448540Sbostic } 58548540Sbostic err = glob2(pathbuf, --dc, restpattern, pglob); 58640087Sbostic if (err) 58740087Sbostic break; 58840087Sbostic } 58950050Sbostic 59056942Smckusick if (pglob->gl_flags & GLOB_ALTDIRFUNC) 59156942Smckusick (*pglob->gl_closedir)(dirp); 59256942Smckusick else 59356942Smckusick closedir(dirp); 59440087Sbostic return(err); 59540087Sbostic } 59640087Sbostic 59740087Sbostic 59840087Sbostic /* 59940087Sbostic * Extend the gl_pathv member of a glob_t structure to accomodate a new item, 60040087Sbostic * add the new item, and update gl_pathc. 60140087Sbostic * 60240087Sbostic * This assumes the BSD realloc, which only copies the block when its size 60340087Sbostic * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic 60440087Sbostic * behavior. 60540087Sbostic * 60640087Sbostic * Return 0 if new item added, error code if memory couldn't be allocated. 60740087Sbostic * 60840087Sbostic * Invariant of the glob_t structure: 60940087Sbostic * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and 61050050Sbostic * gl_pathv points to (gl_offs + gl_pathc + 1) items. 61140087Sbostic */ 61248540Sbostic static int 61340087Sbostic globextend(path, pglob) 61457897Sbostic const Char *path; 61540087Sbostic glob_t *pglob; 61640087Sbostic { 61740087Sbostic register char **pathv; 61840087Sbostic register int i; 61948540Sbostic u_int newsize; 62040087Sbostic char *copy; 62157897Sbostic const Char *p; 62240087Sbostic 62340087Sbostic newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs); 62457897Sbostic pathv = pglob->gl_pathv ? 62557897Sbostic realloc((char *)pglob->gl_pathv, newsize) : 62657897Sbostic malloc(newsize); 62740087Sbostic if (pathv == NULL) 62840087Sbostic return(GLOB_NOSPACE); 62940087Sbostic 63040087Sbostic if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { 63140087Sbostic /* first time around -- clear initial gl_offs items */ 63240087Sbostic pathv += pglob->gl_offs; 63340087Sbostic for (i = pglob->gl_offs; --i >= 0; ) 63440087Sbostic *--pathv = NULL; 63540087Sbostic } 63640087Sbostic pglob->gl_pathv = pathv; 63740087Sbostic 63857897Sbostic for (p = path; *p++;) 63957897Sbostic continue; 64048540Sbostic if ((copy = malloc(p - path)) != NULL) { 64150050Sbostic g_Ctoc(path, copy); 64240087Sbostic pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; 64340087Sbostic } 64440087Sbostic pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; 64550050Sbostic return(copy == NULL ? GLOB_NOSPACE : 0); 64640087Sbostic } 64740087Sbostic 64840087Sbostic 64940087Sbostic /* 65040087Sbostic * pattern matching function for filenames. Each occurrence of the * 65140087Sbostic * pattern causes a recursion level. 65240087Sbostic */ 65357897Sbostic static int 65440087Sbostic match(name, pat, patend) 65550050Sbostic register Char *name, *pat, *patend; 65640087Sbostic { 65748540Sbostic int ok, negate_range; 65850050Sbostic Char c, k; 65940087Sbostic 66040087Sbostic while (pat < patend) { 66140087Sbostic c = *pat++; 66248540Sbostic switch (c & M_MASK) { 66340087Sbostic case M_ALL: 66440087Sbostic if (pat == patend) 66540087Sbostic return(1); 66656373Sbostic do 66756373Sbostic if (match(name, pat, patend)) 66856373Sbostic return(1); 66956373Sbostic while (*name++ != EOS); 67040087Sbostic return(0); 67140087Sbostic case M_ONE: 67240087Sbostic if (*name++ == EOS) 67340087Sbostic return(0); 67440087Sbostic break; 67540087Sbostic case M_SET: 67640087Sbostic ok = 0; 67752339Sbostic if ((k = *name++) == EOS) 67852339Sbostic return(0); 67957897Sbostic if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS) 68040087Sbostic ++pat; 68150050Sbostic while (((c = *pat++) & M_MASK) != M_END) 68248540Sbostic if ((*pat & M_MASK) == M_RNG) { 68340087Sbostic if (c <= k && k <= pat[1]) 68440087Sbostic ok = 1; 68540087Sbostic pat += 2; 68650050Sbostic } else if (c == k) 68740087Sbostic ok = 1; 68840087Sbostic if (ok == negate_range) 68940087Sbostic return(0); 69040087Sbostic break; 69140087Sbostic default: 69240087Sbostic if (*name++ != c) 69340087Sbostic return(0); 69440087Sbostic break; 69540087Sbostic } 69640087Sbostic } 69740087Sbostic return(*name == EOS); 69840087Sbostic } 69940087Sbostic 70050050Sbostic /* Free allocated data belonging to a glob_t structure. */ 70140087Sbostic void 70240087Sbostic globfree(pglob) 70340087Sbostic glob_t *pglob; 70440087Sbostic { 70540087Sbostic register int i; 70640087Sbostic register char **pp; 70740087Sbostic 70840087Sbostic if (pglob->gl_pathv != NULL) { 70940087Sbostic pp = pglob->gl_pathv + pglob->gl_offs; 71040087Sbostic for (i = pglob->gl_pathc; i--; ++pp) 71140087Sbostic if (*pp) 71250050Sbostic free(*pp); 71350049Sbostic free(pglob->gl_pathv); 71440087Sbostic } 71540087Sbostic } 71650050Sbostic 71750050Sbostic static DIR * 71856942Smckusick g_opendir(str, pglob) 71950050Sbostic register Char *str; 72056942Smckusick glob_t *pglob; 72150050Sbostic { 72250050Sbostic char buf[MAXPATHLEN]; 72350050Sbostic 72450050Sbostic if (!*str) 72556942Smckusick strcpy(buf, "."); 72656942Smckusick else 72756942Smckusick g_Ctoc(str, buf); 72857897Sbostic 72956942Smckusick if (pglob->gl_flags & GLOB_ALTDIRFUNC) 73056942Smckusick return((*pglob->gl_opendir)(buf)); 73157897Sbostic 73250050Sbostic return(opendir(buf)); 73350050Sbostic } 73450050Sbostic 73550050Sbostic static int 73656942Smckusick g_lstat(fn, sb, pglob) 73750050Sbostic register Char *fn; 73850050Sbostic struct stat *sb; 73956942Smckusick glob_t *pglob; 74050050Sbostic { 74150050Sbostic char buf[MAXPATHLEN]; 74250050Sbostic 74350050Sbostic g_Ctoc(fn, buf); 74456942Smckusick if (pglob->gl_flags & GLOB_ALTDIRFUNC) 74556942Smckusick return((*pglob->gl_lstat)(buf, sb)); 74650050Sbostic return(lstat(buf, sb)); 74750050Sbostic } 74850050Sbostic 74950050Sbostic static int 75056942Smckusick g_stat(fn, sb, pglob) 75150050Sbostic register Char *fn; 75250050Sbostic struct stat *sb; 75356942Smckusick glob_t *pglob; 75450050Sbostic { 75550050Sbostic char buf[MAXPATHLEN]; 75650050Sbostic 75750050Sbostic g_Ctoc(fn, buf); 75856942Smckusick if (pglob->gl_flags & GLOB_ALTDIRFUNC) 75956942Smckusick return((*pglob->gl_stat)(buf, sb)); 76050050Sbostic return(stat(buf, sb)); 76150050Sbostic } 76250050Sbostic 76350050Sbostic static Char * 76450050Sbostic g_strchr(str, ch) 76550050Sbostic Char *str; 76650050Sbostic int ch; 76750050Sbostic { 76850050Sbostic do { 76950050Sbostic if (*str == ch) 77050050Sbostic return (str); 77150050Sbostic } while (*str++); 77250050Sbostic return (NULL); 77350050Sbostic } 77450050Sbostic 77557897Sbostic #ifdef notdef 77657897Sbostic static Char * 77757897Sbostic g_strcat(dst, src) 77857897Sbostic Char *dst; 77957897Sbostic const Char* src; 78057897Sbostic { 78157897Sbostic Char *sdst = dst; 78257897Sbostic 78357897Sbostic while (*dst++) 78457897Sbostic continue; 78557897Sbostic --dst; 78657897Sbostic while((*dst++ = *src++) != EOS) 78757897Sbostic continue; 78857897Sbostic 78957897Sbostic return (sdst); 79057897Sbostic } 79157897Sbostic #endif 79257897Sbostic 79350050Sbostic static void 79450050Sbostic g_Ctoc(str, buf) 79557897Sbostic register const Char *str; 79650050Sbostic char *buf; 79750050Sbostic { 79850050Sbostic register char *dc; 79950050Sbostic 80057897Sbostic for (dc = buf; (*dc++ = *str++) != EOS;) 80157897Sbostic continue; 80250050Sbostic } 80350050Sbostic 80450050Sbostic #ifdef DEBUG 80550050Sbostic static void 80657897Sbostic qprintf(str, s) 80757897Sbostic const char *str; 80850050Sbostic register Char *s; 80950050Sbostic { 81050050Sbostic register Char *p; 81150050Sbostic 81257897Sbostic (void)printf("%s:\n", str); 81350050Sbostic for (p = s; *p; p++) 81457006Sbostic (void)printf("%c", CHAR(*p)); 81550050Sbostic (void)printf("\n"); 81650050Sbostic for (p = s; *p; p++) 81750050Sbostic (void)printf("%c", *p & M_PROTECT ? '"' : ' '); 81850050Sbostic (void)printf("\n"); 81950050Sbostic for (p = s; *p; p++) 82057006Sbostic (void)printf("%c", ismeta(*p) ? '_' : ' '); 82150050Sbostic (void)printf("\n"); 82250050Sbostic } 82350050Sbostic #endif 824