From 7f2ac73888b8ef372ea597049981b27e7d810ac2 Mon Sep 17 00:00:00 2001 From: Mystery Man Date: Wed, 20 Aug 2003 07:37:27 +0000 Subject: This commit was manufactured by cvs2svn to create branch 'MDKC_1_0'. --- mdk-stage1/dietlibc/libshell/basename.c | 27 +++++++++++ mdk-stage1/dietlibc/libshell/dirname.c | 25 ++++++++++ mdk-stage1/dietlibc/libshell/fnmatch.c | 34 +++++++------ mdk-stage1/dietlibc/libshell/glob.c | 84 ++++++++++++++++++--------------- mdk-stage1/dietlibc/libshell/realpath.c | 18 +++---- 5 files changed, 125 insertions(+), 63 deletions(-) create mode 100644 mdk-stage1/dietlibc/libshell/basename.c create mode 100644 mdk-stage1/dietlibc/libshell/dirname.c (limited to 'mdk-stage1/dietlibc/libshell') diff --git a/mdk-stage1/dietlibc/libshell/basename.c b/mdk-stage1/dietlibc/libshell/basename.c new file mode 100644 index 000000000..fbf5b2478 --- /dev/null +++ b/mdk-stage1/dietlibc/libshell/basename.c @@ -0,0 +1,27 @@ +#include +#include + +/* + path dirname basename + "/usr/lib" "/usr" "lib" + "/usr/" "/" "usr" + "usr" "." "usr" + "/" "/" "/" + "." "." "." + ".." "." ".." +*/ + +char *basename(char *path) { + char *c; +again: + if (!(c=strrchr(path,'/'))) return path; + if (c[1]==0) { + if (c == path) + return c; + else { + *c=0; + goto again; + } + } + return c+1; +} diff --git a/mdk-stage1/dietlibc/libshell/dirname.c b/mdk-stage1/dietlibc/libshell/dirname.c new file mode 100644 index 000000000..d35e6ea62 --- /dev/null +++ b/mdk-stage1/dietlibc/libshell/dirname.c @@ -0,0 +1,25 @@ +#include +#include + +/* + path dirname basename + "/usr/lib" "/usr" "lib" + "/usr/" "/" "usr" + "usr" "." "usr" + "/" "/" "/" + "." "." "." + ".." "." ".." +*/ + +char *dirname(char *path) { + char *c; +again: + if (!(c=strrchr(path,'/'))) return "."; + while (c[1]==0) { /* remove trailing slashes */ + if (c==path) return c; /* unless path=='/' */ + *c=0; + if (*--c!='/') break; + } + if (*c=='/') { if (c!=path) *c=0; return path; } + goto again; +} diff --git a/mdk-stage1/dietlibc/libshell/fnmatch.c b/mdk-stage1/dietlibc/libshell/fnmatch.c index 4e7664510..ef43735f6 100644 --- a/mdk-stage1/dietlibc/libshell/fnmatch.c +++ b/mdk-stage1/dietlibc/libshell/fnmatch.c @@ -1,5 +1,6 @@ #include #include +#include #define NOTFIRST 128 @@ -11,6 +12,7 @@ static int match(char c,char d,int flags) { } int fnmatch(const char *pattern, const char *string, int flags) { + /*printf("fnmatch(\"%s\",\"%s\")\n",pattern,string);*/ if (*string==0) { while (*pattern=='*') ++pattern; return (!!*pattern); @@ -18,31 +20,36 @@ int fnmatch(const char *pattern, const char *string, int flags) { if (*string=='.' && *pattern!='.' && (flags&FNM_PERIOD)) { /* don't match if FNM_PERIOD and this is the first char */ if ((flags&FNM_PERIOD) && (!(flags&NOTFIRST))) - return 1; + return FNM_NOMATCH; /* don't match if FNM_PERIOD and FNM_FILE_NAME and previous was '/' */ if ((flags&(FNM_FILE_NAME|FNM_PERIOD)) && string[-1]=='/') - return 1; + return FNM_NOMATCH; } flags|=NOTFIRST; switch (*pattern) { case '[': - ++pattern; - while (*pattern && *pattern!=']') { - if (flags&FNM_PATHNAME || *string!='/') { + { + int neg=0; + ++pattern; + if (*string=='/' && flags&FNM_PATHNAME) return FNM_NOMATCH; + if (*pattern=='^') { neg=1; ++pattern; } + while (*pattern && *pattern!=']') { int res=0; if (pattern[1]=='-') { if (*string>=*pattern && *string<=pattern[2]) res=1; if (flags&FNM_CASEFOLD) { if (tolower(*string)>=tolower(*pattern) && tolower(*string)<=tolower(pattern[2])) res=1; } - } else + pattern+=3; + } else { res=match(*pattern,*string,flags); - if (res) { + ++pattern; + } + if (res ^ neg) { while (*pattern && *pattern!=']') ++pattern; return fnmatch(pattern+1,string+1,flags); } } - ++pattern; } break; case '\\': @@ -55,12 +62,9 @@ int fnmatch(const char *pattern, const char *string, int flags) { } break; case '*': - if (!pattern[1] || fnmatch(pattern+1,string,flags)==0) - return 0; - else - if (*string!='/') - return fnmatch(pattern,string+1,flags); - break; + if ((*string=='/' && flags&FNM_PATHNAME) || fnmatch(pattern,string+1,flags)) + return fnmatch(pattern+1,string,flags); + return 0; case 0: if (*string==0 || (*string=='/' && (flags&FNM_LEADING_DIR))) return 0; @@ -70,5 +74,5 @@ int fnmatch(const char *pattern, const char *string, int flags) { return fnmatch(pattern+1,string+1,flags); break; } - return 1; + return FNM_NOMATCH; } diff --git a/mdk-stage1/dietlibc/libshell/glob.c b/mdk-stage1/dietlibc/libshell/glob.c index 34d09d87a..def55f8dc 100644 --- a/mdk-stage1/dietlibc/libshell/glob.c +++ b/mdk-stage1/dietlibc/libshell/glob.c @@ -24,6 +24,7 @@ #include #include #include +#include "dietfeatures.h" #include @@ -38,7 +39,7 @@ static int cmp_func(const void * a, const void * b) return 1; if (s2 == NULL) return -1; - return strcmp(s1, s2); + return strcoll(s1, s2); } @@ -47,6 +48,38 @@ static int cmp_func(const void * a, const void * b) The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done. The GLOB_APPEND flag is assumed to be set (always appends). Prepends DIRECTORY in constructed PGLOB. */ +static void close_dir_keep_errno(DIR* dp) { + int save = errno; + if (dp) + closedir (dp); + errno=save; +} + +static int add_entry(const char* name,glob_t *pglob,int* nfound) { + pglob->gl_pathv = (char **) realloc(pglob->gl_pathv, + (pglob->gl_pathc + pglob->gl_offs + 2) + * sizeof (char *)); + if (pglob->gl_pathv == NULL) + return 1; + pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = strdup(name); + pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc + 1] = NULL; + pglob->gl_pathc++; + (*nfound)++; + return 0; +} + +static void build_fullname(char * fullname, const char * directory, const char * filename) { + char *dest=fullname; + if (directory[0]=='/' && !directory[1]) { + *dest='/'; ++dest; + } else if (directory[0]!='.' || directory[1]) { + strcpy(dest,directory); + dest=strchr(dest,0); + *dest='/'; ++dest; + } + strcpy(dest,filename); +} + static int glob_in_dir(const char *pattern, const char *directory, int flags, int errfunc(const char * epath, int eerrno), glob_t *pglob) @@ -57,33 +90,6 @@ static int glob_in_dir(const char *pattern, const char *directory, int flags, int i; char * ptr; - void close_dir_keep_errno(void) { - int save = errno; - if (dp) - closedir (dp); - __set_errno(save); - } - int add_entry(const char * name) { - pglob->gl_pathv = (char **) realloc(pglob->gl_pathv, - (pglob->gl_pathc + pglob->gl_offs + 2) - * sizeof (char *)); - if (pglob->gl_pathv == NULL) - return 1; - pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = strdup(name); - pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc + 1] = NULL; - pglob->gl_pathc++; - nfound++; - return 0; - } - void build_fullname(char * fullname, int fullnamesize, const char * directory, const char * filename) { - if (!strcmp(directory, "/")) - snprintf(fullname, fullnamesize, "/%s", filename); - else if (!strcmp(directory, ".")) - snprintf(fullname, fullnamesize, "%s", filename); - else - snprintf(fullname, fullnamesize, "%s/%s", directory, filename); - } - if (!dp) { if (errno != ENOTDIR && ((errfunc != NULL && (*errfunc) (directory, errno)) @@ -96,19 +102,19 @@ static int glob_in_dir(const char *pattern, const char *directory, int flags, while ((ep = readdir(dp))) { i = strlen(directory) + strlen(ep->d_name) + 2; ptr = (char *) alloca(i); - build_fullname(ptr, i, directory, ep->d_name); + build_fullname(ptr, directory, ep->d_name); if (flags & GLOB_ONLYDIR) { struct stat statr; if (stat(ptr, &statr) || !S_ISDIR(statr.st_mode)) continue; } if (fnmatch(pattern, ep->d_name, fnm_flags) == 0) - if (add_entry(ptr)) + if (add_entry(ptr,pglob,&nfound)) goto memory_error; } } - close_dir_keep_errno(); + close_dir_keep_errno(dp); if (nfound != 0) pglob->gl_flags = flags; @@ -116,8 +122,8 @@ static int glob_in_dir(const char *pattern, const char *directory, int flags, /* nfound == 0 */ i = strlen(directory) + strlen(pattern) + 2; ptr = (char *) alloca(i); - build_fullname(ptr, i, directory, pattern); - if (add_entry(ptr)) + build_fullname(ptr, directory, pattern); + if (add_entry(ptr,pglob,&nfound)) goto memory_error; } @@ -127,7 +133,7 @@ static int glob_in_dir(const char *pattern, const char *directory, int flags, /* We're in trouble since we can't free the already allocated memory. [allocated from strdup(filame)] * Well, after all, when malloc returns NULL we're already in a bad mood, and no doubt the * program will manage to segfault by itself very soon :-). */ - close_dir_keep_errno(); + close_dir_keep_errno(dp); return GLOB_NOSPACE; } @@ -146,7 +152,7 @@ int glob(const char *pattern, int flags, int errfunc(const char * epath, int eer char * ptr, * ptr2; if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0) { - __set_errno (EINVAL); + errno=EINVAL; return -1; } @@ -196,10 +202,10 @@ int glob(const char *pattern, int flags, int errfunc(const char * epath, int eer if (filename == NULL) { /* We have no '/' in the pattern */ filename = pattern_; - dirname = "."; + dirname = (char*)"."; } else if (filename == pattern_) { /* "/pattern". */ - dirname = "/"; + dirname = (char*)"/"; filename++; } else { dirname = pattern_; @@ -293,13 +299,13 @@ int glob(const char *pattern, int flags, int errfunc(const char * epath, int eer } /* okay now we add the new entry */ - k = strlen(dirs.gl_pathv[i]) + 1 + strlen(filename) + 1; + k = strlen(dirs.gl_pathv[i]) + strlen(filename) + 2; if ((pglob->gl_pathv[j] = malloc(k)) == NULL) { globfree(&dirs); globfree(pglob); return GLOB_NOSPACE; } - snprintf(pglob->gl_pathv[j], k, "%s/%s", dirs.gl_pathv[i], filename); + build_fullname(pglob->gl_pathv[j], dirs.gl_pathv[i], filename); pglob->gl_pathc++; pglob->gl_pathv[j+1] = NULL; } diff --git a/mdk-stage1/dietlibc/libshell/realpath.c b/mdk-stage1/dietlibc/libshell/realpath.c index 1377018e5..9aee2c585 100644 --- a/mdk-stage1/dietlibc/libshell/realpath.c +++ b/mdk-stage1/dietlibc/libshell/realpath.c @@ -5,21 +5,21 @@ #include #include "dietfeatures.h" -char *realpath(const char *path, char *resolved_path) { +char* realpath(const char *path, char *resolved_path) { int fd=open(".",O_RDONLY); - char *tmp=""; + char* tmp=(char*)""; + if (fd<0) return 0; if (chdir(path)) { -#ifdef WANT_THREAD_SAFE - if (*__errno_location()==ENOTDIR) -#else - if (errno==ENOTDIR) -#endif - { - if ((tmp=strrchr(path,'/'))) { + if (errno==ENOTDIR) { + char* match; + if ((match=strrchr(path,'/'))) { + tmp=match; memmove(resolved_path,path,tmp-path); resolved_path[tmp-path]=0; if (chdir(resolved_path)) { resolved_path=0; goto abort; } } + } else { + resolved_path=0; goto abort; } } if (!getcwd(resolved_path,PATH_MAX)) { resolved_path=0; goto abort; } -- cgit v1.2.1