glob.c revision 1573
11573Srgrimes/* 21573Srgrimes * Copyright (c) 1989, 1993 31573Srgrimes * The Regents of the University of California. All rights reserved. 41573Srgrimes * 51573Srgrimes * This code is derived from software contributed to Berkeley by 61573Srgrimes * Guido van Rossum. 71573Srgrimes * 81573Srgrimes * Redistribution and use in source and binary forms, with or without 91573Srgrimes * modification, are permitted provided that the following conditions 101573Srgrimes * are met: 111573Srgrimes * 1. Redistributions of source code must retain the above copyright 121573Srgrimes * notice, this list of conditions and the following disclaimer. 131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141573Srgrimes * notice, this list of conditions and the following disclaimer in the 151573Srgrimes * documentation and/or other materials provided with the distribution. 161573Srgrimes * 3. All advertising materials mentioning features or use of this software 171573Srgrimes * must display the following acknowledgement: 181573Srgrimes * This product includes software developed by the University of 191573Srgrimes * California, Berkeley and its contributors. 201573Srgrimes * 4. Neither the name of the University nor the names of its contributors 211573Srgrimes * may be used to endorse or promote products derived from this software 221573Srgrimes * without specific prior written permission. 231573Srgrimes * 241573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341573Srgrimes * SUCH DAMAGE. 351573Srgrimes */ 361573Srgrimes 371573Srgrimes#if defined(LIBC_SCCS) && !defined(lint) 381573Srgrimesstatic char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93"; 391573Srgrimes#endif /* LIBC_SCCS and not lint */ 401573Srgrimes 411573Srgrimes/* 421573Srgrimes * glob(3) -- a superset of the one defined in POSIX 1003.2. 431573Srgrimes * 441573Srgrimes * The [!...] convention to negate a range is supported (SysV, Posix, ksh). 451573Srgrimes * 461573Srgrimes * Optional extra services, controlled by flags not defined by POSIX: 471573Srgrimes * 481573Srgrimes * GLOB_QUOTE: 491573Srgrimes * Escaping convention: \ inhibits any special meaning the following 501573Srgrimes * character might have (except \ at end of string is retained). 511573Srgrimes * GLOB_MAGCHAR: 521573Srgrimes * Set in gl_flags if pattern contained a globbing character. 531573Srgrimes * GLOB_NOMAGIC: 541573Srgrimes * Same as GLOB_NOCHECK, but it will only append pattern if it did 551573Srgrimes * not contain any magic characters. [Used in csh style globbing] 561573Srgrimes * GLOB_ALTDIRFUNC: 571573Srgrimes * Use alternately specified directory access functions. 581573Srgrimes * GLOB_TILDE: 591573Srgrimes * expand ~user/foo to the /home/dir/of/user/foo 601573Srgrimes * GLOB_BRACE: 611573Srgrimes * expand {1,2}{a,b} to 1a 1b 2a 2b 621573Srgrimes * gl_matchc: 631573Srgrimes * Number of matches in the current invocation of glob. 641573Srgrimes */ 651573Srgrimes 661573Srgrimes#include <sys/param.h> 671573Srgrimes#include <sys/stat.h> 681573Srgrimes 691573Srgrimes#include <ctype.h> 701573Srgrimes#include <dirent.h> 711573Srgrimes#include <errno.h> 721573Srgrimes#include <glob.h> 731573Srgrimes#include <pwd.h> 741573Srgrimes#include <stdio.h> 751573Srgrimes#include <stdlib.h> 761573Srgrimes#include <string.h> 771573Srgrimes#include <unistd.h> 781573Srgrimes 791573Srgrimes#define DOLLAR '$' 801573Srgrimes#define DOT '.' 811573Srgrimes#define EOS '\0' 821573Srgrimes#define LBRACKET '[' 831573Srgrimes#define NOT '!' 841573Srgrimes#define QUESTION '?' 851573Srgrimes#define QUOTE '\\' 861573Srgrimes#define RANGE '-' 871573Srgrimes#define RBRACKET ']' 881573Srgrimes#define SEP '/' 891573Srgrimes#define STAR '*' 901573Srgrimes#define TILDE '~' 911573Srgrimes#define UNDERSCORE '_' 921573Srgrimes#define LBRACE '{' 931573Srgrimes#define RBRACE '}' 941573Srgrimes#define SLASH '/' 951573Srgrimes#define COMMA ',' 961573Srgrimes 971573Srgrimes#ifndef DEBUG 981573Srgrimes 991573Srgrimes#define M_QUOTE 0x8000 1001573Srgrimes#define M_PROTECT 0x4000 1011573Srgrimes#define M_MASK 0xffff 1021573Srgrimes#define M_ASCII 0x00ff 1031573Srgrimes 1041573Srgrimestypedef u_short Char; 1051573Srgrimes 1061573Srgrimes#else 1071573Srgrimes 1081573Srgrimes#define M_QUOTE 0x80 1091573Srgrimes#define M_PROTECT 0x40 1101573Srgrimes#define M_MASK 0xff 1111573Srgrimes#define M_ASCII 0x7f 1121573Srgrimes 1131573Srgrimestypedef char Char; 1141573Srgrimes 1151573Srgrimes#endif 1161573Srgrimes 1171573Srgrimes 1181573Srgrimes#define CHAR(c) ((Char)((c)&M_ASCII)) 1191573Srgrimes#define META(c) ((Char)((c)|M_QUOTE)) 1201573Srgrimes#define M_ALL META('*') 1211573Srgrimes#define M_END META(']') 1221573Srgrimes#define M_NOT META('!') 1231573Srgrimes#define M_ONE META('?') 1241573Srgrimes#define M_RNG META('-') 1251573Srgrimes#define M_SET META('[') 1261573Srgrimes#define ismeta(c) (((c)&M_QUOTE) != 0) 1271573Srgrimes 1281573Srgrimes 1291573Srgrimesstatic int compare __P((const void *, const void *)); 1301573Srgrimesstatic void g_Ctoc __P((const Char *, char *)); 1311573Srgrimesstatic int g_lstat __P((Char *, struct stat *, glob_t *)); 1321573Srgrimesstatic DIR *g_opendir __P((Char *, glob_t *)); 1331573Srgrimesstatic Char *g_strchr __P((Char *, int)); 1341573Srgrimes#ifdef notdef 1351573Srgrimesstatic Char *g_strcat __P((Char *, const Char *)); 1361573Srgrimes#endif 1371573Srgrimesstatic int g_stat __P((Char *, struct stat *, glob_t *)); 1381573Srgrimesstatic int glob0 __P((const Char *, glob_t *)); 1391573Srgrimesstatic int glob1 __P((Char *, glob_t *)); 1401573Srgrimesstatic int glob2 __P((Char *, Char *, Char *, glob_t *)); 1411573Srgrimesstatic int glob3 __P((Char *, Char *, Char *, Char *, glob_t *)); 1421573Srgrimesstatic int globextend __P((const Char *, glob_t *)); 1431573Srgrimesstatic const Char * globtilde __P((const Char *, Char *, glob_t *)); 1441573Srgrimesstatic int globexp1 __P((const Char *, glob_t *)); 1451573Srgrimesstatic int globexp2 __P((const Char *, const Char *, glob_t *, int *)); 1461573Srgrimesstatic int match __P((Char *, Char *, Char *)); 1471573Srgrimes#ifdef DEBUG 1481573Srgrimesstatic void qprintf __P((const char *, Char *)); 1491573Srgrimes#endif 1501573Srgrimes 1511573Srgrimesint 1521573Srgrimesglob(pattern, flags, errfunc, pglob) 1531573Srgrimes const char *pattern; 1541573Srgrimes int flags, (*errfunc) __P((const char *, int)); 1551573Srgrimes glob_t *pglob; 1561573Srgrimes{ 1571573Srgrimes const u_char *patnext; 1581573Srgrimes int c; 1591573Srgrimes Char *bufnext, *bufend, patbuf[MAXPATHLEN+1]; 1601573Srgrimes 1611573Srgrimes patnext = (u_char *) pattern; 1621573Srgrimes if (!(flags & GLOB_APPEND)) { 1631573Srgrimes pglob->gl_pathc = 0; 1641573Srgrimes pglob->gl_pathv = NULL; 1651573Srgrimes if (!(flags & GLOB_DOOFFS)) 1661573Srgrimes pglob->gl_offs = 0; 1671573Srgrimes } 1681573Srgrimes pglob->gl_flags = flags & ~GLOB_MAGCHAR; 1691573Srgrimes pglob->gl_errfunc = errfunc; 1701573Srgrimes pglob->gl_matchc = 0; 1711573Srgrimes 1721573Srgrimes bufnext = patbuf; 1731573Srgrimes bufend = bufnext + MAXPATHLEN; 1741573Srgrimes if (flags & GLOB_QUOTE) { 1751573Srgrimes /* Protect the quoted characters. */ 1761573Srgrimes while (bufnext < bufend && (c = *patnext++) != EOS) 1771573Srgrimes if (c == QUOTE) { 1781573Srgrimes if ((c = *patnext++) == EOS) { 1791573Srgrimes c = QUOTE; 1801573Srgrimes --patnext; 1811573Srgrimes } 1821573Srgrimes *bufnext++ = c | M_PROTECT; 1831573Srgrimes } 1841573Srgrimes else 1851573Srgrimes *bufnext++ = c; 1861573Srgrimes } 1871573Srgrimes else 1881573Srgrimes while (bufnext < bufend && (c = *patnext++) != EOS) 1891573Srgrimes *bufnext++ = c; 1901573Srgrimes *bufnext = EOS; 1911573Srgrimes 1921573Srgrimes if (flags & GLOB_BRACE) 1931573Srgrimes return globexp1(patbuf, pglob); 1941573Srgrimes else 1951573Srgrimes return glob0(patbuf, pglob); 1961573Srgrimes} 1971573Srgrimes 1981573Srgrimes/* 1991573Srgrimes * Expand recursively a glob {} pattern. When there is no more expansion 2001573Srgrimes * invoke the standard globbing routine to glob the rest of the magic 2011573Srgrimes * characters 2021573Srgrimes */ 2031573Srgrimesstatic int globexp1(pattern, pglob) 2041573Srgrimes const Char *pattern; 2051573Srgrimes glob_t *pglob; 2061573Srgrimes{ 2071573Srgrimes const Char* ptr = pattern; 2081573Srgrimes int rv; 2091573Srgrimes 2101573Srgrimes /* Protect a single {}, for find(1), like csh */ 2111573Srgrimes if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS) 2121573Srgrimes return glob0(pattern, pglob); 2131573Srgrimes 2141573Srgrimes while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL) 2151573Srgrimes if (!globexp2(ptr, pattern, pglob, &rv)) 2161573Srgrimes return rv; 2171573Srgrimes 2181573Srgrimes return glob0(pattern, pglob); 2191573Srgrimes} 2201573Srgrimes 2211573Srgrimes 2221573Srgrimes/* 2231573Srgrimes * Recursive brace globbing helper. Tries to expand a single brace. 2241573Srgrimes * If it succeeds then it invokes globexp1 with the new pattern. 2251573Srgrimes * If it fails then it tries to glob the rest of the pattern and returns. 2261573Srgrimes */ 2271573Srgrimesstatic int globexp2(ptr, pattern, pglob, rv) 2281573Srgrimes const Char *ptr, *pattern; 2291573Srgrimes glob_t *pglob; 2301573Srgrimes int *rv; 2311573Srgrimes{ 2321573Srgrimes int i; 2331573Srgrimes Char *lm, *ls; 2341573Srgrimes const Char *pe, *pm, *pl; 2351573Srgrimes Char patbuf[MAXPATHLEN + 1]; 2361573Srgrimes 2371573Srgrimes /* copy part up to the brace */ 2381573Srgrimes for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++) 2391573Srgrimes continue; 2401573Srgrimes ls = lm; 2411573Srgrimes 2421573Srgrimes /* Find the balanced brace */ 2431573Srgrimes for (i = 0, pe = ++ptr; *pe; pe++) 2441573Srgrimes if (*pe == LBRACKET) { 2451573Srgrimes /* Ignore everything between [] */ 2461573Srgrimes for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++) 2471573Srgrimes continue; 2481573Srgrimes if (*pe == EOS) { 2491573Srgrimes /* 2501573Srgrimes * We could not find a matching RBRACKET. 2511573Srgrimes * Ignore and just look for RBRACE 2521573Srgrimes */ 2531573Srgrimes pe = pm; 2541573Srgrimes } 2551573Srgrimes } 2561573Srgrimes else if (*pe == LBRACE) 2571573Srgrimes i++; 2581573Srgrimes else if (*pe == RBRACE) { 2591573Srgrimes if (i == 0) 2601573Srgrimes break; 2611573Srgrimes i--; 2621573Srgrimes } 2631573Srgrimes 2641573Srgrimes /* Non matching braces; just glob the pattern */ 2651573Srgrimes if (i != 0 || *pe == EOS) { 2661573Srgrimes *rv = glob0(patbuf, pglob); 2671573Srgrimes return 0; 2681573Srgrimes } 2691573Srgrimes 2701573Srgrimes for (i = 0, pl = pm = ptr; pm <= pe; pm++) 2711573Srgrimes switch (*pm) { 2721573Srgrimes case LBRACKET: 2731573Srgrimes /* Ignore everything between [] */ 2741573Srgrimes for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++) 2751573Srgrimes continue; 2761573Srgrimes if (*pm == EOS) { 2771573Srgrimes /* 2781573Srgrimes * We could not find a matching RBRACKET. 2791573Srgrimes * Ignore and just look for RBRACE 2801573Srgrimes */ 2811573Srgrimes pm = pl; 2821573Srgrimes } 2831573Srgrimes break; 2841573Srgrimes 2851573Srgrimes case LBRACE: 2861573Srgrimes i++; 2871573Srgrimes break; 2881573Srgrimes 2891573Srgrimes case RBRACE: 2901573Srgrimes if (i) { 2911573Srgrimes i--; 2921573Srgrimes break; 2931573Srgrimes } 2941573Srgrimes /* FALLTHROUGH */ 2951573Srgrimes case COMMA: 2961573Srgrimes if (i && *pm == COMMA) 2971573Srgrimes break; 2981573Srgrimes else { 2991573Srgrimes /* Append the current string */ 3001573Srgrimes for (lm = ls; (pl < pm); *lm++ = *pl++) 3011573Srgrimes continue; 3021573Srgrimes /* 3031573Srgrimes * Append the rest of the pattern after the 3041573Srgrimes * closing brace 3051573Srgrimes */ 3061573Srgrimes for (pl = pe + 1; (*lm++ = *pl++) != EOS;) 3071573Srgrimes continue; 3081573Srgrimes 3091573Srgrimes /* Expand the current pattern */ 3101573Srgrimes#ifdef DEBUG 3111573Srgrimes qprintf("globexp2:", patbuf); 3121573Srgrimes#endif 3131573Srgrimes *rv = globexp1(patbuf, pglob); 3141573Srgrimes 3151573Srgrimes /* move after the comma, to the next string */ 3161573Srgrimes pl = pm + 1; 3171573Srgrimes } 3181573Srgrimes break; 3191573Srgrimes 3201573Srgrimes default: 3211573Srgrimes break; 3221573Srgrimes } 3231573Srgrimes *rv = 0; 3241573Srgrimes return 0; 3251573Srgrimes} 3261573Srgrimes 3271573Srgrimes 3281573Srgrimes 3291573Srgrimes/* 3301573Srgrimes * expand tilde from the passwd file. 3311573Srgrimes */ 3321573Srgrimesstatic const Char * 3331573Srgrimesglobtilde(pattern, patbuf, pglob) 3341573Srgrimes const Char *pattern; 3351573Srgrimes Char *patbuf; 3361573Srgrimes glob_t *pglob; 3371573Srgrimes{ 3381573Srgrimes struct passwd *pwd; 3391573Srgrimes char *h; 3401573Srgrimes const Char *p; 3411573Srgrimes Char *b; 3421573Srgrimes 3431573Srgrimes if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE)) 3441573Srgrimes return pattern; 3451573Srgrimes 3461573Srgrimes /* Copy up to the end of the string or / */ 3471573Srgrimes for (p = pattern + 1, h = (char *) patbuf; *p && *p != SLASH; 3481573Srgrimes *h++ = *p++) 3491573Srgrimes continue; 3501573Srgrimes 3511573Srgrimes *h = EOS; 3521573Srgrimes 3531573Srgrimes if (((char *) patbuf)[0] == EOS) { 3541573Srgrimes /* 3551573Srgrimes * handle a plain ~ or ~/ by expanding $HOME 3561573Srgrimes * first and then trying the password file 3571573Srgrimes */ 3581573Srgrimes if ((h = getenv("HOME")) == NULL) { 3591573Srgrimes if ((pwd = getpwuid(getuid())) == NULL) 3601573Srgrimes return pattern; 3611573Srgrimes else 3621573Srgrimes h = pwd->pw_dir; 3631573Srgrimes } 3641573Srgrimes } 3651573Srgrimes else { 3661573Srgrimes /* 3671573Srgrimes * Expand a ~user 3681573Srgrimes */ 3691573Srgrimes if ((pwd = getpwnam((char*) patbuf)) == NULL) 3701573Srgrimes return pattern; 3711573Srgrimes else 3721573Srgrimes h = pwd->pw_dir; 3731573Srgrimes } 3741573Srgrimes 3751573Srgrimes /* Copy the home directory */ 3761573Srgrimes for (b = patbuf; *h; *b++ = *h++) 3771573Srgrimes continue; 3781573Srgrimes 3791573Srgrimes /* Append the rest of the pattern */ 3801573Srgrimes while ((*b++ = *p++) != EOS) 3811573Srgrimes continue; 3821573Srgrimes 3831573Srgrimes return patbuf; 3841573Srgrimes} 3851573Srgrimes 3861573Srgrimes 3871573Srgrimes/* 3881573Srgrimes * The main glob() routine: compiles the pattern (optionally processing 3891573Srgrimes * quotes), calls glob1() to do the real pattern matching, and finally 3901573Srgrimes * sorts the list (unless unsorted operation is requested). Returns 0 3911573Srgrimes * if things went well, nonzero if errors occurred. It is not an error 3921573Srgrimes * to find no matches. 3931573Srgrimes */ 3941573Srgrimesstatic int 3951573Srgrimesglob0(pattern, pglob) 3961573Srgrimes const Char *pattern; 3971573Srgrimes glob_t *pglob; 3981573Srgrimes{ 3991573Srgrimes const Char *qpatnext; 4001573Srgrimes int c, err, oldpathc; 4011573Srgrimes Char *bufnext, patbuf[MAXPATHLEN+1]; 4021573Srgrimes 4031573Srgrimes qpatnext = globtilde(pattern, patbuf, pglob); 4041573Srgrimes oldpathc = pglob->gl_pathc; 4051573Srgrimes bufnext = patbuf; 4061573Srgrimes 4071573Srgrimes /* We don't need to check for buffer overflow any more. */ 4081573Srgrimes while ((c = *qpatnext++) != EOS) { 4091573Srgrimes switch (c) { 4101573Srgrimes case LBRACKET: 4111573Srgrimes c = *qpatnext; 4121573Srgrimes if (c == NOT) 4131573Srgrimes ++qpatnext; 4141573Srgrimes if (*qpatnext == EOS || 4151573Srgrimes g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) { 4161573Srgrimes *bufnext++ = LBRACKET; 4171573Srgrimes if (c == NOT) 4181573Srgrimes --qpatnext; 4191573Srgrimes break; 4201573Srgrimes } 4211573Srgrimes *bufnext++ = M_SET; 4221573Srgrimes if (c == NOT) 4231573Srgrimes *bufnext++ = M_NOT; 4241573Srgrimes c = *qpatnext++; 4251573Srgrimes do { 4261573Srgrimes *bufnext++ = CHAR(c); 4271573Srgrimes if (*qpatnext == RANGE && 4281573Srgrimes (c = qpatnext[1]) != RBRACKET) { 4291573Srgrimes *bufnext++ = M_RNG; 4301573Srgrimes *bufnext++ = CHAR(c); 4311573Srgrimes qpatnext += 2; 4321573Srgrimes } 4331573Srgrimes } while ((c = *qpatnext++) != RBRACKET); 4341573Srgrimes pglob->gl_flags |= GLOB_MAGCHAR; 4351573Srgrimes *bufnext++ = M_END; 4361573Srgrimes break; 4371573Srgrimes case QUESTION: 4381573Srgrimes pglob->gl_flags |= GLOB_MAGCHAR; 4391573Srgrimes *bufnext++ = M_ONE; 4401573Srgrimes break; 4411573Srgrimes case STAR: 4421573Srgrimes pglob->gl_flags |= GLOB_MAGCHAR; 4431573Srgrimes /* collapse adjacent stars to one, 4441573Srgrimes * to avoid exponential behavior 4451573Srgrimes */ 4461573Srgrimes if (bufnext == patbuf || bufnext[-1] != M_ALL) 4471573Srgrimes *bufnext++ = M_ALL; 4481573Srgrimes break; 4491573Srgrimes default: 4501573Srgrimes *bufnext++ = CHAR(c); 4511573Srgrimes break; 4521573Srgrimes } 4531573Srgrimes } 4541573Srgrimes *bufnext = EOS; 4551573Srgrimes#ifdef DEBUG 4561573Srgrimes qprintf("glob0:", patbuf); 4571573Srgrimes#endif 4581573Srgrimes 4591573Srgrimes if ((err = glob1(patbuf, pglob)) != 0) 4601573Srgrimes return(err); 4611573Srgrimes 4621573Srgrimes /* 4631573Srgrimes * If there was no match we are going to append the pattern 4641573Srgrimes * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified 4651573Srgrimes * and the pattern did not contain any magic characters 4661573Srgrimes * GLOB_NOMAGIC is there just for compatibility with csh. 4671573Srgrimes */ 4681573Srgrimes if (pglob->gl_pathc == oldpathc && 4691573Srgrimes ((pglob->gl_flags & GLOB_NOCHECK) || 4701573Srgrimes ((pglob->gl_flags & GLOB_NOMAGIC) && 4711573Srgrimes !(pglob->gl_flags & GLOB_MAGCHAR)))) 4721573Srgrimes return(globextend(pattern, pglob)); 4731573Srgrimes else if (!(pglob->gl_flags & GLOB_NOSORT)) 4741573Srgrimes qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, 4751573Srgrimes pglob->gl_pathc - oldpathc, sizeof(char *), compare); 4761573Srgrimes return(0); 4771573Srgrimes} 4781573Srgrimes 4791573Srgrimesstatic int 4801573Srgrimescompare(p, q) 4811573Srgrimes const void *p, *q; 4821573Srgrimes{ 4831573Srgrimes return(strcmp(*(char **)p, *(char **)q)); 4841573Srgrimes} 4851573Srgrimes 4861573Srgrimesstatic int 4871573Srgrimesglob1(pattern, pglob) 4881573Srgrimes Char *pattern; 4891573Srgrimes glob_t *pglob; 4901573Srgrimes{ 4911573Srgrimes Char pathbuf[MAXPATHLEN+1]; 4921573Srgrimes 4931573Srgrimes /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ 4941573Srgrimes if (*pattern == EOS) 4951573Srgrimes return(0); 4961573Srgrimes return(glob2(pathbuf, pathbuf, pattern, pglob)); 4971573Srgrimes} 4981573Srgrimes 4991573Srgrimes/* 5001573Srgrimes * The functions glob2 and glob3 are mutually recursive; there is one level 5011573Srgrimes * of recursion for each segment in the pattern that contains one or more 5021573Srgrimes * meta characters. 5031573Srgrimes */ 5041573Srgrimesstatic int 5051573Srgrimesglob2(pathbuf, pathend, pattern, pglob) 5061573Srgrimes Char *pathbuf, *pathend, *pattern; 5071573Srgrimes glob_t *pglob; 5081573Srgrimes{ 5091573Srgrimes struct stat sb; 5101573Srgrimes Char *p, *q; 5111573Srgrimes int anymeta; 5121573Srgrimes 5131573Srgrimes /* 5141573Srgrimes * Loop over pattern segments until end of pattern or until 5151573Srgrimes * segment with meta character found. 5161573Srgrimes */ 5171573Srgrimes for (anymeta = 0;;) { 5181573Srgrimes if (*pattern == EOS) { /* End of pattern? */ 5191573Srgrimes *pathend = EOS; 5201573Srgrimes if (g_lstat(pathbuf, &sb, pglob)) 5211573Srgrimes return(0); 5221573Srgrimes 5231573Srgrimes if (((pglob->gl_flags & GLOB_MARK) && 5241573Srgrimes pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) 5251573Srgrimes || (S_ISLNK(sb.st_mode) && 5261573Srgrimes (g_stat(pathbuf, &sb, pglob) == 0) && 5271573Srgrimes S_ISDIR(sb.st_mode)))) { 5281573Srgrimes *pathend++ = SEP; 5291573Srgrimes *pathend = EOS; 5301573Srgrimes } 5311573Srgrimes ++pglob->gl_matchc; 5321573Srgrimes return(globextend(pathbuf, pglob)); 5331573Srgrimes } 5341573Srgrimes 5351573Srgrimes /* Find end of next segment, copy tentatively to pathend. */ 5361573Srgrimes q = pathend; 5371573Srgrimes p = pattern; 5381573Srgrimes while (*p != EOS && *p != SEP) { 5391573Srgrimes if (ismeta(*p)) 5401573Srgrimes anymeta = 1; 5411573Srgrimes *q++ = *p++; 5421573Srgrimes } 5431573Srgrimes 5441573Srgrimes if (!anymeta) { /* No expansion, do next segment. */ 5451573Srgrimes pathend = q; 5461573Srgrimes pattern = p; 5471573Srgrimes while (*pattern == SEP) 5481573Srgrimes *pathend++ = *pattern++; 5491573Srgrimes } else /* Need expansion, recurse. */ 5501573Srgrimes return(glob3(pathbuf, pathend, pattern, p, pglob)); 5511573Srgrimes } 5521573Srgrimes /* NOTREACHED */ 5531573Srgrimes} 5541573Srgrimes 5551573Srgrimesstatic int 5561573Srgrimesglob3(pathbuf, pathend, pattern, restpattern, pglob) 5571573Srgrimes Char *pathbuf, *pathend, *pattern, *restpattern; 5581573Srgrimes glob_t *pglob; 5591573Srgrimes{ 5601573Srgrimes register struct dirent *dp; 5611573Srgrimes DIR *dirp; 5621573Srgrimes int err; 5631573Srgrimes char buf[MAXPATHLEN]; 5641573Srgrimes 5651573Srgrimes /* 5661573Srgrimes * The readdirfunc declaration can't be prototyped, because it is 5671573Srgrimes * assigned, below, to two functions which are prototyped in glob.h 5681573Srgrimes * and dirent.h as taking pointers to differently typed opaque 5691573Srgrimes * structures. 5701573Srgrimes */ 5711573Srgrimes struct dirent *(*readdirfunc)(); 5721573Srgrimes 5731573Srgrimes *pathend = EOS; 5741573Srgrimes errno = 0; 5751573Srgrimes 5761573Srgrimes if ((dirp = g_opendir(pathbuf, pglob)) == NULL) { 5771573Srgrimes /* TODO: don't call for ENOENT or ENOTDIR? */ 5781573Srgrimes if (pglob->gl_errfunc) { 5791573Srgrimes g_Ctoc(pathbuf, buf); 5801573Srgrimes if (pglob->gl_errfunc(buf, errno) || 5811573Srgrimes pglob->gl_flags & GLOB_ERR) 5821573Srgrimes return (GLOB_ABEND); 5831573Srgrimes } 5841573Srgrimes return(0); 5851573Srgrimes } 5861573Srgrimes 5871573Srgrimes err = 0; 5881573Srgrimes 5891573Srgrimes /* Search directory for matching names. */ 5901573Srgrimes if (pglob->gl_flags & GLOB_ALTDIRFUNC) 5911573Srgrimes readdirfunc = pglob->gl_readdir; 5921573Srgrimes else 5931573Srgrimes readdirfunc = readdir; 5941573Srgrimes while ((dp = (*readdirfunc)(dirp))) { 5951573Srgrimes register u_char *sc; 5961573Srgrimes register Char *dc; 5971573Srgrimes 5981573Srgrimes /* Initial DOT must be matched literally. */ 5991573Srgrimes if (dp->d_name[0] == DOT && *pattern != DOT) 6001573Srgrimes continue; 6011573Srgrimes for (sc = (u_char *) dp->d_name, dc = pathend; 6021573Srgrimes (*dc++ = *sc++) != EOS;) 6031573Srgrimes continue; 6041573Srgrimes if (!match(pathend, pattern, restpattern)) { 6051573Srgrimes *pathend = EOS; 6061573Srgrimes continue; 6071573Srgrimes } 6081573Srgrimes err = glob2(pathbuf, --dc, restpattern, pglob); 6091573Srgrimes if (err) 6101573Srgrimes break; 6111573Srgrimes } 6121573Srgrimes 6131573Srgrimes if (pglob->gl_flags & GLOB_ALTDIRFUNC) 6141573Srgrimes (*pglob->gl_closedir)(dirp); 6151573Srgrimes else 6161573Srgrimes closedir(dirp); 6171573Srgrimes return(err); 6181573Srgrimes} 6191573Srgrimes 6201573Srgrimes 6211573Srgrimes/* 6221573Srgrimes * Extend the gl_pathv member of a glob_t structure to accomodate a new item, 6231573Srgrimes * add the new item, and update gl_pathc. 6241573Srgrimes * 6251573Srgrimes * This assumes the BSD realloc, which only copies the block when its size 6261573Srgrimes * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic 6271573Srgrimes * behavior. 6281573Srgrimes * 6291573Srgrimes * Return 0 if new item added, error code if memory couldn't be allocated. 6301573Srgrimes * 6311573Srgrimes * Invariant of the glob_t structure: 6321573Srgrimes * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and 6331573Srgrimes * gl_pathv points to (gl_offs + gl_pathc + 1) items. 6341573Srgrimes */ 6351573Srgrimesstatic int 6361573Srgrimesglobextend(path, pglob) 6371573Srgrimes const Char *path; 6381573Srgrimes glob_t *pglob; 6391573Srgrimes{ 6401573Srgrimes register char **pathv; 6411573Srgrimes register int i; 6421573Srgrimes u_int newsize; 6431573Srgrimes char *copy; 6441573Srgrimes const Char *p; 6451573Srgrimes 6461573Srgrimes newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs); 6471573Srgrimes pathv = pglob->gl_pathv ? 6481573Srgrimes realloc((char *)pglob->gl_pathv, newsize) : 6491573Srgrimes malloc(newsize); 6501573Srgrimes if (pathv == NULL) 6511573Srgrimes return(GLOB_NOSPACE); 6521573Srgrimes 6531573Srgrimes if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { 6541573Srgrimes /* first time around -- clear initial gl_offs items */ 6551573Srgrimes pathv += pglob->gl_offs; 6561573Srgrimes for (i = pglob->gl_offs; --i >= 0; ) 6571573Srgrimes *--pathv = NULL; 6581573Srgrimes } 6591573Srgrimes pglob->gl_pathv = pathv; 6601573Srgrimes 6611573Srgrimes for (p = path; *p++;) 6621573Srgrimes continue; 6631573Srgrimes if ((copy = malloc(p - path)) != NULL) { 6641573Srgrimes g_Ctoc(path, copy); 6651573Srgrimes pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; 6661573Srgrimes } 6671573Srgrimes pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; 6681573Srgrimes return(copy == NULL ? GLOB_NOSPACE : 0); 6691573Srgrimes} 6701573Srgrimes 6711573Srgrimes 6721573Srgrimes/* 6731573Srgrimes * pattern matching function for filenames. Each occurrence of the * 6741573Srgrimes * pattern causes a recursion level. 6751573Srgrimes */ 6761573Srgrimesstatic int 6771573Srgrimesmatch(name, pat, patend) 6781573Srgrimes register Char *name, *pat, *patend; 6791573Srgrimes{ 6801573Srgrimes int ok, negate_range; 6811573Srgrimes Char c, k; 6821573Srgrimes 6831573Srgrimes while (pat < patend) { 6841573Srgrimes c = *pat++; 6851573Srgrimes switch (c & M_MASK) { 6861573Srgrimes case M_ALL: 6871573Srgrimes if (pat == patend) 6881573Srgrimes return(1); 6891573Srgrimes do 6901573Srgrimes if (match(name, pat, patend)) 6911573Srgrimes return(1); 6921573Srgrimes while (*name++ != EOS); 6931573Srgrimes return(0); 6941573Srgrimes case M_ONE: 6951573Srgrimes if (*name++ == EOS) 6961573Srgrimes return(0); 6971573Srgrimes break; 6981573Srgrimes case M_SET: 6991573Srgrimes ok = 0; 7001573Srgrimes if ((k = *name++) == EOS) 7011573Srgrimes return(0); 7021573Srgrimes if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS) 7031573Srgrimes ++pat; 7041573Srgrimes while (((c = *pat++) & M_MASK) != M_END) 7051573Srgrimes if ((*pat & M_MASK) == M_RNG) { 7061573Srgrimes if (c <= k && k <= pat[1]) 7071573Srgrimes ok = 1; 7081573Srgrimes pat += 2; 7091573Srgrimes } else if (c == k) 7101573Srgrimes ok = 1; 7111573Srgrimes if (ok == negate_range) 7121573Srgrimes return(0); 7131573Srgrimes break; 7141573Srgrimes default: 7151573Srgrimes if (*name++ != c) 7161573Srgrimes return(0); 7171573Srgrimes break; 7181573Srgrimes } 7191573Srgrimes } 7201573Srgrimes return(*name == EOS); 7211573Srgrimes} 7221573Srgrimes 7231573Srgrimes/* Free allocated data belonging to a glob_t structure. */ 7241573Srgrimesvoid 7251573Srgrimesglobfree(pglob) 7261573Srgrimes glob_t *pglob; 7271573Srgrimes{ 7281573Srgrimes register int i; 7291573Srgrimes register char **pp; 7301573Srgrimes 7311573Srgrimes if (pglob->gl_pathv != NULL) { 7321573Srgrimes pp = pglob->gl_pathv + pglob->gl_offs; 7331573Srgrimes for (i = pglob->gl_pathc; i--; ++pp) 7341573Srgrimes if (*pp) 7351573Srgrimes free(*pp); 7361573Srgrimes free(pglob->gl_pathv); 7371573Srgrimes } 7381573Srgrimes} 7391573Srgrimes 7401573Srgrimesstatic DIR * 7411573Srgrimesg_opendir(str, pglob) 7421573Srgrimes register Char *str; 7431573Srgrimes glob_t *pglob; 7441573Srgrimes{ 7451573Srgrimes char buf[MAXPATHLEN]; 7461573Srgrimes 7471573Srgrimes if (!*str) 7481573Srgrimes strcpy(buf, "."); 7491573Srgrimes else 7501573Srgrimes g_Ctoc(str, buf); 7511573Srgrimes 7521573Srgrimes if (pglob->gl_flags & GLOB_ALTDIRFUNC) 7531573Srgrimes return((*pglob->gl_opendir)(buf)); 7541573Srgrimes 7551573Srgrimes return(opendir(buf)); 7561573Srgrimes} 7571573Srgrimes 7581573Srgrimesstatic int 7591573Srgrimesg_lstat(fn, sb, pglob) 7601573Srgrimes register Char *fn; 7611573Srgrimes struct stat *sb; 7621573Srgrimes glob_t *pglob; 7631573Srgrimes{ 7641573Srgrimes char buf[MAXPATHLEN]; 7651573Srgrimes 7661573Srgrimes g_Ctoc(fn, buf); 7671573Srgrimes if (pglob->gl_flags & GLOB_ALTDIRFUNC) 7681573Srgrimes return((*pglob->gl_lstat)(buf, sb)); 7691573Srgrimes return(lstat(buf, sb)); 7701573Srgrimes} 7711573Srgrimes 7721573Srgrimesstatic int 7731573Srgrimesg_stat(fn, sb, pglob) 7741573Srgrimes register Char *fn; 7751573Srgrimes struct stat *sb; 7761573Srgrimes glob_t *pglob; 7771573Srgrimes{ 7781573Srgrimes char buf[MAXPATHLEN]; 7791573Srgrimes 7801573Srgrimes g_Ctoc(fn, buf); 7811573Srgrimes if (pglob->gl_flags & GLOB_ALTDIRFUNC) 7821573Srgrimes return((*pglob->gl_stat)(buf, sb)); 7831573Srgrimes return(stat(buf, sb)); 7841573Srgrimes} 7851573Srgrimes 7861573Srgrimesstatic Char * 7871573Srgrimesg_strchr(str, ch) 7881573Srgrimes Char *str; 7891573Srgrimes int ch; 7901573Srgrimes{ 7911573Srgrimes do { 7921573Srgrimes if (*str == ch) 7931573Srgrimes return (str); 7941573Srgrimes } while (*str++); 7951573Srgrimes return (NULL); 7961573Srgrimes} 7971573Srgrimes 7981573Srgrimes#ifdef notdef 7991573Srgrimesstatic Char * 8001573Srgrimesg_strcat(dst, src) 8011573Srgrimes Char *dst; 8021573Srgrimes const Char* src; 8031573Srgrimes{ 8041573Srgrimes Char *sdst = dst; 8051573Srgrimes 8061573Srgrimes while (*dst++) 8071573Srgrimes continue; 8081573Srgrimes --dst; 8091573Srgrimes while((*dst++ = *src++) != EOS) 8101573Srgrimes continue; 8111573Srgrimes 8121573Srgrimes return (sdst); 8131573Srgrimes} 8141573Srgrimes#endif 8151573Srgrimes 8161573Srgrimesstatic void 8171573Srgrimesg_Ctoc(str, buf) 8181573Srgrimes register const Char *str; 8191573Srgrimes char *buf; 8201573Srgrimes{ 8211573Srgrimes register char *dc; 8221573Srgrimes 8231573Srgrimes for (dc = buf; (*dc++ = *str++) != EOS;) 8241573Srgrimes continue; 8251573Srgrimes} 8261573Srgrimes 8271573Srgrimes#ifdef DEBUG 8281573Srgrimesstatic void 8291573Srgrimesqprintf(str, s) 8301573Srgrimes const char *str; 8311573Srgrimes register Char *s; 8321573Srgrimes{ 8331573Srgrimes register Char *p; 8341573Srgrimes 8351573Srgrimes (void)printf("%s:\n", str); 8361573Srgrimes for (p = s; *p; p++) 8371573Srgrimes (void)printf("%c", CHAR(*p)); 8381573Srgrimes (void)printf("\n"); 8391573Srgrimes for (p = s; *p; p++) 8401573Srgrimes (void)printf("%c", *p & M_PROTECT ? '"' : ' '); 8411573Srgrimes (void)printf("\n"); 8421573Srgrimes for (p = s; *p; p++) 8431573Srgrimes (void)printf("%c", ismeta(*p) ? '_' : ' '); 8441573Srgrimes (void)printf("\n"); 8451573Srgrimes} 8461573Srgrimes#endif 847