diff options
Diffstat (limited to 'libc/misc/glob')
-rw-r--r-- | libc/misc/glob/Makefile.in | 35 | ||||
-rw-r--r-- | libc/misc/glob/glob-hooks.c | 6 | ||||
-rw-r--r-- | libc/misc/glob/glob-susv3.c | 322 | ||||
-rw-r--r-- | libc/misc/glob/glob.c | 518 | ||||
-rw-r--r-- | libc/misc/glob/glob64-susv3.c | 20 | ||||
-rw-r--r-- | libc/misc/glob/glob64.c | 35 |
6 files changed, 622 insertions, 314 deletions
diff --git a/libc/misc/glob/Makefile.in b/libc/misc/glob/Makefile.in index cd845464f..2eda3dea2 100644 --- a/libc/misc/glob/Makefile.in +++ b/libc/misc/glob/Makefile.in @@ -1,31 +1,32 @@ # Makefile for uClibc # # Copyright (C) 2000 by Lineo, inc. -# Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org> +# Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org> # # Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. # -CSRC:=glob.c glob64.c glob-hooks.c +ifeq ($(UCLIBC_HAS_GNU_GLOB),y) +CSRC := glob.c glob-hooks.c +ifeq ($(UCLIBC_HAS_LFS),y) +CSRC += glob64.c +endif +else +CSRC := glob-susv3.c +ifeq ($(UCLIBC_HAS_LFS),y) +CSRC += glob64-susv3.c +endif +endif -MISC_GLOB_DIR:=$(top_srcdir)libc/misc/glob -MISC_GLOB_OUT:=$(top_builddir)libc/misc/glob +MISC_GLOB_DIR := $(top_srcdir)libc/misc/glob +MISC_GLOB_OUT := $(top_builddir)libc/misc/glob -MISC_GLOB_SRC:=$(patsubst %.c,$(MISC_GLOB_DIR)/%.c,$(CSRC)) -MISC_GLOB_OBJ:=$(patsubst %.c,$(MISC_GLOB_OUT)/%.o,$(CSRC)) +MISC_GLOB_SRC := $(patsubst %.c,$(MISC_GLOB_DIR)/%.c,$(CSRC)) +MISC_GLOB_OBJ := $(patsubst %.c,$(MISC_GLOB_OUT)/%.o,$(CSRC)) -$(MISC_GLOB_OUT)/glob64.o: $(MISC_GLOB_DIR)/glob64.c $(MISC_GLOB_DIR)/glob.c +libc-$(UCLIBC_HAS_GLOB) += $(MISC_GLOB_OBJ) -$(MISC_GLOB_OUT)/glob64.os: $(MISC_GLOB_DIR)/glob64.c $(MISC_GLOB_DIR)/glob.c - -libc-a-$(UCLIBC_HAS_GLOB)+=$(MISC_GLOB_OBJ) -libc-so-$(UCLIBC_HAS_GLOB)+=$(MISC_GLOB_OBJ:.o=.os) - -# glob has to be rewritten to allow multi -#libc-multi-$(UCLIBC_HAS_GLOB)+=$(MISC_GLOB_SRC) -libc-nomulti-$(UCLIBC_HAS_GLOB)+=$(MISC_GLOB_OBJ) - -objclean-y+=misc_glob_objclean +objclean-y += misc_glob_objclean misc_glob_objclean: $(RM) $(MISC_GLOB_OUT)/*.{o,os} diff --git a/libc/misc/glob/glob-hooks.c b/libc/misc/glob/glob-hooks.c index e2d8030cf..e3529181f 100644 --- a/libc/misc/glob/glob-hooks.c +++ b/libc/misc/glob/glob-hooks.c @@ -29,7 +29,7 @@ #include <glob.h> -__ptr_t (*__glob_opendir_hook) __P ((const char *directory)); -const char *(*__glob_readdir_hook) __P ((__ptr_t stream)); -void (*__glob_closedir_hook) __P ((__ptr_t stream)); +attribute_hidden __ptr_t (*__glob_opendir_hook) (const char *directory); +attribute_hidden const char *(*__glob_readdir_hook) (__ptr_t stream); +attribute_hidden void (*__glob_closedir_hook) (__ptr_t stream); diff --git a/libc/misc/glob/glob-susv3.c b/libc/misc/glob/glob-susv3.c new file mode 100644 index 000000000..e5b48cac3 --- /dev/null +++ b/libc/misc/glob/glob-susv3.c @@ -0,0 +1,322 @@ +/* + * Copyright (C) 2006 Rich Felker <dalias@aerifal.cx> + * + * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. + */ + +#include <features.h> + +#ifdef __UCLIBC_HAS_LFS__ +# define BUILD_GLOB64 +#endif + +#include <glob.h> +#include <fnmatch.h> +#include <sys/stat.h> +#include <dirent.h> +#include <limits.h> +#include <string.h> +#include <stdlib.h> +#include <errno.h> +#include <stddef.h> + +#include <unistd.h> +#include <stdio.h> + +libc_hidden_proto(memcpy) +libc_hidden_proto(strcat) +libc_hidden_proto(strchr) +libc_hidden_proto(strcmp) +libc_hidden_proto(strcpy) +libc_hidden_proto(strlen) +libc_hidden_proto(opendir) +libc_hidden_proto(closedir) +libc_hidden_proto(qsort) +libc_hidden_proto(fnmatch) + +struct match +{ + struct match *next; + char name[1]; +}; + +#ifdef BUILD_GLOB64 +extern int __glob_is_literal(const char *p, int useesc) attribute_hidden; +extern int __glob_append(struct match **tail, const char *name, size_t len, int mark) attribute_hidden; +extern int __glob_ignore_err(const char *path, int err) attribute_hidden; +extern void __glob_freelist(struct match *head) attribute_hidden; +extern int __glob_sort(const void *a, const void *b) attribute_hidden; +extern int __glob_match_in_dir(const char *d, const char *p, int flags, int (*errfunc)(const char *path, int err), struct match **tail) attribute_hidden; +#endif + +#ifdef __UCLIBC_HAS_LFS__ +# define stat stat64 +# define readdir_r readdir64_r +# define dirent dirent64 +libc_hidden_proto(readdir64_r) +libc_hidden_proto(stat64) +# define struct_stat struct stat64 +#else +libc_hidden_proto(readdir_r) +libc_hidden_proto(stat) +# define struct_stat struct stat +#endif + +/* keep only one copy of these */ +#ifndef __GLOB64 + +# ifndef BUILD_GLOB64 +static +# endif +int __glob_is_literal(const char *p, int useesc) +{ + int bracket = 0; + for (; *p; p++) { + switch (*p) { + case '\\': + if (!useesc) break; + case '?': + case '*': + return 0; + case '[': + bracket = 1; + break; + case ']': + if (bracket) return 0; + break; + } + } + return 1; +} + +# ifndef BUILD_GLOB64 +static +# endif +int __glob_append(struct match **tail, const char *name, size_t len, int mark) +{ + struct match *new = malloc(sizeof(struct match) + len + 1); + if (!new) return -1; + (*tail)->next = new; + new->next = NULL; + strcpy(new->name, name); + if (mark) strcat(new->name, "/"); + *tail = new; + return 0; +} + +# ifndef BUILD_GLOB64 +static +# endif +int __glob_match_in_dir(const char *d, const char *p, int flags, int (*errfunc)(const char *path, int err), struct match **tail) +{ + DIR *dir; + long long de_buf[(sizeof(struct dirent) + NAME_MAX + sizeof(long long))/sizeof(long long)]; + struct dirent *de; + char pat[strlen(p)+1]; + char *p2; + size_t l = strlen(d); + int literal; + int fnm_flags= ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0) | FNM_PERIOD; + int error; + + if ((p2 = strchr(p, '/'))) { + strcpy(pat, p); + pat[p2-p] = 0; + for (; *p2 == '/'; p2++); + p = pat; + } + literal = __glob_is_literal(p, !(flags & GLOB_NOESCAPE)); + if (*d == '/' && !*(d+1)) l = 0; + + /* rely on opendir failing for nondirectory objects */ + dir = opendir(*d ? d : "."); + error = errno; + if (!dir) { + /* this is not an error -- we let opendir call stat for us */ + if (error == ENOTDIR) return 0; + if (error == EACCES && !*p) { + struct_stat st; + if (!stat(d, &st) && S_ISDIR(st.st_mode)) { + if (__glob_append(tail, d, l, l)) + return GLOB_NOSPACE; + return 0; + } + } + if (errfunc(d, error) || (flags & GLOB_ERR)) + return GLOB_ABORTED; + return 0; + } + if (!*p) { + error = __glob_append(tail, d, l, l) ? GLOB_NOSPACE : 0; + closedir(dir); + return error; + } + while (!(error = readdir_r(dir, (void *)de_buf, &de)) && de) { + char namebuf[l+de->d_reclen+2], *name = namebuf; + if (!literal && fnmatch(p, de->d_name, fnm_flags)) + continue; + if (literal && strcmp(p, de->d_name)) + continue; + if (p2 && de->d_type && !S_ISDIR(de->d_type<<12) && !S_ISLNK(de->d_type<<12)) + continue; + if (*d) { + memcpy(name, d, l); + name[l] = '/'; + strcpy(name+l+1, de->d_name); + } else { + name = de->d_name; + } + if (p2) { + if ((error = __glob_match_in_dir(name, p2, flags, errfunc, tail))) { + closedir(dir); + return error; + } + } else { + int mark = 0; + if (flags & GLOB_MARK) { + if (de->d_type) + mark = S_ISDIR(de->d_type<<12); + else { + struct_stat st; + stat(name, &st); + mark = S_ISDIR(st.st_mode); + } + } + if (__glob_append(tail, name, l+de->d_reclen+1, mark)) { + closedir(dir); + return GLOB_NOSPACE; + } + } + } + closedir(dir); + if (error && (errfunc(d, error) || (flags & GLOB_ERR))) + return GLOB_ABORTED; + return 0; +} + +# ifndef BUILD_GLOB64 +static +# endif +int __glob_ignore_err(const char *path, int err) +{ + return 0; +} + +# ifndef BUILD_GLOB64 +static +# endif +void __glob_freelist(struct match *head) +{ + struct match *match, *next; + for (match=head->next; match; match=next) { + next = match->next; + free(match); + } +} + +# ifndef BUILD_GLOB64 +static +# endif +int __glob_sort(const void *a, const void *b) +{ + return strcmp(*(const char **)a, *(const char **)b); +} +#endif /* !__GLOB64 */ + +#ifdef __GLOB64 +libc_hidden_proto(glob64) +#else +libc_hidden_proto(glob) +#endif +int glob(const char *pat, int flags, int (*errfunc)(const char *path, int err), glob_t *g) +{ + const char *p=pat, *d; + struct match head = { .next = NULL }, *tail = &head; + size_t cnt, i; + size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0; + int error = 0; + + if (*p == '/') { + for (; *p == '/'; p++); + d = "/"; + } else { + d = ""; + } + + if (!errfunc) errfunc = __glob_ignore_err; + + if (!(flags & GLOB_APPEND)) { + g->gl_offs = offs; + g->gl_pathc = 0; + g->gl_pathv = NULL; + } + + if (*p) error = __glob_match_in_dir(d, p, flags, errfunc, &tail); + if (error == GLOB_NOSPACE) { + __glob_freelist(&head); + return error; + } + + for (cnt=0, tail=head.next; tail; tail=tail->next, cnt++); + if (!cnt) { + if (flags & GLOB_NOCHECK) { + tail = &head; + if (__glob_append(&tail, pat, strlen(pat), 0)) + return GLOB_NOSPACE; + cnt++; + } else + return GLOB_NOMATCH; + } + + if (flags & GLOB_APPEND) { + char **pathv = realloc(g->gl_pathv, (offs + g->gl_pathc + cnt + 1) * sizeof(char *)); + if (!pathv) { + __glob_freelist(&head); + return GLOB_NOSPACE; + } + g->gl_pathv = pathv; + offs += g->gl_pathc; + } else { + g->gl_pathv = malloc((offs + cnt + 1) * sizeof(char *)); + if (!g->gl_pathv) { + __glob_freelist(&head); + return GLOB_NOSPACE; + } + for (i=0; i<offs; i++) + g->gl_pathv[i] = NULL; + } + for (i=0, tail=head.next; i<cnt; tail=tail->next, i++) + g->gl_pathv[offs + i] = tail->name; + g->gl_pathv[offs + i] = NULL; + g->gl_pathc += cnt; + + if (!(flags & GLOB_NOSORT)) + qsort(g->gl_pathv+offs, cnt, sizeof(char *), __glob_sort); + + return error; +} +#ifdef __GLOB64 +libc_hidden_def(glob64) +#else +libc_hidden_def(glob) +#endif + +#ifdef __GLOB64 +libc_hidden_proto(globfree64) +#else +libc_hidden_proto(globfree) +#endif +void globfree(glob_t *g) +{ + size_t i; + for (i=0; i<g->gl_pathc; i++) + free(g->gl_pathv[g->gl_offs + i] - offsetof(struct match, name)); + free(g->gl_pathv); + g->gl_pathc = 0; + g->gl_pathv = NULL; +} +#ifdef __GLOB64 +libc_hidden_def(globfree64) +#else +libc_hidden_def(globfree) +#endif diff --git a/libc/misc/glob/glob.c b/libc/misc/glob/glob.c index ea87d371c..f1875e8e3 100644 --- a/libc/misc/glob/glob.c +++ b/libc/misc/glob/glob.c @@ -15,11 +15,6 @@ License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#define strrchr __strrchr -#define strcoll __strcoll -#define qsort __qsort -#define fnmatch __fnmatch - #include <features.h> #include <stdlib.h> #include <string.h> @@ -30,36 +25,42 @@ Cambridge, MA 02139, USA. */ #include <dirent.h> #include <malloc.h> #include <fnmatch.h> -#define _GNU_SOURCE #include <glob.h> -extern DIR *__opendir (__const char *__name) __nonnull ((1)) attribute_hidden; -extern int __closedir (DIR *__dirp) __nonnull ((1)) attribute_hidden; +libc_hidden_proto(memcpy) +libc_hidden_proto(strcat) +libc_hidden_proto(strcmp) +libc_hidden_proto(strlen) +libc_hidden_proto(strrchr) +libc_hidden_proto(strcoll) +libc_hidden_proto(opendir) +libc_hidden_proto(closedir) +libc_hidden_proto(fnmatch) +libc_hidden_proto(qsort) +libc_hidden_proto(lstat) -extern __ptr_t (*__glob_opendir_hook) __P ((const char *directory)); -extern void (*__glob_closedir_hook) __P ((__ptr_t stream)); -extern const char *(*__glob_readdir_hook) __P ((__ptr_t stream)); +extern __ptr_t (*__glob_opendir_hook) (const char *directory) attribute_hidden; +extern void (*__glob_closedir_hook) (__ptr_t stream) attribute_hidden; +extern const char *(*__glob_readdir_hook) (__ptr_t stream) attribute_hidden; -static int glob_in_dir __P ((const char *pattern, const char *directory, - int flags, - int (*errfunc) __P ((const char *, int)), - glob_t *pglob)); -static int prefix_array __P ((const char *prefix, char **array, size_t n, - int add_slash)); -static int collated_compare __P ((const __ptr_t, const __ptr_t)); +extern int __collated_compare (const __ptr_t a, const __ptr_t b) attribute_hidden; +extern int __prefix_array (const char *dirname, char **array, size_t n, int add_slash) attribute_hidden; +libc_hidden_proto(glob_pattern_p) #ifdef __GLOB64 -extern int __glob_pattern_p(const char *pattern, int quote) attribute_hidden; +libc_hidden_proto(glob64) +libc_hidden_proto(globfree64) +libc_hidden_proto(readdir64) +#define __readdir readdir64 #else -extern struct dirent *__readdir (DIR *__dirp) __nonnull ((1)) attribute_hidden; -extern int __glob (__const char *__restrict __pattern, int __flags, - int (*__errfunc) (__const char *, int), - glob_t *__restrict __pglob) __THROW attribute_hidden; -extern void __globfree (glob_t *__pglob) __THROW attribute_hidden; +libc_hidden_proto(glob) +libc_hidden_proto(globfree) +#define __readdir readdir +libc_hidden_proto(readdir) /* Return nonzero if PATTERN contains any metacharacters. Metacharacters can be quoted with backslashes if QUOTE is nonzero. */ -int attribute_hidden __glob_pattern_p(const char *pattern, int quote) +int glob_pattern_p(const char *pattern, int quote) { const char *p; int open = 0; @@ -88,10 +89,208 @@ int attribute_hidden __glob_pattern_p(const char *pattern, int quote) return 0; } -strong_alias(__glob_pattern_p,glob_pattern_p) +libc_hidden_def(glob_pattern_p) + + +/* Do a collated comparison of A and B. */ +int +__collated_compare (const __ptr_t a, const __ptr_t b) +{ + const char *const s1 = *(const char *const *) a; + const char *const s2 = *(const char *const *) b; + + if (s1 == s2) + return 0; + if (s1 == NULL) + return 1; + if (s2 == NULL) + return -1; + return strcoll (s1, s2); +} + + +/* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's + elements in place. Return nonzero if out of memory, zero if successful. + A slash is inserted between DIRNAME and each elt of ARRAY, + unless DIRNAME is just "/". Each old element of ARRAY is freed. + If ADD_SLASH is non-zero, allocate one character more than + necessary, so that a slash can be appended later. */ +int +__prefix_array (const char *dirname, char **array, size_t n, int add_slash) +{ + register size_t i; + size_t dirlen = strlen (dirname); + + if (dirlen == 1 && dirname[0] == '/') + /* DIRNAME is just "/", so normal prepending would get us "//foo". + We want "/foo" instead, so don't prepend any chars from DIRNAME. */ + dirlen = 0; + + for (i = 0; i < n; ++i) + { + size_t eltlen = strlen (array[i]) + 1; + char *new = (char *) malloc (dirlen + 1 + eltlen + (add_slash ? 1 : 0)); + if (new == NULL) + { + while (i > 0) + free ((__ptr_t) array[--i]); + return 1; + } + + memcpy (new, dirname, dirlen); + new[dirlen] = '/'; + memcpy (&new[dirlen + 1], array[i], eltlen); + free ((__ptr_t) array[i]); + array[i] = new; + } + + return 0; +} #endif +/* Like `glob', but PATTERN is a final pathname component, + and matches are searched for in DIRECTORY. + The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done. + The GLOB_APPEND flag is assumed to be set (always appends). */ +static int +glob_in_dir (const char *pattern, const char *directory, int flags, int (*errfunc) (const char *, int), glob_t *pglob) +{ + __ptr_t stream; + + struct globlink + { + struct globlink *next; + char *name; + }; + struct globlink *names = NULL; + size_t nfound = 0; + int meta; + + stream = (__glob_opendir_hook ? (*__glob_opendir_hook) (directory) + : (__ptr_t) opendir (directory)); + if (stream == NULL) + { + if ((errfunc != NULL && (*errfunc) (directory, errno)) || + (flags & GLOB_ERR)) + return GLOB_ABORTED; + } + + meta = glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)); + + if (meta) + flags |= GLOB_MAGCHAR; + + while (1) + { + const char *name; + size_t len; + + if (__glob_readdir_hook) + { + name = (*__glob_readdir_hook) (stream); + if (name == NULL) + break; + len = 0; + } + else + { + struct dirent *d = __readdir ((DIR *) stream); + if (d == NULL) + break; + if (! (d->d_ino != 0)) + continue; + name = d->d_name; +#ifdef _DIRENT_HAVE_D_NAMLEN + len = d->d_namlen; +#else + len = 0; +#endif + } + + if ((!meta && strcmp (pattern, name) == 0) + || fnmatch (pattern, name, + (!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) | + ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)) == 0) + { + struct globlink *new + = (struct globlink *) alloca (sizeof (struct globlink)); + if (len == 0) + len = strlen (name); + new->name + = (char *) malloc (len + ((flags & GLOB_MARK) ? 1 : 0) + 1); + if (new->name == NULL) + goto memory_error; + memcpy ((__ptr_t) new->name, name, len); + new->name[len] = '\0'; + new->next = names; + names = new; + ++nfound; + if (!meta) + break; + } + } + + if (nfound == 0 && (flags & GLOB_NOCHECK)) + { + size_t len = strlen (pattern); + nfound = 1; + names = (struct globlink *) alloca (sizeof (struct globlink)); + names->next = NULL; + names->name = (char *) malloc (len + (flags & GLOB_MARK ? 1 : 0) + 1); + if (names->name == NULL) + goto memory_error; + memcpy (names->name, pattern, len); + names->name[len] = '\0'; + } + + pglob->gl_pathv + = (char **) realloc (pglob->gl_pathv, + (pglob->gl_pathc + + ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) + + nfound + 1) * + sizeof (char *)); + if (pglob->gl_pathv == NULL) + goto memory_error; + + if (flags & GLOB_DOOFFS) + while (pglob->gl_pathc < pglob->gl_offs) + pglob->gl_pathv[pglob->gl_pathc++] = NULL; + + for (; names != NULL; names = names->next) + pglob->gl_pathv[pglob->gl_pathc++] = names->name; + pglob->gl_pathv[pglob->gl_pathc] = NULL; + + pglob->gl_flags = flags; + + { + int save = errno; + if (__glob_closedir_hook) + (*__glob_closedir_hook) (stream); + else + (void) closedir ((DIR *) stream); + errno = save; + } + return nfound == 0 ? GLOB_NOMATCH : 0; + + memory_error: + { + int save = errno; + if (__glob_closedir_hook) + (*__glob_closedir_hook) (stream); + else + (void) closedir ((DIR *) stream); + errno = save; + } + while (names != NULL) + { + if (names->name != NULL) + free ((__ptr_t) names->name); + names = names->next; + } + return GLOB_NOSPACE; +} + /* Do glob searching for PATTERN, placing results in PGLOB. The bits defined above may be set in FLAGS. If a directory cannot be opened or read and ERRFUNC is not nil, @@ -100,11 +299,11 @@ strong_alias(__glob_pattern_p,glob_pattern_p) `glob' returns GLOB_ABEND; if it returns zero, the error is ignored. If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned. Otherwise, `glob' returns zero. */ -int attribute_hidden -__glob (pattern, flags, errfunc, pglob) +int +glob (pattern, flags, errfunc, pglob) const char *pattern; int flags; - int (*errfunc) __P ((const char *, int)); + int (*errfunc) (const char *, int); glob_t *pglob; { const char *filename; @@ -138,7 +337,7 @@ __glob (pattern, flags, errfunc, pglob) { dirlen = filename - pattern; dirname = (char *) alloca (dirlen + 1); - __memcpy (dirname, pattern, dirlen); + memcpy (dirname, pattern, dirlen); dirname[dirlen] = '\0'; ++filename; } @@ -146,7 +345,7 @@ __glob (pattern, flags, errfunc, pglob) if (filename[0] == '\0' && dirlen > 1) /* "pattern/". Expand "pattern", appending slashes. */ { - int val = __glob (dirname, flags | GLOB_MARK, errfunc, pglob); + int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob); if (val == 0) pglob->gl_flags = (pglob->gl_flags & ~GLOB_MARK) | (flags & GLOB_MARK); return val; @@ -160,7 +359,7 @@ __glob (pattern, flags, errfunc, pglob) oldcount = pglob->gl_pathc; - if (__glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE))) + if (glob_pattern_p (dirname, !(flags & GLOB_NOESCAPE))) { /* The directory name contains metacharacters, so we have to glob for the directory, and then glob for @@ -168,7 +367,7 @@ __glob (pattern, flags, errfunc, pglob) glob_t dirs; register int i; - status = __glob (dirname, + status = glob (dirname, ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE)) | GLOB_NOSORT), errfunc, &dirs); @@ -180,8 +379,6 @@ __glob (pattern, flags, errfunc, pglob) appending the results to PGLOB. */ for (i = 0; i < dirs.gl_pathc; ++i) { - int oldcount; - #ifdef SHELL { /* Make globbing interruptible in the bash shell. */ @@ -189,8 +386,8 @@ __glob (pattern, flags, errfunc, pglob) if (interrupt_state) { - __globfree (&dirs); - __globfree (&files); + globfree (&dirs); + globfree (&files); return GLOB_ABEND; } } @@ -206,19 +403,19 @@ __glob (pattern, flags, errfunc, pglob) if (status != 0) { - __globfree (&dirs); - __globfree (pglob); + globfree (&dirs); + globfree (pglob); return status; } /* Stick the directory on the front of each name. */ - if (prefix_array (dirs.gl_pathv[i], + if (__prefix_array (dirs.gl_pathv[i], &pglob->gl_pathv[oldcount], pglob->gl_pathc - oldcount, flags & GLOB_MARK)) { - __globfree (&dirs); - __globfree (pglob); + globfree (&dirs); + globfree (pglob); return GLOB_NOSPACE; } } @@ -230,11 +427,11 @@ __glob (pattern, flags, errfunc, pglob) /* No matches. */ if (flags & GLOB_NOCHECK) { - size_t len = __strlen (pattern) + 1; + size_t len = strlen (pattern) + 1; char *patcopy = (char *) malloc (len); if (patcopy == NULL) return GLOB_NOSPACE; - __memcpy (patcopy, pattern, len); + memcpy (patcopy, pattern, len); pglob->gl_pathv = (char **) realloc (pglob->gl_pathv, @@ -272,12 +469,12 @@ __glob (pattern, flags, errfunc, pglob) if (dirlen > 0) { /* Stick the directory on the front of each name. */ - if (prefix_array (dirname, + if (__prefix_array (dirname, &pglob->gl_pathv[oldcount], pglob->gl_pathc - oldcount, flags & GLOB_MARK)) { - __globfree (pglob); + globfree (pglob); return GLOB_NOSPACE; } } @@ -290,29 +487,29 @@ __glob (pattern, flags, errfunc, pglob) int i; struct stat st; for (i = oldcount; i < pglob->gl_pathc; ++i) - if (__lstat (pglob->gl_pathv[i], &st) == 0 && + if (lstat (pglob->gl_pathv[i], &st) == 0 && S_ISDIR (st.st_mode)) - __strcat (pglob->gl_pathv[i], "/"); + strcat (pglob->gl_pathv[i], "/"); } if (!(flags & GLOB_NOSORT)) /* Sort the vector. */ qsort ((__ptr_t) &pglob->gl_pathv[oldcount], pglob->gl_pathc - oldcount, - sizeof (char *), (__compar_fn_t)collated_compare); + sizeof (char *), (__compar_fn_t)__collated_compare); return 0; } #ifdef __GLOB64 -strong_alias(__glob64,glob64) +libc_hidden_def(glob64) #else -strong_alias(__glob,glob) +libc_hidden_def(glob) #endif /* Free storage allocated in PGLOB by a previous `glob' call. */ -void attribute_hidden -__globfree (pglob) +void +globfree (pglob) register glob_t *pglob; { if (pglob->gl_pathv != NULL) @@ -325,218 +522,7 @@ __globfree (pglob) } } #ifdef __GLOB64 -strong_alias(__globfree64,globfree64) +libc_hidden_def(globfree64) #else -strong_alias(__globfree,globfree) +libc_hidden_def(globfree) #endif - - -/* Do a collated comparison of A and B. */ -static int -collated_compare (a, b) - const __ptr_t a; - const __ptr_t b; -{ - const char *const s1 = *(const char *const *) a; - const char *const s2 = *(const char *const *) b; - - if (s1 == s2) - return 0; - if (s1 == NULL) - return 1; - if (s2 == NULL) - return -1; - return strcoll (s1, s2); -} - - -/* Prepend DIRNAME to each of N members of ARRAY, replacing ARRAY's - elements in place. Return nonzero if out of memory, zero if successful. - A slash is inserted between DIRNAME and each elt of ARRAY, - unless DIRNAME is just "/". Each old element of ARRAY is freed. - If ADD_SLASH is non-zero, allocate one character more than - necessary, so that a slash can be appended later. */ -static int -prefix_array (dirname, array, n, add_slash) - const char *dirname; - char **array; - size_t n; - int add_slash; -{ - register size_t i; - size_t dirlen = __strlen (dirname); - - if (dirlen == 1 && dirname[0] == '/') - /* DIRNAME is just "/", so normal prepending would get us "//foo". - We want "/foo" instead, so don't prepend any chars from DIRNAME. */ - dirlen = 0; - - for (i = 0; i < n; ++i) - { - size_t eltlen = __strlen (array[i]) + 1; - char *new = (char *) malloc (dirlen + 1 + eltlen + (add_slash ? 1 : 0)); - if (new == NULL) - { - while (i > 0) - free ((__ptr_t) array[--i]); - return 1; - } - - __memcpy (new, dirname, dirlen); - new[dirlen] = '/'; - __memcpy (&new[dirlen + 1], array[i], eltlen); - free ((__ptr_t) array[i]); - array[i] = new; - } - - return 0; -} - - -/* Like `glob', but PATTERN is a final pathname component, - and matches are searched for in DIRECTORY. - The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done. - The GLOB_APPEND flag is assumed to be set (always appends). */ -static int -glob_in_dir (pattern, directory, flags, errfunc, pglob) - const char *pattern; - const char *directory; - int flags; - int (*errfunc) __P ((const char *, int)); - glob_t *pglob; -{ - __ptr_t stream; - - struct globlink - { - struct globlink *next; - char *name; - }; - struct globlink *names = NULL; - size_t nfound = 0; - int meta; - - stream = (__glob_opendir_hook ? (*__glob_opendir_hook) (directory) - : (__ptr_t) __opendir (directory)); - if (stream == NULL) - { - if ((errfunc != NULL && (*errfunc) (directory, errno)) || - (flags & GLOB_ERR)) - return GLOB_ABORTED; - } - - meta = __glob_pattern_p (pattern, !(flags & GLOB_NOESCAPE)); - - if (meta) - flags |= GLOB_MAGCHAR; - - while (1) - { - const char *name; - size_t len; - - if (__glob_readdir_hook) - { - name = (*__glob_readdir_hook) (stream); - if (name == NULL) - break; - len = 0; - } - else - { - struct dirent *d = __readdir ((DIR *) stream); - if (d == NULL) - break; - if (! (d->d_ino != 0)) - continue; - name = d->d_name; -#ifdef _DIRENT_HAVE_D_NAMLEN - len = d->d_namlen; -#else - len = 0; -#endif - } - - if ((!meta && __strcmp (pattern, name) == 0) - || fnmatch (pattern, name, - (!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) | - ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)) == 0) - { - struct globlink *new - = (struct globlink *) alloca (sizeof (struct globlink)); - if (len == 0) - len = __strlen (name); - new->name - = (char *) malloc (len + ((flags & GLOB_MARK) ? 1 : 0) + 1); - if (new->name == NULL) - goto memory_error; - __memcpy ((__ptr_t) new->name, name, len); - new->name[len] = '\0'; - new->next = names; - names = new; - ++nfound; - if (!meta) - break; - } - } - - if (nfound == 0 && (flags & GLOB_NOCHECK)) - { - size_t len = __strlen (pattern); - nfound = 1; - names = (struct globlink *) alloca (sizeof (struct globlink)); - names->next = NULL; - names->name = (char *) malloc (len + (flags & GLOB_MARK ? 1 : 0) + 1); - if (names->name == NULL) - goto memory_error; - __memcpy (names->name, pattern, len); - names->name[len] = '\0'; - } - - pglob->gl_pathv - = (char **) realloc (pglob->gl_pathv, - (pglob->gl_pathc + - ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) + - nfound + 1) * - sizeof (char *)); - if (pglob->gl_pathv == NULL) - goto memory_error; - - if (flags & GLOB_DOOFFS) - while (pglob->gl_pathc < pglob->gl_offs) - pglob->gl_pathv[pglob->gl_pathc++] = NULL; - - for (; names != NULL; names = names->next) - pglob->gl_pathv[pglob->gl_pathc++] = names->name; - pglob->gl_pathv[pglob->gl_pathc] = NULL; - - pglob->gl_flags = flags; - - { - int save = errno; - if (__glob_closedir_hook) - (*__glob_closedir_hook) (stream); - else - (void) __closedir ((DIR *) stream); - errno = save; - } - return nfound == 0 ? GLOB_NOMATCH : 0; - - memory_error: - { - int save = errno; - if (__glob_closedir_hook) - (*__glob_closedir_hook) (stream); - else - (void) __closedir ((DIR *) stream); - errno = save; - } - while (names != NULL) - { - if (names->name != NULL) - free ((__ptr_t) names->name); - names = names->next; - } - return GLOB_NOSPACE; -} - diff --git a/libc/misc/glob/glob64-susv3.c b/libc/misc/glob/glob64-susv3.c new file mode 100644 index 000000000..cc633cd88 --- /dev/null +++ b/libc/misc/glob/glob64-susv3.c @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2006 Rich Felker <dalias@aerifal.cx> + * + * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. + */ + +#include <_lfs_64.h> + +#include <dirent.h> +#include <glob.h> +#include <sys/stat.h> + +#define glob_t glob64_t +#define glob(pattern, flags, errfunc, pglob) \ + glob64 (pattern, flags, errfunc, pglob) +#define globfree(pglob) globfree64 (pglob) + +#define __GLOB64 1 + +#include "glob-susv3.c" diff --git a/libc/misc/glob/glob64.c b/libc/misc/glob/glob64.c index f0c65abe0..d84f874d5 100644 --- a/libc/misc/glob/glob64.c +++ b/libc/misc/glob/glob64.c @@ -1,47 +1,26 @@ -#include <features.h> +/* + * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org> + * + * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. + */ -#ifdef __UCLIBC_HAS_LFS__ +#include <_lfs_64.h> -#if defined _FILE_OFFSET_BITS && _FILE_OFFSET_BITS != 64 -#undef _FILE_OFFSET_BITS -#define _FILE_OFFSET_BITS 64 -#endif -#ifndef __USE_LARGEFILE64 -# define __USE_LARGEFILE64 1 -#endif -/* We absolutely do _NOT_ want interfaces silently - * renamed under us or very bad things will happen... */ -#ifdef __USE_FILE_OFFSET64 -# undef __USE_FILE_OFFSET64 -#endif #include <dirent.h> #include <glob.h> #include <sys/stat.h> -extern struct dirent64 *__readdir64 (DIR *__dirp) __nonnull ((1)) attribute_hidden; -extern int __glob64 (__const char *__restrict __pattern, int __flags, - int (*__errfunc) (__const char *, int), - glob64_t *__restrict __pglob) __THROW attribute_hidden; -extern void __globfree (glob_t *__pglob) __THROW attribute_hidden; -extern void __globfree64 (glob64_t *__pglob) __THROW attribute_hidden; - #define dirent dirent64 -#define __readdir(dirp) __readdir64(dirp) #define glob_t glob64_t -#define __glob(pattern, flags, errfunc, pglob) \ - __glob64 (pattern, flags, errfunc, pglob) #define glob(pattern, flags, errfunc, pglob) \ glob64 (pattern, flags, errfunc, pglob) -#define __globfree(pglob) __globfree64 (pglob) #define globfree(pglob) globfree64 (pglob) #undef stat #define stat stat64 -#define __lstat __lstat64 +#define lstat lstat64 #define __GLOB64 1 #include "glob.c" - -#endif |