| version 1.1, 1993/03/21 09:45:37 |
version 1.2, 1993/07/30 07:57:52 |
|
|
| */ |
*/ |
| |
|
| #if defined(LIBC_SCCS) && !defined(lint) |
#if defined(LIBC_SCCS) && !defined(lint) |
| static char sccsid[] = "@(#)glob.c 5.12 (Berkeley) 6/24/91"; |
/*static char sccsid[] = "from: @(#)glob.c 5.18 (Berkeley) 12/4/92";*/ |
| |
static char rcsid[] = "$Id$"; |
| #endif /* LIBC_SCCS and not lint */ |
#endif /* LIBC_SCCS and not lint */ |
| |
|
| /* |
/* |
| Line 50 static char sccsid[] = "@(#)glob.c 5.12 |
|
| Line 51 static char sccsid[] = "@(#)glob.c 5.12 |
|
| * character might have (except \ at end of string is retained). |
* character might have (except \ at end of string is retained). |
| * GLOB_MAGCHAR: |
* GLOB_MAGCHAR: |
| * Set in gl_flags if pattern contained a globbing character. |
* Set in gl_flags if pattern contained a globbing character. |
| |
* GLOB_NOMAGIC: |
| |
* Same as GLOB_NOCHECK, but it will only append pattern if it did |
| |
* not contain any magic characters. [Used in csh style globbing] |
| |
* GLOB_ALTDIRFUNC: |
| |
* Use alternately specified directory access functions. |
| * gl_matchc: |
* gl_matchc: |
| * Number of matches in the current invocation of glob. |
* Number of matches in the current invocation of glob. |
| */ |
*/ |
| |
|
| #include <sys/cdefs.h> |
|
| #include <sys/param.h> |
#include <sys/param.h> |
| #include <sys/stat.h> |
#include <sys/stat.h> |
| #include <dirent.h> |
#include <dirent.h> |
| Line 98 typedef u_short Char; |
|
| Line 103 typedef u_short Char; |
|
| |
|
| static int compare __P((const void *, const void *)); |
static int compare __P((const void *, const void *)); |
| static void g_Ctoc __P((Char *, char *)); |
static void g_Ctoc __P((Char *, char *)); |
| static int g_lstat __P((Char *, struct stat *)); |
static int g_lstat __P((Char *, struct stat *, glob_t *)); |
| static DIR *g_opendir __P((Char *)); |
static DIR *g_opendir __P((Char *, glob_t *)); |
| static Char *g_strchr __P((Char *, int)); |
static Char *g_strchr __P((Char *, int)); |
| static int g_stat __P((Char *, struct stat *)); |
static int g_stat __P((Char *, struct stat *, glob_t *)); |
| static int glob1 __P((Char *, glob_t *)); |
static int glob1 __P((Char *, glob_t *)); |
| static int glob2 __P((Char *, Char *, Char *, glob_t *)); |
static int glob2 __P((Char *, Char *, Char *, glob_t *)); |
| static int glob3 __P((Char *, Char *, Char *, Char *, glob_t *)); |
static int glob3 __P((Char *, Char *, Char *, Char *, glob_t *)); |
| Line 167 glob(pattern, flags, errfunc, pglob) |
|
| Line 172 glob(pattern, flags, errfunc, pglob) |
|
| while ((c = *qpatnext++) != EOS) { |
while ((c = *qpatnext++) != EOS) { |
| switch (c) { |
switch (c) { |
| case LBRACKET: |
case LBRACKET: |
| pglob->gl_flags |= GLOB_MAGCHAR; |
|
| c = *qpatnext; |
c = *qpatnext; |
| if (c == NOT) |
if (c == NOT) |
| ++qpatnext; |
++qpatnext; |
| Line 191 glob(pattern, flags, errfunc, pglob) |
|
| Line 195 glob(pattern, flags, errfunc, pglob) |
|
| qpatnext += 2; |
qpatnext += 2; |
| } |
} |
| } while ((c = *qpatnext++) != RBRACKET); |
} while ((c = *qpatnext++) != RBRACKET); |
| |
pglob->gl_flags |= GLOB_MAGCHAR; |
| *bufnext++ = M_END; |
*bufnext++ = M_END; |
| break; |
break; |
| case QUESTION: |
case QUESTION: |
| Line 199 glob(pattern, flags, errfunc, pglob) |
|
| Line 204 glob(pattern, flags, errfunc, pglob) |
|
| break; |
break; |
| case STAR: |
case STAR: |
| pglob->gl_flags |= GLOB_MAGCHAR; |
pglob->gl_flags |= GLOB_MAGCHAR; |
| *bufnext++ = M_ALL; |
/* collapse adjacent stars to one, |
| |
* to avoid exponential behavior |
| |
*/ |
| |
if (bufnext == patbuf || bufnext[-1] != M_ALL) |
| |
*bufnext++ = M_ALL; |
| break; |
break; |
| default: |
default: |
| *bufnext++ = CHAR(c); |
*bufnext++ = CHAR(c); |
| Line 214 glob(pattern, flags, errfunc, pglob) |
|
| Line 223 glob(pattern, flags, errfunc, pglob) |
|
| if ((err = glob1(patbuf, pglob)) != 0) |
if ((err = glob1(patbuf, pglob)) != 0) |
| return(err); |
return(err); |
| |
|
| if (pglob->gl_pathc == oldpathc && flags & GLOB_NOCHECK) { |
/* |
| |
* If there was no match we are going to append the pattern |
| |
* if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified |
| |
* and the pattern did not contain any magic characters |
| |
* GLOB_NOMAGIC is there just for compatibility with csh. |
| |
*/ |
| |
if (pglob->gl_pathc == oldpathc && |
| |
((flags & GLOB_NOCHECK) || |
| |
((flags & GLOB_NOMAGIC) && !(pglob->gl_flags & GLOB_MAGCHAR)))) { |
| if (!(flags & GLOB_QUOTE)) { |
if (!(flags & GLOB_QUOTE)) { |
| Char *dp = compilebuf; |
Char *dp = compilebuf; |
| const u_char *sp = compilepat; |
const u_char *sp = compilepat; |
| Line 283 glob2(pathbuf, pathend, pattern, pglob) |
|
| Line 300 glob2(pathbuf, pathend, pattern, pglob) |
|
| for (anymeta = 0;;) { |
for (anymeta = 0;;) { |
| if (*pattern == EOS) { /* End of pattern? */ |
if (*pattern == EOS) { /* End of pattern? */ |
| *pathend = EOS; |
*pathend = EOS; |
| if (g_stat(pathbuf, &sb)) |
if (g_lstat(pathbuf, &sb, pglob)) |
| return(0); |
return(0); |
| |
|
| if (((pglob->gl_flags & GLOB_MARK) && |
if (((pglob->gl_flags & GLOB_MARK) && |
| pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) |
pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) |
| || (S_ISLNK(sb.st_mode) && |
|| (S_ISLNK(sb.st_mode) && |
| (g_stat(pathbuf, &sb) == 0) && |
(g_stat(pathbuf, &sb, pglob) == 0) && |
| S_ISDIR(sb.st_mode)))) { |
S_ISDIR(sb.st_mode)))) { |
| *pathend++ = SEP; |
*pathend++ = SEP; |
| *pathend = EOS; |
*pathend = EOS; |
| Line 324 glob3(pathbuf, pathend, pattern, restpat |
|
| Line 341 glob3(pathbuf, pathend, pattern, restpat |
|
| glob_t *pglob; |
glob_t *pglob; |
| { |
{ |
| register struct dirent *dp; |
register struct dirent *dp; |
| |
struct dirent *(*readdirfunc)(); |
| DIR *dirp; |
DIR *dirp; |
| int len, err; |
int len, err; |
| |
char buf[MAXPATHLEN]; |
| |
|
| *pathend = EOS; |
*pathend = EOS; |
| errno = 0; |
errno = 0; |
| |
|
| if (!(dirp = g_opendir(pathbuf))) |
if ((dirp = g_opendir(pathbuf, pglob)) == NULL) { |
| /* TODO: don't call for ENOENT or ENOTDIR? */ |
/* TODO: don't call for ENOENT or ENOTDIR? */ |
| if (pglob->gl_errfunc && |
if (pglob->gl_errfunc) { |
| (*pglob->gl_errfunc)(pathbuf, errno) || |
g_Ctoc(pathbuf, buf); |
| (pglob->gl_flags & GLOB_ERR)) |
if (pglob->gl_errfunc(buf, errno) || |
| return(GLOB_ABEND); |
pglob->gl_flags & GLOB_ERR) |
| else |
return (GLOB_ABEND); |
| return(0); |
} |
| |
return(0); |
| |
} |
| |
|
| err = 0; |
err = 0; |
| |
|
| /* Search directory for matching names. */ |
/* Search directory for matching names. */ |
| while ((dp = readdir(dirp))) { |
if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
| |
readdirfunc = pglob->gl_readdir; |
| |
else |
| |
readdirfunc = readdir; |
| |
while ((dp = (*readdirfunc)(dirp))) { |
| register u_char *sc; |
register u_char *sc; |
| register Char *dc; |
register Char *dc; |
| |
|
| Line 360 glob3(pathbuf, pathend, pattern, restpat |
|
| Line 385 glob3(pathbuf, pathend, pattern, restpat |
|
| break; |
break; |
| } |
} |
| |
|
| /* TODO: check error from readdir? */ |
if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
| (void)closedir(dirp); |
(*pglob->gl_closedir)(dirp); |
| |
else |
| |
closedir(dirp); |
| return(err); |
return(err); |
| } |
} |
| |
|
| Line 431 match(name, pat, patend) |
|
| Line 458 match(name, pat, patend) |
|
| case M_ALL: |
case M_ALL: |
| if (pat == patend) |
if (pat == patend) |
| return(1); |
return(1); |
| for (; *name != EOS; ++name) |
do |
| if (match(name, pat, patend)) |
if (match(name, pat, patend)) |
| return(1); |
return(1); |
| |
while (*name++ != EOS); |
| return(0); |
return(0); |
| case M_ONE: |
case M_ONE: |
| if (*name++ == EOS) |
if (*name++ == EOS) |
| Line 441 match(name, pat, patend) |
|
| Line 469 match(name, pat, patend) |
|
| break; |
break; |
| case M_SET: |
case M_SET: |
| ok = 0; |
ok = 0; |
| k = *name++; |
if ((k = *name++) == EOS) |
| |
return(0); |
| if (negate_range = ((*pat & M_MASK) == M_NOT)) |
if (negate_range = ((*pat & M_MASK) == M_NOT)) |
| ++pat; |
++pat; |
| while (((c = *pat++) & M_MASK) != M_END) |
while (((c = *pat++) & M_MASK) != M_END) |
|
|
| } |
} |
| |
|
| static DIR * |
static DIR * |
| g_opendir(str) |
g_opendir(str, pglob) |
| register Char *str; |
register Char *str; |
| |
glob_t *pglob; |
| { |
{ |
| char buf[MAXPATHLEN]; |
char buf[MAXPATHLEN]; |
| |
char *dirname; |
| |
|
| if (!*str) |
if (!*str) |
| return(opendir(".")); |
strcpy(buf, "."); |
| g_Ctoc(str, buf); |
else |
| |
g_Ctoc(str, buf); |
| |
if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
| |
return((*pglob->gl_opendir)(buf)); |
| return(opendir(buf)); |
return(opendir(buf)); |
| } |
} |
| |
|
| static int |
static int |
| g_lstat(fn, sb) |
g_lstat(fn, sb, pglob) |
| register Char *fn; |
register Char *fn; |
| struct stat *sb; |
struct stat *sb; |
| |
glob_t *pglob; |
| { |
{ |
| char buf[MAXPATHLEN]; |
char buf[MAXPATHLEN]; |
| |
|
| g_Ctoc(fn, buf); |
g_Ctoc(fn, buf); |
| |
if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
| |
return((*pglob->gl_lstat)(buf, sb)); |
| return(lstat(buf, sb)); |
return(lstat(buf, sb)); |
| } |
} |
| |
|
| static int |
static int |
| g_stat(fn, sb) |
g_stat(fn, sb, pglob) |
| register Char *fn; |
register Char *fn; |
| struct stat *sb; |
struct stat *sb; |
| |
glob_t *pglob; |
| { |
{ |
| char buf[MAXPATHLEN]; |
char buf[MAXPATHLEN]; |
| |
|
| g_Ctoc(fn, buf); |
g_Ctoc(fn, buf); |
| |
if (pglob->gl_flags & GLOB_ALTDIRFUNC) |
| |
return((*pglob->gl_stat)(buf, sb)); |
| return(stat(buf, sb)); |
return(stat(buf, sb)); |
| } |
} |
| |
|
|
|
| register Char *p; |
register Char *p; |
| |
|
| for (p = s; *p; p++) |
for (p = s; *p; p++) |
| (void)printf("%c", *p & 0xff); |
(void)printf("%c", CHAR(*p)); |
| (void)printf("\n"); |
(void)printf("\n"); |
| for (p = s; *p; p++) |
for (p = s; *p; p++) |
| (void)printf("%c", *p & M_PROTECT ? '"' : ' '); |
(void)printf("%c", *p & M_PROTECT ? '"' : ' '); |
| (void)printf("\n"); |
(void)printf("\n"); |
| for (p = s; *p; p++) |
for (p = s; *p; p++) |
| (void)printf("%c", *p & M_META ? '_' : ' '); |
(void)printf("%c", ismeta(*p) ? '_' : ' '); |
| (void)printf("\n"); |
(void)printf("\n"); |
| } |
} |
| #endif |
#endif |