158f0484fSRodney W. Grimes /* 258f0484fSRodney W. Grimes * Copyright (c) 1989, 1993 358f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 458f0484fSRodney W. Grimes * 558f0484fSRodney W. Grimes * This code is derived from software contributed to Berkeley by 658f0484fSRodney W. Grimes * Guido van Rossum. 758f0484fSRodney W. Grimes * 858f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 958f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 1058f0484fSRodney W. Grimes * are met: 1158f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 1258f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1358f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1458f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1558f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1658f0484fSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 1758f0484fSRodney W. Grimes * must display the following acknowledgement: 1858f0484fSRodney W. Grimes * This product includes software developed by the University of 1958f0484fSRodney W. Grimes * California, Berkeley and its contributors. 2058f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 2158f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 2258f0484fSRodney W. Grimes * without specific prior written permission. 2358f0484fSRodney W. Grimes * 2458f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2558f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2658f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2758f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2858f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2958f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3058f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3158f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3258f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3358f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3458f0484fSRodney W. Grimes * SUCH DAMAGE. 3558f0484fSRodney W. Grimes */ 3658f0484fSRodney W. Grimes 3758f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 3858f0484fSRodney W. Grimes static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93"; 3958f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 4058f0484fSRodney W. Grimes 4158f0484fSRodney W. Grimes /* 4258f0484fSRodney W. Grimes * glob(3) -- a superset of the one defined in POSIX 1003.2. 4358f0484fSRodney W. Grimes * 4458f0484fSRodney W. Grimes * The [!...] convention to negate a range is supported (SysV, Posix, ksh). 4558f0484fSRodney W. Grimes * 4658f0484fSRodney W. Grimes * Optional extra services, controlled by flags not defined by POSIX: 4758f0484fSRodney W. Grimes * 4858f0484fSRodney W. Grimes * GLOB_QUOTE: 4958f0484fSRodney W. Grimes * Escaping convention: \ inhibits any special meaning the following 5058f0484fSRodney W. Grimes * character might have (except \ at end of string is retained). 5158f0484fSRodney W. Grimes * GLOB_MAGCHAR: 5258f0484fSRodney W. Grimes * Set in gl_flags if pattern contained a globbing character. 5358f0484fSRodney W. Grimes * GLOB_NOMAGIC: 5458f0484fSRodney W. Grimes * Same as GLOB_NOCHECK, but it will only append pattern if it did 5558f0484fSRodney W. Grimes * not contain any magic characters. [Used in csh style globbing] 5658f0484fSRodney W. Grimes * GLOB_ALTDIRFUNC: 5758f0484fSRodney W. Grimes * Use alternately specified directory access functions. 5858f0484fSRodney W. Grimes * GLOB_TILDE: 5958f0484fSRodney W. Grimes * expand ~user/foo to the /home/dir/of/user/foo 6058f0484fSRodney W. Grimes * GLOB_BRACE: 6158f0484fSRodney W. Grimes * expand {1,2}{a,b} to 1a 1b 2a 2b 6258f0484fSRodney W. Grimes * gl_matchc: 6358f0484fSRodney W. Grimes * Number of matches in the current invocation of glob. 6458f0484fSRodney W. Grimes */ 6558f0484fSRodney W. Grimes 6658f0484fSRodney W. Grimes #include <sys/param.h> 6758f0484fSRodney W. Grimes #include <sys/stat.h> 6858f0484fSRodney W. Grimes 6958f0484fSRodney W. Grimes #include <ctype.h> 7058f0484fSRodney W. Grimes #include <dirent.h> 7158f0484fSRodney W. Grimes #include <errno.h> 7258f0484fSRodney W. Grimes #include <glob.h> 7358f0484fSRodney W. Grimes #include <pwd.h> 7458f0484fSRodney W. Grimes #include <stdio.h> 7558f0484fSRodney W. Grimes #include <stdlib.h> 7658f0484fSRodney W. Grimes #include <string.h> 7758f0484fSRodney W. Grimes #include <unistd.h> 7858f0484fSRodney W. Grimes 7958f0484fSRodney W. Grimes #define DOLLAR '$' 8058f0484fSRodney W. Grimes #define DOT '.' 8158f0484fSRodney W. Grimes #define EOS '\0' 8258f0484fSRodney W. Grimes #define LBRACKET '[' 8358f0484fSRodney W. Grimes #define NOT '!' 8458f0484fSRodney W. Grimes #define QUESTION '?' 8558f0484fSRodney W. Grimes #define QUOTE '\\' 8658f0484fSRodney W. Grimes #define RANGE '-' 8758f0484fSRodney W. Grimes #define RBRACKET ']' 8858f0484fSRodney W. Grimes #define SEP '/' 8958f0484fSRodney W. Grimes #define STAR '*' 9058f0484fSRodney W. Grimes #define TILDE '~' 9158f0484fSRodney W. Grimes #define UNDERSCORE '_' 9258f0484fSRodney W. Grimes #define LBRACE '{' 9358f0484fSRodney W. Grimes #define RBRACE '}' 9458f0484fSRodney W. Grimes #define SLASH '/' 9558f0484fSRodney W. Grimes #define COMMA ',' 9658f0484fSRodney W. Grimes 9758f0484fSRodney W. Grimes #ifndef DEBUG 9858f0484fSRodney W. Grimes 9958f0484fSRodney W. Grimes #define M_QUOTE 0x8000 10058f0484fSRodney W. Grimes #define M_PROTECT 0x4000 10158f0484fSRodney W. Grimes #define M_MASK 0xffff 10258f0484fSRodney W. Grimes #define M_ASCII 0x00ff 10358f0484fSRodney W. Grimes 10458f0484fSRodney W. Grimes typedef u_short Char; 10558f0484fSRodney W. Grimes 10658f0484fSRodney W. Grimes #else 10758f0484fSRodney W. Grimes 10858f0484fSRodney W. Grimes #define M_QUOTE 0x80 10958f0484fSRodney W. Grimes #define M_PROTECT 0x40 11058f0484fSRodney W. Grimes #define M_MASK 0xff 11158f0484fSRodney W. Grimes #define M_ASCII 0x7f 11258f0484fSRodney W. Grimes 11358f0484fSRodney W. Grimes typedef char Char; 11458f0484fSRodney W. Grimes 11558f0484fSRodney W. Grimes #endif 11658f0484fSRodney W. Grimes 11758f0484fSRodney W. Grimes 11858f0484fSRodney W. Grimes #define CHAR(c) ((Char)((c)&M_ASCII)) 11958f0484fSRodney W. Grimes #define META(c) ((Char)((c)|M_QUOTE)) 12058f0484fSRodney W. Grimes #define M_ALL META('*') 12158f0484fSRodney W. Grimes #define M_END META(']') 12258f0484fSRodney W. Grimes #define M_NOT META('!') 12358f0484fSRodney W. Grimes #define M_ONE META('?') 12458f0484fSRodney W. Grimes #define M_RNG META('-') 12558f0484fSRodney W. Grimes #define M_SET META('[') 12658f0484fSRodney W. Grimes #define ismeta(c) (((c)&M_QUOTE) != 0) 12758f0484fSRodney W. Grimes 12858f0484fSRodney W. Grimes 12958f0484fSRodney W. Grimes static int compare __P((const void *, const void *)); 13058f0484fSRodney W. Grimes static void g_Ctoc __P((const Char *, char *)); 13158f0484fSRodney W. Grimes static int g_lstat __P((Char *, struct stat *, glob_t *)); 13258f0484fSRodney W. Grimes static DIR *g_opendir __P((Char *, glob_t *)); 13358f0484fSRodney W. Grimes static Char *g_strchr __P((Char *, int)); 13458f0484fSRodney W. Grimes #ifdef notdef 13558f0484fSRodney W. Grimes static Char *g_strcat __P((Char *, const Char *)); 13658f0484fSRodney W. Grimes #endif 13758f0484fSRodney W. Grimes static int g_stat __P((Char *, struct stat *, glob_t *)); 13858f0484fSRodney W. Grimes static int glob0 __P((const Char *, glob_t *)); 13958f0484fSRodney W. Grimes static int glob1 __P((Char *, glob_t *)); 14058f0484fSRodney W. Grimes static int glob2 __P((Char *, Char *, Char *, glob_t *)); 14158f0484fSRodney W. Grimes static int glob3 __P((Char *, Char *, Char *, Char *, glob_t *)); 14258f0484fSRodney W. Grimes static int globextend __P((const Char *, glob_t *)); 14358f0484fSRodney W. Grimes static const Char * globtilde __P((const Char *, Char *, glob_t *)); 14458f0484fSRodney W. Grimes static int globexp1 __P((const Char *, glob_t *)); 14558f0484fSRodney W. Grimes static int globexp2 __P((const Char *, const Char *, glob_t *, int *)); 14658f0484fSRodney W. Grimes static int match __P((Char *, Char *, Char *)); 14758f0484fSRodney W. Grimes #ifdef DEBUG 14858f0484fSRodney W. Grimes static void qprintf __P((const char *, Char *)); 14958f0484fSRodney W. Grimes #endif 15058f0484fSRodney W. Grimes 15158f0484fSRodney W. Grimes int 15258f0484fSRodney W. Grimes glob(pattern, flags, errfunc, pglob) 15358f0484fSRodney W. Grimes const char *pattern; 15458f0484fSRodney W. Grimes int flags, (*errfunc) __P((const char *, int)); 15558f0484fSRodney W. Grimes glob_t *pglob; 15658f0484fSRodney W. Grimes { 15758f0484fSRodney W. Grimes const u_char *patnext; 15858f0484fSRodney W. Grimes int c; 15958f0484fSRodney W. Grimes Char *bufnext, *bufend, patbuf[MAXPATHLEN+1]; 16058f0484fSRodney W. Grimes 16158f0484fSRodney W. Grimes patnext = (u_char *) pattern; 16258f0484fSRodney W. Grimes if (!(flags & GLOB_APPEND)) { 16358f0484fSRodney W. Grimes pglob->gl_pathc = 0; 16458f0484fSRodney W. Grimes pglob->gl_pathv = NULL; 16558f0484fSRodney W. Grimes if (!(flags & GLOB_DOOFFS)) 16658f0484fSRodney W. Grimes pglob->gl_offs = 0; 16758f0484fSRodney W. Grimes } 16858f0484fSRodney W. Grimes pglob->gl_flags = flags & ~GLOB_MAGCHAR; 16958f0484fSRodney W. Grimes pglob->gl_errfunc = errfunc; 17058f0484fSRodney W. Grimes pglob->gl_matchc = 0; 17158f0484fSRodney W. Grimes 17258f0484fSRodney W. Grimes bufnext = patbuf; 17358f0484fSRodney W. Grimes bufend = bufnext + MAXPATHLEN; 17458f0484fSRodney W. Grimes if (flags & GLOB_QUOTE) { 17558f0484fSRodney W. Grimes /* Protect the quoted characters. */ 17658f0484fSRodney W. Grimes while (bufnext < bufend && (c = *patnext++) != EOS) 17758f0484fSRodney W. Grimes if (c == QUOTE) { 17858f0484fSRodney W. Grimes if ((c = *patnext++) == EOS) { 17958f0484fSRodney W. Grimes c = QUOTE; 18058f0484fSRodney W. Grimes --patnext; 18158f0484fSRodney W. Grimes } 18258f0484fSRodney W. Grimes *bufnext++ = c | M_PROTECT; 18358f0484fSRodney W. Grimes } 18458f0484fSRodney W. Grimes else 18558f0484fSRodney W. Grimes *bufnext++ = c; 18658f0484fSRodney W. Grimes } 18758f0484fSRodney W. Grimes else 18858f0484fSRodney W. Grimes while (bufnext < bufend && (c = *patnext++) != EOS) 18958f0484fSRodney W. Grimes *bufnext++ = c; 19058f0484fSRodney W. Grimes *bufnext = EOS; 19158f0484fSRodney W. Grimes 19258f0484fSRodney W. Grimes if (flags & GLOB_BRACE) 19358f0484fSRodney W. Grimes return globexp1(patbuf, pglob); 19458f0484fSRodney W. Grimes else 19558f0484fSRodney W. Grimes return glob0(patbuf, pglob); 19658f0484fSRodney W. Grimes } 19758f0484fSRodney W. Grimes 19858f0484fSRodney W. Grimes /* 19958f0484fSRodney W. Grimes * Expand recursively a glob {} pattern. When there is no more expansion 20058f0484fSRodney W. Grimes * invoke the standard globbing routine to glob the rest of the magic 20158f0484fSRodney W. Grimes * characters 20258f0484fSRodney W. Grimes */ 20358f0484fSRodney W. Grimes static int globexp1(pattern, pglob) 20458f0484fSRodney W. Grimes const Char *pattern; 20558f0484fSRodney W. Grimes glob_t *pglob; 20658f0484fSRodney W. Grimes { 20758f0484fSRodney W. Grimes const Char* ptr = pattern; 20858f0484fSRodney W. Grimes int rv; 20958f0484fSRodney W. Grimes 21058f0484fSRodney W. Grimes /* Protect a single {}, for find(1), like csh */ 21158f0484fSRodney W. Grimes if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS) 21258f0484fSRodney W. Grimes return glob0(pattern, pglob); 21358f0484fSRodney W. Grimes 21458f0484fSRodney W. Grimes while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL) 21558f0484fSRodney W. Grimes if (!globexp2(ptr, pattern, pglob, &rv)) 21658f0484fSRodney W. Grimes return rv; 21758f0484fSRodney W. Grimes 21858f0484fSRodney W. Grimes return glob0(pattern, pglob); 21958f0484fSRodney W. Grimes } 22058f0484fSRodney W. Grimes 22158f0484fSRodney W. Grimes 22258f0484fSRodney W. Grimes /* 22358f0484fSRodney W. Grimes * Recursive brace globbing helper. Tries to expand a single brace. 22458f0484fSRodney W. Grimes * If it succeeds then it invokes globexp1 with the new pattern. 22558f0484fSRodney W. Grimes * If it fails then it tries to glob the rest of the pattern and returns. 22658f0484fSRodney W. Grimes */ 22758f0484fSRodney W. Grimes static int globexp2(ptr, pattern, pglob, rv) 22858f0484fSRodney W. Grimes const Char *ptr, *pattern; 22958f0484fSRodney W. Grimes glob_t *pglob; 23058f0484fSRodney W. Grimes int *rv; 23158f0484fSRodney W. Grimes { 23258f0484fSRodney W. Grimes int i; 23358f0484fSRodney W. Grimes Char *lm, *ls; 23458f0484fSRodney W. Grimes const Char *pe, *pm, *pl; 23558f0484fSRodney W. Grimes Char patbuf[MAXPATHLEN + 1]; 23658f0484fSRodney W. Grimes 23758f0484fSRodney W. Grimes /* copy part up to the brace */ 23858f0484fSRodney W. Grimes for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++) 23958f0484fSRodney W. Grimes continue; 24058f0484fSRodney W. Grimes ls = lm; 24158f0484fSRodney W. Grimes 24258f0484fSRodney W. Grimes /* Find the balanced brace */ 24358f0484fSRodney W. Grimes for (i = 0, pe = ++ptr; *pe; pe++) 24458f0484fSRodney W. Grimes if (*pe == LBRACKET) { 24558f0484fSRodney W. Grimes /* Ignore everything between [] */ 24658f0484fSRodney W. Grimes for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++) 24758f0484fSRodney W. Grimes continue; 24858f0484fSRodney W. Grimes if (*pe == EOS) { 24958f0484fSRodney W. Grimes /* 25058f0484fSRodney W. Grimes * We could not find a matching RBRACKET. 25158f0484fSRodney W. Grimes * Ignore and just look for RBRACE 25258f0484fSRodney W. Grimes */ 25358f0484fSRodney W. Grimes pe = pm; 25458f0484fSRodney W. Grimes } 25558f0484fSRodney W. Grimes } 25658f0484fSRodney W. Grimes else if (*pe == LBRACE) 25758f0484fSRodney W. Grimes i++; 25858f0484fSRodney W. Grimes else if (*pe == RBRACE) { 25958f0484fSRodney W. Grimes if (i == 0) 26058f0484fSRodney W. Grimes break; 26158f0484fSRodney W. Grimes i--; 26258f0484fSRodney W. Grimes } 26358f0484fSRodney W. Grimes 26458f0484fSRodney W. Grimes /* Non matching braces; just glob the pattern */ 26558f0484fSRodney W. Grimes if (i != 0 || *pe == EOS) { 26658f0484fSRodney W. Grimes *rv = glob0(patbuf, pglob); 26758f0484fSRodney W. Grimes return 0; 26858f0484fSRodney W. Grimes } 26958f0484fSRodney W. Grimes 27058f0484fSRodney W. Grimes for (i = 0, pl = pm = ptr; pm <= pe; pm++) 27158f0484fSRodney W. Grimes switch (*pm) { 27258f0484fSRodney W. Grimes case LBRACKET: 27358f0484fSRodney W. Grimes /* Ignore everything between [] */ 27458f0484fSRodney W. Grimes for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++) 27558f0484fSRodney W. Grimes continue; 27658f0484fSRodney W. Grimes if (*pm == EOS) { 27758f0484fSRodney W. Grimes /* 27858f0484fSRodney W. Grimes * We could not find a matching RBRACKET. 27958f0484fSRodney W. Grimes * Ignore and just look for RBRACE 28058f0484fSRodney W. Grimes */ 28158f0484fSRodney W. Grimes pm = pl; 28258f0484fSRodney W. Grimes } 28358f0484fSRodney W. Grimes break; 28458f0484fSRodney W. Grimes 28558f0484fSRodney W. Grimes case LBRACE: 28658f0484fSRodney W. Grimes i++; 28758f0484fSRodney W. Grimes break; 28858f0484fSRodney W. Grimes 28958f0484fSRodney W. Grimes case RBRACE: 29058f0484fSRodney W. Grimes if (i) { 29158f0484fSRodney W. Grimes i--; 29258f0484fSRodney W. Grimes break; 29358f0484fSRodney W. Grimes } 29458f0484fSRodney W. Grimes /* FALLTHROUGH */ 29558f0484fSRodney W. Grimes case COMMA: 29658f0484fSRodney W. Grimes if (i && *pm == COMMA) 29758f0484fSRodney W. Grimes break; 29858f0484fSRodney W. Grimes else { 29958f0484fSRodney W. Grimes /* Append the current string */ 30058f0484fSRodney W. Grimes for (lm = ls; (pl < pm); *lm++ = *pl++) 30158f0484fSRodney W. Grimes continue; 30258f0484fSRodney W. Grimes /* 30358f0484fSRodney W. Grimes * Append the rest of the pattern after the 30458f0484fSRodney W. Grimes * closing brace 30558f0484fSRodney W. Grimes */ 30658f0484fSRodney W. Grimes for (pl = pe + 1; (*lm++ = *pl++) != EOS;) 30758f0484fSRodney W. Grimes continue; 30858f0484fSRodney W. Grimes 30958f0484fSRodney W. Grimes /* Expand the current pattern */ 31058f0484fSRodney W. Grimes #ifdef DEBUG 31158f0484fSRodney W. Grimes qprintf("globexp2:", patbuf); 31258f0484fSRodney W. Grimes #endif 31358f0484fSRodney W. Grimes *rv = globexp1(patbuf, pglob); 31458f0484fSRodney W. Grimes 31558f0484fSRodney W. Grimes /* move after the comma, to the next string */ 31658f0484fSRodney W. Grimes pl = pm + 1; 31758f0484fSRodney W. Grimes } 31858f0484fSRodney W. Grimes break; 31958f0484fSRodney W. Grimes 32058f0484fSRodney W. Grimes default: 32158f0484fSRodney W. Grimes break; 32258f0484fSRodney W. Grimes } 32358f0484fSRodney W. Grimes *rv = 0; 32458f0484fSRodney W. Grimes return 0; 32558f0484fSRodney W. Grimes } 32658f0484fSRodney W. Grimes 32758f0484fSRodney W. Grimes 32858f0484fSRodney W. Grimes 32958f0484fSRodney W. Grimes /* 33058f0484fSRodney W. Grimes * expand tilde from the passwd file. 33158f0484fSRodney W. Grimes */ 33258f0484fSRodney W. Grimes static const Char * 33358f0484fSRodney W. Grimes globtilde(pattern, patbuf, pglob) 33458f0484fSRodney W. Grimes const Char *pattern; 33558f0484fSRodney W. Grimes Char *patbuf; 33658f0484fSRodney W. Grimes glob_t *pglob; 33758f0484fSRodney W. Grimes { 33858f0484fSRodney W. Grimes struct passwd *pwd; 33958f0484fSRodney W. Grimes char *h; 34058f0484fSRodney W. Grimes const Char *p; 34158f0484fSRodney W. Grimes Char *b; 34258f0484fSRodney W. Grimes 34358f0484fSRodney W. Grimes if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE)) 34458f0484fSRodney W. Grimes return pattern; 34558f0484fSRodney W. Grimes 34658f0484fSRodney W. Grimes /* Copy up to the end of the string or / */ 34758f0484fSRodney W. Grimes for (p = pattern + 1, h = (char *) patbuf; *p && *p != SLASH; 34858f0484fSRodney W. Grimes *h++ = *p++) 34958f0484fSRodney W. Grimes continue; 35058f0484fSRodney W. Grimes 35158f0484fSRodney W. Grimes *h = EOS; 35258f0484fSRodney W. Grimes 35358f0484fSRodney W. Grimes if (((char *) patbuf)[0] == EOS) { 35458f0484fSRodney W. Grimes /* 35558f0484fSRodney W. Grimes * handle a plain ~ or ~/ by expanding $HOME 35658f0484fSRodney W. Grimes * first and then trying the password file 35758f0484fSRodney W. Grimes */ 35858f0484fSRodney W. Grimes if ((h = getenv("HOME")) == NULL) { 35958f0484fSRodney W. Grimes if ((pwd = getpwuid(getuid())) == NULL) 36058f0484fSRodney W. Grimes return pattern; 36158f0484fSRodney W. Grimes else 36258f0484fSRodney W. Grimes h = pwd->pw_dir; 36358f0484fSRodney W. Grimes } 36458f0484fSRodney W. Grimes } 36558f0484fSRodney W. Grimes else { 36658f0484fSRodney W. Grimes /* 36758f0484fSRodney W. Grimes * Expand a ~user 36858f0484fSRodney W. Grimes */ 36958f0484fSRodney W. Grimes if ((pwd = getpwnam((char*) patbuf)) == NULL) 37058f0484fSRodney W. Grimes return pattern; 37158f0484fSRodney W. Grimes else 37258f0484fSRodney W. Grimes h = pwd->pw_dir; 37358f0484fSRodney W. Grimes } 37458f0484fSRodney W. Grimes 37558f0484fSRodney W. Grimes /* Copy the home directory */ 37658f0484fSRodney W. Grimes for (b = patbuf; *h; *b++ = *h++) 37758f0484fSRodney W. Grimes continue; 37858f0484fSRodney W. Grimes 37958f0484fSRodney W. Grimes /* Append the rest of the pattern */ 38058f0484fSRodney W. Grimes while ((*b++ = *p++) != EOS) 38158f0484fSRodney W. Grimes continue; 38258f0484fSRodney W. Grimes 38358f0484fSRodney W. Grimes return patbuf; 38458f0484fSRodney W. Grimes } 38558f0484fSRodney W. Grimes 38658f0484fSRodney W. Grimes 38758f0484fSRodney W. Grimes /* 38858f0484fSRodney W. Grimes * The main glob() routine: compiles the pattern (optionally processing 38958f0484fSRodney W. Grimes * quotes), calls glob1() to do the real pattern matching, and finally 39058f0484fSRodney W. Grimes * sorts the list (unless unsorted operation is requested). Returns 0 39158f0484fSRodney W. Grimes * if things went well, nonzero if errors occurred. It is not an error 39258f0484fSRodney W. Grimes * to find no matches. 39358f0484fSRodney W. Grimes */ 39458f0484fSRodney W. Grimes static int 39558f0484fSRodney W. Grimes glob0(pattern, pglob) 39658f0484fSRodney W. Grimes const Char *pattern; 39758f0484fSRodney W. Grimes glob_t *pglob; 39858f0484fSRodney W. Grimes { 39958f0484fSRodney W. Grimes const Char *qpatnext; 40058f0484fSRodney W. Grimes int c, err, oldpathc; 40158f0484fSRodney W. Grimes Char *bufnext, patbuf[MAXPATHLEN+1]; 40258f0484fSRodney W. Grimes 40358f0484fSRodney W. Grimes qpatnext = globtilde(pattern, patbuf, pglob); 40458f0484fSRodney W. Grimes oldpathc = pglob->gl_pathc; 40558f0484fSRodney W. Grimes bufnext = patbuf; 40658f0484fSRodney W. Grimes 40758f0484fSRodney W. Grimes /* We don't need to check for buffer overflow any more. */ 40858f0484fSRodney W. Grimes while ((c = *qpatnext++) != EOS) { 40958f0484fSRodney W. Grimes switch (c) { 41058f0484fSRodney W. Grimes case LBRACKET: 41158f0484fSRodney W. Grimes c = *qpatnext; 41258f0484fSRodney W. Grimes if (c == NOT) 41358f0484fSRodney W. Grimes ++qpatnext; 41458f0484fSRodney W. Grimes if (*qpatnext == EOS || 41558f0484fSRodney W. Grimes g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) { 41658f0484fSRodney W. Grimes *bufnext++ = LBRACKET; 41758f0484fSRodney W. Grimes if (c == NOT) 41858f0484fSRodney W. Grimes --qpatnext; 41958f0484fSRodney W. Grimes break; 42058f0484fSRodney W. Grimes } 42158f0484fSRodney W. Grimes *bufnext++ = M_SET; 42258f0484fSRodney W. Grimes if (c == NOT) 42358f0484fSRodney W. Grimes *bufnext++ = M_NOT; 42458f0484fSRodney W. Grimes c = *qpatnext++; 42558f0484fSRodney W. Grimes do { 42658f0484fSRodney W. Grimes *bufnext++ = CHAR(c); 42758f0484fSRodney W. Grimes if (*qpatnext == RANGE && 42858f0484fSRodney W. Grimes (c = qpatnext[1]) != RBRACKET) { 42958f0484fSRodney W. Grimes *bufnext++ = M_RNG; 43058f0484fSRodney W. Grimes *bufnext++ = CHAR(c); 43158f0484fSRodney W. Grimes qpatnext += 2; 43258f0484fSRodney W. Grimes } 43358f0484fSRodney W. Grimes } while ((c = *qpatnext++) != RBRACKET); 43458f0484fSRodney W. Grimes pglob->gl_flags |= GLOB_MAGCHAR; 43558f0484fSRodney W. Grimes *bufnext++ = M_END; 43658f0484fSRodney W. Grimes break; 43758f0484fSRodney W. Grimes case QUESTION: 43858f0484fSRodney W. Grimes pglob->gl_flags |= GLOB_MAGCHAR; 43958f0484fSRodney W. Grimes *bufnext++ = M_ONE; 44058f0484fSRodney W. Grimes break; 44158f0484fSRodney W. Grimes case STAR: 44258f0484fSRodney W. Grimes pglob->gl_flags |= GLOB_MAGCHAR; 44358f0484fSRodney W. Grimes /* collapse adjacent stars to one, 44458f0484fSRodney W. Grimes * to avoid exponential behavior 44558f0484fSRodney W. Grimes */ 44658f0484fSRodney W. Grimes if (bufnext == patbuf || bufnext[-1] != M_ALL) 44758f0484fSRodney W. Grimes *bufnext++ = M_ALL; 44858f0484fSRodney W. Grimes break; 44958f0484fSRodney W. Grimes default: 45058f0484fSRodney W. Grimes *bufnext++ = CHAR(c); 45158f0484fSRodney W. Grimes break; 45258f0484fSRodney W. Grimes } 45358f0484fSRodney W. Grimes } 45458f0484fSRodney W. Grimes *bufnext = EOS; 45558f0484fSRodney W. Grimes #ifdef DEBUG 45658f0484fSRodney W. Grimes qprintf("glob0:", patbuf); 45758f0484fSRodney W. Grimes #endif 45858f0484fSRodney W. Grimes 45958f0484fSRodney W. Grimes if ((err = glob1(patbuf, pglob)) != 0) 46058f0484fSRodney W. Grimes return(err); 46158f0484fSRodney W. Grimes 46258f0484fSRodney W. Grimes /* 46358f0484fSRodney W. Grimes * If there was no match we are going to append the pattern 46458f0484fSRodney W. Grimes * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified 46558f0484fSRodney W. Grimes * and the pattern did not contain any magic characters 46658f0484fSRodney W. Grimes * GLOB_NOMAGIC is there just for compatibility with csh. 46758f0484fSRodney W. Grimes */ 46858f0484fSRodney W. Grimes if (pglob->gl_pathc == oldpathc && 46958f0484fSRodney W. Grimes ((pglob->gl_flags & GLOB_NOCHECK) || 47058f0484fSRodney W. Grimes ((pglob->gl_flags & GLOB_NOMAGIC) && 47158f0484fSRodney W. Grimes !(pglob->gl_flags & GLOB_MAGCHAR)))) 47258f0484fSRodney W. Grimes return(globextend(pattern, pglob)); 47358f0484fSRodney W. Grimes else if (!(pglob->gl_flags & GLOB_NOSORT)) 47458f0484fSRodney W. Grimes qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, 47558f0484fSRodney W. Grimes pglob->gl_pathc - oldpathc, sizeof(char *), compare); 47658f0484fSRodney W. Grimes return(0); 47758f0484fSRodney W. Grimes } 47858f0484fSRodney W. Grimes 47958f0484fSRodney W. Grimes static int 48058f0484fSRodney W. Grimes compare(p, q) 48158f0484fSRodney W. Grimes const void *p, *q; 48258f0484fSRodney W. Grimes { 48358f0484fSRodney W. Grimes return(strcmp(*(char **)p, *(char **)q)); 48458f0484fSRodney W. Grimes } 48558f0484fSRodney W. Grimes 48658f0484fSRodney W. Grimes static int 48758f0484fSRodney W. Grimes glob1(pattern, pglob) 48858f0484fSRodney W. Grimes Char *pattern; 48958f0484fSRodney W. Grimes glob_t *pglob; 49058f0484fSRodney W. Grimes { 49158f0484fSRodney W. Grimes Char pathbuf[MAXPATHLEN+1]; 49258f0484fSRodney W. Grimes 49358f0484fSRodney W. Grimes /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ 49458f0484fSRodney W. Grimes if (*pattern == EOS) 49558f0484fSRodney W. Grimes return(0); 49658f0484fSRodney W. Grimes return(glob2(pathbuf, pathbuf, pattern, pglob)); 49758f0484fSRodney W. Grimes } 49858f0484fSRodney W. Grimes 49958f0484fSRodney W. Grimes /* 50058f0484fSRodney W. Grimes * The functions glob2 and glob3 are mutually recursive; there is one level 50158f0484fSRodney W. Grimes * of recursion for each segment in the pattern that contains one or more 50258f0484fSRodney W. Grimes * meta characters. 50358f0484fSRodney W. Grimes */ 50458f0484fSRodney W. Grimes static int 50558f0484fSRodney W. Grimes glob2(pathbuf, pathend, pattern, pglob) 50658f0484fSRodney W. Grimes Char *pathbuf, *pathend, *pattern; 50758f0484fSRodney W. Grimes glob_t *pglob; 50858f0484fSRodney W. Grimes { 50958f0484fSRodney W. Grimes struct stat sb; 51058f0484fSRodney W. Grimes Char *p, *q; 51158f0484fSRodney W. Grimes int anymeta; 51258f0484fSRodney W. Grimes 51358f0484fSRodney W. Grimes /* 51458f0484fSRodney W. Grimes * Loop over pattern segments until end of pattern or until 51558f0484fSRodney W. Grimes * segment with meta character found. 51658f0484fSRodney W. Grimes */ 51758f0484fSRodney W. Grimes for (anymeta = 0;;) { 51858f0484fSRodney W. Grimes if (*pattern == EOS) { /* End of pattern? */ 51958f0484fSRodney W. Grimes *pathend = EOS; 52058f0484fSRodney W. Grimes if (g_lstat(pathbuf, &sb, pglob)) 52158f0484fSRodney W. Grimes return(0); 52258f0484fSRodney W. Grimes 52358f0484fSRodney W. Grimes if (((pglob->gl_flags & GLOB_MARK) && 52458f0484fSRodney W. Grimes pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) 52558f0484fSRodney W. Grimes || (S_ISLNK(sb.st_mode) && 52658f0484fSRodney W. Grimes (g_stat(pathbuf, &sb, pglob) == 0) && 52758f0484fSRodney W. Grimes S_ISDIR(sb.st_mode)))) { 52858f0484fSRodney W. Grimes *pathend++ = SEP; 52958f0484fSRodney W. Grimes *pathend = EOS; 53058f0484fSRodney W. Grimes } 53158f0484fSRodney W. Grimes ++pglob->gl_matchc; 53258f0484fSRodney W. Grimes return(globextend(pathbuf, pglob)); 53358f0484fSRodney W. Grimes } 53458f0484fSRodney W. Grimes 53558f0484fSRodney W. Grimes /* Find end of next segment, copy tentatively to pathend. */ 53658f0484fSRodney W. Grimes q = pathend; 53758f0484fSRodney W. Grimes p = pattern; 53858f0484fSRodney W. Grimes while (*p != EOS && *p != SEP) { 53958f0484fSRodney W. Grimes if (ismeta(*p)) 54058f0484fSRodney W. Grimes anymeta = 1; 54158f0484fSRodney W. Grimes *q++ = *p++; 54258f0484fSRodney W. Grimes } 54358f0484fSRodney W. Grimes 54458f0484fSRodney W. Grimes if (!anymeta) { /* No expansion, do next segment. */ 54558f0484fSRodney W. Grimes pathend = q; 54658f0484fSRodney W. Grimes pattern = p; 54758f0484fSRodney W. Grimes while (*pattern == SEP) 54858f0484fSRodney W. Grimes *pathend++ = *pattern++; 54958f0484fSRodney W. Grimes } else /* Need expansion, recurse. */ 55058f0484fSRodney W. Grimes return(glob3(pathbuf, pathend, pattern, p, pglob)); 55158f0484fSRodney W. Grimes } 55258f0484fSRodney W. Grimes /* NOTREACHED */ 55358f0484fSRodney W. Grimes } 55458f0484fSRodney W. Grimes 55558f0484fSRodney W. Grimes static int 55658f0484fSRodney W. Grimes glob3(pathbuf, pathend, pattern, restpattern, pglob) 55758f0484fSRodney W. Grimes Char *pathbuf, *pathend, *pattern, *restpattern; 55858f0484fSRodney W. Grimes glob_t *pglob; 55958f0484fSRodney W. Grimes { 56058f0484fSRodney W. Grimes register struct dirent *dp; 56158f0484fSRodney W. Grimes DIR *dirp; 56258f0484fSRodney W. Grimes int err; 56358f0484fSRodney W. Grimes char buf[MAXPATHLEN]; 56458f0484fSRodney W. Grimes 56558f0484fSRodney W. Grimes /* 56658f0484fSRodney W. Grimes * The readdirfunc declaration can't be prototyped, because it is 56758f0484fSRodney W. Grimes * assigned, below, to two functions which are prototyped in glob.h 56858f0484fSRodney W. Grimes * and dirent.h as taking pointers to differently typed opaque 56958f0484fSRodney W. Grimes * structures. 57058f0484fSRodney W. Grimes */ 57158f0484fSRodney W. Grimes struct dirent *(*readdirfunc)(); 57258f0484fSRodney W. Grimes 57358f0484fSRodney W. Grimes *pathend = EOS; 57458f0484fSRodney W. Grimes errno = 0; 57558f0484fSRodney W. Grimes 57658f0484fSRodney W. Grimes if ((dirp = g_opendir(pathbuf, pglob)) == NULL) { 57758f0484fSRodney W. Grimes /* TODO: don't call for ENOENT or ENOTDIR? */ 57858f0484fSRodney W. Grimes if (pglob->gl_errfunc) { 57958f0484fSRodney W. Grimes g_Ctoc(pathbuf, buf); 58058f0484fSRodney W. Grimes if (pglob->gl_errfunc(buf, errno) || 58158f0484fSRodney W. Grimes pglob->gl_flags & GLOB_ERR) 58258f0484fSRodney W. Grimes return (GLOB_ABEND); 58358f0484fSRodney W. Grimes } 58458f0484fSRodney W. Grimes return(0); 58558f0484fSRodney W. Grimes } 58658f0484fSRodney W. Grimes 58758f0484fSRodney W. Grimes err = 0; 58858f0484fSRodney W. Grimes 58958f0484fSRodney W. Grimes /* Search directory for matching names. */ 59058f0484fSRodney W. Grimes if (pglob->gl_flags & GLOB_ALTDIRFUNC) 59158f0484fSRodney W. Grimes readdirfunc = pglob->gl_readdir; 59258f0484fSRodney W. Grimes else 59358f0484fSRodney W. Grimes readdirfunc = readdir; 59458f0484fSRodney W. Grimes while ((dp = (*readdirfunc)(dirp))) { 59558f0484fSRodney W. Grimes register u_char *sc; 59658f0484fSRodney W. Grimes register Char *dc; 59758f0484fSRodney W. Grimes 59858f0484fSRodney W. Grimes /* Initial DOT must be matched literally. */ 59958f0484fSRodney W. Grimes if (dp->d_name[0] == DOT && *pattern != DOT) 60058f0484fSRodney W. Grimes continue; 60158f0484fSRodney W. Grimes for (sc = (u_char *) dp->d_name, dc = pathend; 60258f0484fSRodney W. Grimes (*dc++ = *sc++) != EOS;) 60358f0484fSRodney W. Grimes continue; 60458f0484fSRodney W. Grimes if (!match(pathend, pattern, restpattern)) { 60558f0484fSRodney W. Grimes *pathend = EOS; 60658f0484fSRodney W. Grimes continue; 60758f0484fSRodney W. Grimes } 60858f0484fSRodney W. Grimes err = glob2(pathbuf, --dc, restpattern, pglob); 60958f0484fSRodney W. Grimes if (err) 61058f0484fSRodney W. Grimes break; 61158f0484fSRodney W. Grimes } 61258f0484fSRodney W. Grimes 61358f0484fSRodney W. Grimes if (pglob->gl_flags & GLOB_ALTDIRFUNC) 61458f0484fSRodney W. Grimes (*pglob->gl_closedir)(dirp); 61558f0484fSRodney W. Grimes else 61658f0484fSRodney W. Grimes closedir(dirp); 61758f0484fSRodney W. Grimes return(err); 61858f0484fSRodney W. Grimes } 61958f0484fSRodney W. Grimes 62058f0484fSRodney W. Grimes 62158f0484fSRodney W. Grimes /* 62258f0484fSRodney W. Grimes * Extend the gl_pathv member of a glob_t structure to accomodate a new item, 62358f0484fSRodney W. Grimes * add the new item, and update gl_pathc. 62458f0484fSRodney W. Grimes * 62558f0484fSRodney W. Grimes * This assumes the BSD realloc, which only copies the block when its size 62658f0484fSRodney W. Grimes * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic 62758f0484fSRodney W. Grimes * behavior. 62858f0484fSRodney W. Grimes * 62958f0484fSRodney W. Grimes * Return 0 if new item added, error code if memory couldn't be allocated. 63058f0484fSRodney W. Grimes * 63158f0484fSRodney W. Grimes * Invariant of the glob_t structure: 63258f0484fSRodney W. Grimes * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and 63358f0484fSRodney W. Grimes * gl_pathv points to (gl_offs + gl_pathc + 1) items. 63458f0484fSRodney W. Grimes */ 63558f0484fSRodney W. Grimes static int 63658f0484fSRodney W. Grimes globextend(path, pglob) 63758f0484fSRodney W. Grimes const Char *path; 63858f0484fSRodney W. Grimes glob_t *pglob; 63958f0484fSRodney W. Grimes { 64058f0484fSRodney W. Grimes register char **pathv; 64158f0484fSRodney W. Grimes register int i; 64258f0484fSRodney W. Grimes u_int newsize; 64358f0484fSRodney W. Grimes char *copy; 64458f0484fSRodney W. Grimes const Char *p; 64558f0484fSRodney W. Grimes 64658f0484fSRodney W. Grimes newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs); 64758f0484fSRodney W. Grimes pathv = pglob->gl_pathv ? 64858f0484fSRodney W. Grimes realloc((char *)pglob->gl_pathv, newsize) : 64958f0484fSRodney W. Grimes malloc(newsize); 65058f0484fSRodney W. Grimes if (pathv == NULL) 65158f0484fSRodney W. Grimes return(GLOB_NOSPACE); 65258f0484fSRodney W. Grimes 65358f0484fSRodney W. Grimes if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { 65458f0484fSRodney W. Grimes /* first time around -- clear initial gl_offs items */ 65558f0484fSRodney W. Grimes pathv += pglob->gl_offs; 65658f0484fSRodney W. Grimes for (i = pglob->gl_offs; --i >= 0; ) 65758f0484fSRodney W. Grimes *--pathv = NULL; 65858f0484fSRodney W. Grimes } 65958f0484fSRodney W. Grimes pglob->gl_pathv = pathv; 66058f0484fSRodney W. Grimes 66158f0484fSRodney W. Grimes for (p = path; *p++;) 66258f0484fSRodney W. Grimes continue; 66358f0484fSRodney W. Grimes if ((copy = malloc(p - path)) != NULL) { 66458f0484fSRodney W. Grimes g_Ctoc(path, copy); 66558f0484fSRodney W. Grimes pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; 66658f0484fSRodney W. Grimes } 66758f0484fSRodney W. Grimes pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; 66858f0484fSRodney W. Grimes return(copy == NULL ? GLOB_NOSPACE : 0); 66958f0484fSRodney W. Grimes } 67058f0484fSRodney W. Grimes 67158f0484fSRodney W. Grimes 67258f0484fSRodney W. Grimes /* 67358f0484fSRodney W. Grimes * pattern matching function for filenames. Each occurrence of the * 67458f0484fSRodney W. Grimes * pattern causes a recursion level. 67558f0484fSRodney W. Grimes */ 67658f0484fSRodney W. Grimes static int 67758f0484fSRodney W. Grimes match(name, pat, patend) 67858f0484fSRodney W. Grimes register Char *name, *pat, *patend; 67958f0484fSRodney W. Grimes { 68058f0484fSRodney W. Grimes int ok, negate_range; 68158f0484fSRodney W. Grimes Char c, k; 68258f0484fSRodney W. Grimes 68358f0484fSRodney W. Grimes while (pat < patend) { 68458f0484fSRodney W. Grimes c = *pat++; 68558f0484fSRodney W. Grimes switch (c & M_MASK) { 68658f0484fSRodney W. Grimes case M_ALL: 68758f0484fSRodney W. Grimes if (pat == patend) 68858f0484fSRodney W. Grimes return(1); 68958f0484fSRodney W. Grimes do 69058f0484fSRodney W. Grimes if (match(name, pat, patend)) 69158f0484fSRodney W. Grimes return(1); 69258f0484fSRodney W. Grimes while (*name++ != EOS); 69358f0484fSRodney W. Grimes return(0); 69458f0484fSRodney W. Grimes case M_ONE: 69558f0484fSRodney W. Grimes if (*name++ == EOS) 69658f0484fSRodney W. Grimes return(0); 69758f0484fSRodney W. Grimes break; 69858f0484fSRodney W. Grimes case M_SET: 69958f0484fSRodney W. Grimes ok = 0; 70058f0484fSRodney W. Grimes if ((k = *name++) == EOS) 70158f0484fSRodney W. Grimes return(0); 70258f0484fSRodney W. Grimes if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS) 70358f0484fSRodney W. Grimes ++pat; 70458f0484fSRodney W. Grimes while (((c = *pat++) & M_MASK) != M_END) 70558f0484fSRodney W. Grimes if ((*pat & M_MASK) == M_RNG) { 70658f0484fSRodney W. Grimes if (c <= k && k <= pat[1]) 70758f0484fSRodney W. Grimes ok = 1; 70858f0484fSRodney W. Grimes pat += 2; 70958f0484fSRodney W. Grimes } else if (c == k) 71058f0484fSRodney W. Grimes ok = 1; 71158f0484fSRodney W. Grimes if (ok == negate_range) 71258f0484fSRodney W. Grimes return(0); 71358f0484fSRodney W. Grimes break; 71458f0484fSRodney W. Grimes default: 71558f0484fSRodney W. Grimes if (*name++ != c) 71658f0484fSRodney W. Grimes return(0); 71758f0484fSRodney W. Grimes break; 71858f0484fSRodney W. Grimes } 71958f0484fSRodney W. Grimes } 72058f0484fSRodney W. Grimes return(*name == EOS); 72158f0484fSRodney W. Grimes } 72258f0484fSRodney W. Grimes 72358f0484fSRodney W. Grimes /* Free allocated data belonging to a glob_t structure. */ 72458f0484fSRodney W. Grimes void 72558f0484fSRodney W. Grimes globfree(pglob) 72658f0484fSRodney W. Grimes glob_t *pglob; 72758f0484fSRodney W. Grimes { 72858f0484fSRodney W. Grimes register int i; 72958f0484fSRodney W. Grimes register char **pp; 73058f0484fSRodney W. Grimes 73158f0484fSRodney W. Grimes if (pglob->gl_pathv != NULL) { 73258f0484fSRodney W. Grimes pp = pglob->gl_pathv + pglob->gl_offs; 73358f0484fSRodney W. Grimes for (i = pglob->gl_pathc; i--; ++pp) 73458f0484fSRodney W. Grimes if (*pp) 73558f0484fSRodney W. Grimes free(*pp); 73658f0484fSRodney W. Grimes free(pglob->gl_pathv); 73758f0484fSRodney W. Grimes } 73858f0484fSRodney W. Grimes } 73958f0484fSRodney W. Grimes 74058f0484fSRodney W. Grimes static DIR * 74158f0484fSRodney W. Grimes g_opendir(str, pglob) 74258f0484fSRodney W. Grimes register Char *str; 74358f0484fSRodney W. Grimes glob_t *pglob; 74458f0484fSRodney W. Grimes { 74558f0484fSRodney W. Grimes char buf[MAXPATHLEN]; 74658f0484fSRodney W. Grimes 74758f0484fSRodney W. Grimes if (!*str) 74858f0484fSRodney W. Grimes strcpy(buf, "."); 74958f0484fSRodney W. Grimes else 75058f0484fSRodney W. Grimes g_Ctoc(str, buf); 75158f0484fSRodney W. Grimes 75258f0484fSRodney W. Grimes if (pglob->gl_flags & GLOB_ALTDIRFUNC) 75358f0484fSRodney W. Grimes return((*pglob->gl_opendir)(buf)); 75458f0484fSRodney W. Grimes 75558f0484fSRodney W. Grimes return(opendir(buf)); 75658f0484fSRodney W. Grimes } 75758f0484fSRodney W. Grimes 75858f0484fSRodney W. Grimes static int 75958f0484fSRodney W. Grimes g_lstat(fn, sb, pglob) 76058f0484fSRodney W. Grimes register Char *fn; 76158f0484fSRodney W. Grimes struct stat *sb; 76258f0484fSRodney W. Grimes glob_t *pglob; 76358f0484fSRodney W. Grimes { 76458f0484fSRodney W. Grimes char buf[MAXPATHLEN]; 76558f0484fSRodney W. Grimes 76658f0484fSRodney W. Grimes g_Ctoc(fn, buf); 76758f0484fSRodney W. Grimes if (pglob->gl_flags & GLOB_ALTDIRFUNC) 76858f0484fSRodney W. Grimes return((*pglob->gl_lstat)(buf, sb)); 76958f0484fSRodney W. Grimes return(lstat(buf, sb)); 77058f0484fSRodney W. Grimes } 77158f0484fSRodney W. Grimes 77258f0484fSRodney W. Grimes static int 77358f0484fSRodney W. Grimes g_stat(fn, sb, pglob) 77458f0484fSRodney W. Grimes register Char *fn; 77558f0484fSRodney W. Grimes struct stat *sb; 77658f0484fSRodney W. Grimes glob_t *pglob; 77758f0484fSRodney W. Grimes { 77858f0484fSRodney W. Grimes char buf[MAXPATHLEN]; 77958f0484fSRodney W. Grimes 78058f0484fSRodney W. Grimes g_Ctoc(fn, buf); 78158f0484fSRodney W. Grimes if (pglob->gl_flags & GLOB_ALTDIRFUNC) 78258f0484fSRodney W. Grimes return((*pglob->gl_stat)(buf, sb)); 78358f0484fSRodney W. Grimes return(stat(buf, sb)); 78458f0484fSRodney W. Grimes } 78558f0484fSRodney W. Grimes 78658f0484fSRodney W. Grimes static Char * 78758f0484fSRodney W. Grimes g_strchr(str, ch) 78858f0484fSRodney W. Grimes Char *str; 78958f0484fSRodney W. Grimes int ch; 79058f0484fSRodney W. Grimes { 79158f0484fSRodney W. Grimes do { 79258f0484fSRodney W. Grimes if (*str == ch) 79358f0484fSRodney W. Grimes return (str); 79458f0484fSRodney W. Grimes } while (*str++); 79558f0484fSRodney W. Grimes return (NULL); 79658f0484fSRodney W. Grimes } 79758f0484fSRodney W. Grimes 79858f0484fSRodney W. Grimes #ifdef notdef 79958f0484fSRodney W. Grimes static Char * 80058f0484fSRodney W. Grimes g_strcat(dst, src) 80158f0484fSRodney W. Grimes Char *dst; 80258f0484fSRodney W. Grimes const Char* src; 80358f0484fSRodney W. Grimes { 80458f0484fSRodney W. Grimes Char *sdst = dst; 80558f0484fSRodney W. Grimes 80658f0484fSRodney W. Grimes while (*dst++) 80758f0484fSRodney W. Grimes continue; 80858f0484fSRodney W. Grimes --dst; 80958f0484fSRodney W. Grimes while((*dst++ = *src++) != EOS) 81058f0484fSRodney W. Grimes continue; 81158f0484fSRodney W. Grimes 81258f0484fSRodney W. Grimes return (sdst); 81358f0484fSRodney W. Grimes } 81458f0484fSRodney W. Grimes #endif 81558f0484fSRodney W. Grimes 81658f0484fSRodney W. Grimes static void 81758f0484fSRodney W. Grimes g_Ctoc(str, buf) 81858f0484fSRodney W. Grimes register const Char *str; 81958f0484fSRodney W. Grimes char *buf; 82058f0484fSRodney W. Grimes { 82158f0484fSRodney W. Grimes register char *dc; 82258f0484fSRodney W. Grimes 82358f0484fSRodney W. Grimes for (dc = buf; (*dc++ = *str++) != EOS;) 82458f0484fSRodney W. Grimes continue; 82558f0484fSRodney W. Grimes } 82658f0484fSRodney W. Grimes 82758f0484fSRodney W. Grimes #ifdef DEBUG 82858f0484fSRodney W. Grimes static void 82958f0484fSRodney W. Grimes qprintf(str, s) 83058f0484fSRodney W. Grimes const char *str; 83158f0484fSRodney W. Grimes register Char *s; 83258f0484fSRodney W. Grimes { 83358f0484fSRodney W. Grimes register Char *p; 83458f0484fSRodney W. Grimes 83558f0484fSRodney W. Grimes (void)printf("%s:\n", str); 83658f0484fSRodney W. Grimes for (p = s; *p; p++) 83758f0484fSRodney W. Grimes (void)printf("%c", CHAR(*p)); 83858f0484fSRodney W. Grimes (void)printf("\n"); 83958f0484fSRodney W. Grimes for (p = s; *p; p++) 84058f0484fSRodney W. Grimes (void)printf("%c", *p & M_PROTECT ? '"' : ' '); 84158f0484fSRodney W. Grimes (void)printf("\n"); 84258f0484fSRodney W. Grimes for (p = s; *p; p++) 84358f0484fSRodney W. Grimes (void)printf("%c", ismeta(*p) ? '_' : ' '); 84458f0484fSRodney W. Grimes (void)printf("\n"); 84558f0484fSRodney W. Grimes } 84658f0484fSRodney W. Grimes #endif 847