glob.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Guido van Rossum.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "includes.h"
#include <ctype.h>
static long
get_arg_max(void)
{
#ifdef ARG_MAX
return(ARG_MAX);
return(sysconf(_SC_ARG_MAX));
#else
return(256); /* XXX: arbitrary */
#endif
}
#if !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) || \
!defined(GLOB_HAS_GL_MATCHC)
#if 0
#else
#endif
#endif /* LIBC_SCCS and not lint */
/*
* glob(3) -- a superset of the one defined in POSIX 1003.2.
*
* The [!...] convention to negate a range is supported (SysV, Posix, ksh).
*
* Optional extra services, controlled by flags not defined by POSIX:
*
* GLOB_QUOTE:
* Escaping convention: \ inhibits any special meaning the following
* character might have (except \ at end of string is retained).
* GLOB_MAGCHAR:
* 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.
* GLOB_TILDE:
* GLOB_BRACE:
* expand {1,2}{a,b} to 1a 1b 2a 2b
* gl_matchc:
* Number of matches in the current invocation of glob.
*/
#define DOLLAR '$'
#define DOT '.'
#define EOS '\0'
#define LBRACKET '['
#define NOT '!'
#define QUESTION '?'
#define QUOTE '\\'
#define RANGE '-'
#define RBRACKET ']'
#define SEP '/'
#define STAR '*'
#define TILDE '~'
#define UNDERSCORE '_'
#define LBRACE '{'
#define RBRACE '}'
#define SLASH '/'
#define COMMA ','
#ifndef DEBUG
#define M_QUOTE 0x8000
#define M_PROTECT 0x4000
#define M_MASK 0xffff
#define M_ASCII 0x00ff
#else
#define M_QUOTE 0x80
#define M_PROTECT 0x40
#define M_MASK 0xff
#define M_ASCII 0x7f
typedef char Char;
#endif
static int compare(const void *, const void *);
static const Char *
#ifdef DEBUG
#endif
int
const char *pattern;
{
int c;
if (!(flags & GLOB_APPEND)) {
if (!(flags & GLOB_DOOFFS))
}
if (flags & GLOB_NOESCAPE)
*bufnext++ = c;
else {
/* Protect the quoted characters. */
if (c == QUOTE) {
c = QUOTE;
--patnext;
}
} else
*bufnext++ = c;
}
if (flags & GLOB_BRACE)
else
}
/*
* Expand recursively a glob {} pattern. When there is no more expansion
* invoke the standard globbing routine to glob the rest of the magic
* characters
*/
static int
{
int rv;
/* Protect a single {}, for find(1), like csh */
return rv;
}
/*
* Recursive brace globbing helper. Tries to expand a single brace.
* If it succeeds then it invokes globexp1 with the new pattern.
* If it fails then it tries to glob the rest of the pattern and returns.
*/
static int
int *rv;
{
int i;
/* copy part up to the brace */
;
/* Find the balanced brace */
/* Ignore everything between [] */
;
/*
* We could not find a matching RBRACKET.
* Ignore and just look for RBRACE
*/
}
i++;
if (i == 0)
break;
i--;
}
/* Non matching braces; just glob the pattern */
return 0;
}
switch (*pm) {
case LBRACKET:
/* Ignore everything between [] */
;
/*
* We could not find a matching RBRACKET.
* Ignore and just look for RBRACE
*/
}
break;
case LBRACE:
i++;
break;
case RBRACE:
if (i) {
i--;
break;
}
/* FALLTHROUGH */
case COMMA:
break;
else {
/* Append the current string */
;
/*
* Append the rest of the pattern after the
* closing brace
*/
;
/* Expand the current pattern */
#ifdef DEBUG
#endif
/* move after the comma, to the next string */
}
break;
default:
break;
}
}
*rv = 0;
return 0;
}
/*
* expand tilde from the passwd file.
*/
static const Char *
{
char *h;
const Char *p;
return pattern;
/* Copy up to the end of the string or / */
;
*h = EOS;
#if 0
if (h == (char *)eb)
return what;
#endif
/*
* handle a plain ~ or ~/ by expanding $HOME
* first and then trying the password file
*/
#if 0
#endif
return pattern;
else
}
} else {
/*
* Expand a ~user
*/
return pattern;
else
}
/* Copy the home directory */
;
/* Append the rest of the pattern */
;
*b = EOS;
return patbuf;
}
/*
* The main glob() routine: compiles the pattern (optionally processing
* quotes), calls glob1() to do the real pattern matching, and finally
* sorts the list (unless unsorted operation is requested). Returns 0
* if things went well, nonzero if errors occurred. It is not an error
* to find no matches.
*/
static int
{
/* We don't need to check for buffer overflow any more. */
switch (c) {
case LBRACKET:
c = *qpatnext;
if (c == NOT)
++qpatnext;
if (c == NOT)
--qpatnext;
break;
}
if (c == NOT)
c = *qpatnext++;
do {
qpatnext += 2;
}
break;
case QUESTION:
break;
case STAR:
/* collapse adjacent stars to one,
* to avoid exponential behavior
*/
break;
default:
break;
}
}
#ifdef DEBUG
#endif
return(err);
/*
* 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.
*/
else
return(GLOB_NOMATCH);
}
return(0);
}
static int
compare(p, q)
const void *p, *q;
{
return(strcmp(*(char **)p, *(char **)q));
}
static int
{
/* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
return(0);
}
/*
* The functions glob2 and glob3 are mutually recursive; there is one level
* of recursion for each segment in the pattern that contains one or more
* meta characters.
*/
static int
{
Char *p, *q;
int anymeta;
/*
* Loop over pattern segments until end of pattern or until
* segment with meta character found.
*/
for (anymeta = 0;;) {
return(0);
return (1);
}
}
/* Find end of next segment, copy tentatively to pathend. */
q = pathend;
p = pattern;
if (ismeta(*p))
anymeta = 1;
if (q+1 > pathend_last)
return (1);
*q++ = *p++;
}
if (!anymeta) { /* No expansion, do next segment. */
pathend = q;
pattern = p;
return (1);
}
} else
/* Need expansion, recurse. */
}
/* NOTREACHED */
}
static int
{
int err;
char buf[MAXPATHLEN];
/*
* The readdirfunc declaration can't be prototyped, because it is
* assigned, below, to two functions which are prototyped in glob.h
* and dirent.h as taking pointers to differently typed opaque
* structures.
*/
struct dirent *(*readdirfunc)();
if (pathend > pathend_last)
return (1);
errno = 0;
/* TODO: don't call for ENOENT or ENOTDIR? */
if (pglob->gl_errfunc) {
return(GLOB_ABORTED);
return(GLOB_ABORTED);
}
return(0);
}
err = 0;
/* Search directory for matching names. */
else
/* Initial DOT must be matched literally. */
continue;
;
if (dc >= pathend_last) {
err = 1;
break;
}
continue;
}
if (err)
break;
}
else
return(err);
}
/*
* Extend the gl_pathv member of a glob_t structure to accommodate a new item,
* add the new item, and update gl_pathc.
*
* This assumes the BSD realloc, which only copies the block when its size
* crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
* behavior.
*
* Return 0 if new item added, error code if memory couldn't be allocated.
*
* Invariant of the glob_t structure:
* Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
* gl_pathv points to (gl_offs + gl_pathc + 1) items.
*/
static int
{
register char **pathv;
register int i;
char *copy;
const Char *p;
}
return(GLOB_NOSPACE);
}
/* first time around -- clear initial gl_offs items */
}
for (p = path; *p++;)
;
return(GLOB_NOSPACE);
}
}
errno = 0;
return(GLOB_NOSPACE);
}
}
/*
* pattern matching function for filenames. Each occurrence of the *
* pattern causes a recursion level.
*/
static int
{
int ok, negate_range;
Char c, k;
c = *pat++;
switch (c & M_MASK) {
case M_ALL:
return(1);
do
return(1);
;
return(0);
case M_ONE:
return(0);
break;
case M_SET:
ok = 0;
return(0);
++pat;
if (c <= k && k <= pat[1])
ok = 1;
pat += 2;
} else if (c == k)
ok = 1;
if (ok == negate_range)
return(0);
break;
default:
if (*name++ != c)
return(0);
break;
}
}
}
/* Free allocated data belonging to a glob_t structure. */
void
{
register int i;
register char **pp;
if (*pp)
}
}
static DIR *
{
char buf[MAXPATHLEN];
if (!*str)
else {
return(NULL);
}
}
static int
{
char buf[MAXPATHLEN];
return(-1);
}
static int
{
char buf[MAXPATHLEN];
return(-1);
}
static Char *
int ch;
{
do {
return (str);
} while (*str++);
return (NULL);
}
static int
char *buf;
{
while (len--) {
return (0);
}
return (1);
}
#ifdef DEBUG
static void
const char *str;
register Char *s;
{
register Char *p;
for (p = s; *p; p++)
(void)printf("\n");
for (p = s; *p; p++)
(void)printf("\n");
for (p = s; *p; p++)
(void)printf("\n");
}
#endif
#endif /* !defined(HAVE_GLOB) || !defined(GLOB_HAS_ALTDIRFUNC) ||
!defined(GLOB_HAS_GL_MATCHC) */
#pragma ident "%Z%%M% %I% %E% SMI"