/*
* Copyright (c) 2013 Gary Mills
*/
/* $OpenBSD: glob.c,v 1.39 2012/01/20 07:09:42 tedu Exp $ */
/*
* 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. 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.
*/
/*
* 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.
*/
#include "lint.h"
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <glob.h>
#include <limits.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>
#include <wctype.h>
/*
* This is the legacy glob_t prior to illumos enhancement 1097,
* used when old programs call the old libc glob functions.
* (New programs call the _glob_ext, _globfree_ext functions.)
* This struct should be considered "carved in stone".
*/
typedef struct old_glob {
/* following are internal to the implementation */
} old_glob_t;
/*
* For old programs, the external names need to be the old names:
* glob() and globfree() . We've redefined those already to
* _glob_ext() and _globfree_ext() . Now redefine old_glob()
* and old_globfree() to glob() and globfree() .
*/
#ifdef __PRAGMA_REDEFINE_EXTNAME
#endif /* __PRAGMA_REDEFINE_EXTNAME */
extern int old_glob(const char *, int, int (*)(const char *, int),
old_glob_t *);
extern void old_globfree(old_glob_t *);
/*
* The various extensions to glob(3C) allow for stat and dirent structures to
* show up whose size may change in a largefile environment. If libc defines
* _FILE_OFFSET_BITS to be 64 that is the key to indicate that we're building
* the LFS version of this file. As such, we rename the public functions here,
* _glob_ext() and _globfree_ext() to have a 64 suffix. When building the LFS
* version, we do not include the old versions.
*/
#endif /* !_LP64 && _FILE_OFFSET_BITS == 64 */
typedef struct wcat {
} wcat_t;
/* Limit of recursion during matching attempts. */
struct glob_lim {
};
struct glob_path_stat {
char *gps_path;
};
static int compare(const void *, const void *);
static int compare_gps(const void *, const void *);
int (*)(const char *, int));
int (*)(const char *, int));
int (*)(const char *, int));
int (*)(const char *, int));
struct stat *);
static
int (*)(const char *, int));
struct glob_lim *, int (*)(const char *, int));
/*
* Extended glob() function, selected by #pragma redefine_extname
* in glob.h with the external name _glob_ext() .
*/
int
{
const char *patnext;
int n;
wchar_t c;
return (GLOB_NOMATCH);
if (!(flags & GLOB_APPEND)) {
if ((flags & GLOB_KEEPSTAT) != 0)
if (!(flags & GLOB_DOOFFS))
}
return (GLOB_NOSPACE);
patlen += 1;
if (flags & GLOB_NOESCAPE) {
patnext += n;
patlen -= n;
} else if (n == 0) {
break;
} else {
return (GLOB_NOMATCH);
}
}
} else {
/* Protect the quoted characters. */
patnext += n;
patlen -= n;
if (c == QUOTE) {
if (n < 0)
return (GLOB_NOMATCH);
if (n > 0) {
patnext += n;
patlen -= n;
}
if (n == 0)
c = QUOTE;
} else {
}
} else if (n == 0) {
break;
} else {
return (GLOB_NOMATCH);
}
}
}
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 (*errfunc)(const char *, int))
{
/* Protect a single {}, for find(1), like csh */
}
/*
* 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 i, rv;
/* 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 */
case LBRACKET:
/* Ignore everything between [] */
pm++)
;
/*
* 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 */
return (rv);
/* move after the comma, to the next string */
}
break;
default:
break;
}
}
return (0);
}
/*
* expand tilde from the passwd file.
*/
static const wcat_t *
{
char *h;
const wcat_t *p;
int n;
wchar_t c;
return (pattern);
/* Copy up to the end of the string or / */
;
q->w_at = 0;
/* What to do if patbuf is full? */
/*
* handle a plain ~ or ~/ by expanding $HOME
* first and then trying the password file
*/
if (issetugid() != 0)
return (pattern);
return (pattern);
else
}
} else {
/*
* Expand a ~user
*/
return (pattern);
else
}
/* Copy the home directory */
h += n;
lenh -= n;
b->w_at = 0;
b->w_wc = c;
} else if (n < 0) {
return (pattern);
} else {
break;
}
}
/* Append the rest of the pattern */
;
b->w_at = 0;
return (patbuf);
}
static int
{
return (1); /* not a character class */
return (-1); /* invalid character class */
{
wchar_t w;
/* Copy the string. */
while (n > 0) {
/* Character class names must be ASCII. */
if (iswascii(w)) {
n--;
*s2++ = w;
} else {
return (-1); /* invalid character class */
}
}
}
return (-1); /* invalid character class */
return (0);
}
/*
* 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
int (*errfunc)(const char *, int))
{
wchar_t c;
int a;
/*
* We don't need to check for buffer overflow any more.
* The pattern has already been copied to an internal buffer.
*/
switch (c) {
case LBRACKET:
if (a != 0) {
break;
}
if (a == 0 && c == NOT)
++qpatnext;
if (a == 0 && c == NOT)
--qpatnext;
break;
}
if (a == 0 && c == NOT) {
}
do {
if (a == 0 && c == LBRACKET &&
do {
&bufnext);
if (err)
break;
} while (a == 0 && c == LBRACKET &&
if (err == -1 &&
return (GLOB_NOMATCH);
if (a == 0 && c == RBRACKET)
break;
}
qpatnext += 2;
}
}
} while (a != 0 || c != RBRACKET);
break;
case QUESTION:
if (a != 0) {
break;
}
break;
case STAR:
if (a != 0) {
break;
}
/*
* collapse adjacent stars to one,
* to avoid exponential behavior
*/
}
break;
default:
break;
}
}
!= 0)
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);
}
/* Keep the paths and stat info synced during sort */
int i;
NULL)
return (GLOB_NOSPACE);
for (i = 0; i < n; i++) {
}
for (i = 0; i < n; i++) {
}
} else {
compare);
}
}
return (0);
}
static int
compare(const void *p, const void *q)
{
return (strcmp(*(char **)p, *(char **)q));
}
static int
{
}
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
{
wcat_t *p, *q;
int anymeta;
/*
* Loop over pattern segments until end of pattern or until
* segment with meta character found.
*/
for (anymeta = 0; ; ) {
errno = 0;
return (GLOB_NOSPACE);
}
return (0);
return (GLOB_NOSPACE);
}
}
/* Find end of next segment, copy tentatively to pathend. */
q = pathend;
p = pattern;
if (ismeta(*p))
anymeta = 1;
if (q+1 > pathend_last)
return (GLOB_NOSPACE);
*q++ = *p++;
}
if (!anymeta) { /* No expansion, do next segment. */
pathend = q;
pattern = p;
return (GLOB_NOSPACE);
}
} else {
/* Need expansion, recurse. */
}
}
/* NOTREACHED */
}
static int
int (*errfunc)(const char *, int))
{
int err;
/*
* 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)(void *);
if (pathend > pathend_last)
return (GLOB_NOSPACE);
errno = 0;
/* TODO: don't call for ENOENT or ENOTDIR? */
if (errfunc) {
return (GLOB_ABORTED);
return (GLOB_ABORTED);
}
return (0);
}
err = 0;
/* Search directory for matching names. */
else
char *sc;
int n;
int lensc;
wchar_t w;
errno = 0;
err = GLOB_NOSPACE;
break;
}
/* Initial DOT must be matched literally. */
continue;
while (dc < pathend_last) {
sc += 1;
lensc -= 1;
} else {
sc += n;
lensc -= n;
}
dc++;
if (n <= 0)
break;
}
if (dc >= pathend_last) {
err = GLOB_NOSPACE;
break;
}
if (n < 0) {
err = GLOB_NOMATCH;
break;
}
continue;
}
errfunc);
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. Avoids excessive reallocation
* by doubling the number of elements each time. Uses gl_pathn to contain
* the number.
*
* 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
{
char **pathv;
ssize_t i;
const wcat_t *p;
int n;
} else {
if (allocn == 0)
allocn *= 2;
i++) {
}
}
}
return (GLOB_NOSPACE);
}
goto nospace;
goto nospace;
}
}
/* first time around -- clear initial gl_offs items */
}
/* first time around -- clear initial gl_offs items */
}
else {
goto copy_error;
}
}
len = MB_LEN_MAX;
p = path;
len += n;
break;
}
if (n < 0)
return (GLOB_NOMATCH);
return (GLOB_NOSPACE);
}
}
errno = 0;
return (GLOB_NOSPACE);
}
}
/*
* pattern matching function for filenames. Each occurrence of the *
* pattern causes a recursion level.
*/
static int
{
wcat_t c, k;
if (recur-- == 0)
return (1);
c = *pat++;
switch (c.w_wc) {
case M_ALL:
k = *name++;
return (0);
break;
}
pat++; /* eat consecutive '*' */
return (1);
do {
return (1);
return (0);
case M_ONE:
k = *name++;
return (0);
break;
}
return (0);
break;
case M_SET:
k = *name++;
return (0);
break;
}
ok = 0;
return (0);
++pat;
ok = 1;
++pat;
}
ok = 1;
pat += 2;
ok = 1;
}
if (ok == negate_range)
return (0);
break;
default:
k = *name++;
return (0);
break;
}
}
}
/*
* Extended globfree() function, selected by #pragma redefine_extname
* in glob.h with the external name _globfree_ext() .
*/
void
{
int i;
char **pp;
if (*pp)
}
}
}
}
static DIR *
{
else {
return (NULL);
}
}
static int
{
return (-1);
}
static int
{
return (-1);
}
static wcat_t *
{
do {
return (NULL);
}
static int
{
int n;
wchar_t w;
while (len >= MB_LEN_MAX) {
len -= n;
buf += n;
}
if (n < 0)
break;
if (w == EOS)
return (0);
}
return (1);
}
/* glob() function with legacy glob structure */
int
{
int rv;
flags &= GLOB_POSIX;
/*
* Copy all the members, old to new. There's
* really no point in micro-optimizing the copying.
* Other members are set to zero.
*/
/*
* Copy all the members, new to old. There's
* really no point in micro-optimizing the copying.
*/
return (rv);
}
/* globfree() function with legacy glob structure */
void
{
/*
* Copy all the members, old to new. There's
* really no point in micro-optimizing the copying.
* Other members are set to zero.
*/
_globfree_ext(&gl);
/*
* Copy all the members, new to old. There's
* really no point in micro-optimizing the copying.
*/
}
#endif /* _LP64 || _FILE_OFFSET_BITS != 64 */