diff options
Diffstat (limited to 'navit/support')
-rw-r--r-- | navit/support/wordexp/glob.c | 89 | ||||
-rw-r--r-- | navit/support/wordexp/glob.h | 4 | ||||
-rw-r--r-- | navit/support/wordexp/wordexp.c | 6 |
3 files changed, 90 insertions, 9 deletions
diff --git a/navit/support/wordexp/glob.c b/navit/support/wordexp/glob.c index 08a3c999f..6cbf5e5e3 100644 --- a/navit/support/wordexp/glob.c +++ b/navit/support/wordexp/glob.c @@ -93,6 +93,94 @@ int glob(const char *pattern, int flags, FindClose(hFiles); return 0; } +#else + +#include <dirent.h> +#include <string.h> +#include <fnmatch.h> +#include "debug.h" +#include "glob.h" + +static int +glob_requires_match(const char *pattern, int flags) +{ + for (;;) { + switch (*pattern++) { + case '\0': + return 0; + case '?': + case '*': + case '[': + return 1; + case '\\': + if (!*pattern++) + return 0; + } + } + return 0; +} + +static int +glob_recursive(const char *path1, const char *path2, const char *pattern, int flags, int (*errfunc) (const char *epath, int eerrno), glob_t *pglob) +{ + const char *next; + char *fname,*path=malloc(strlen(path1)+strlen(path2)+2); + int flen; + strcpy(path, path1); + if (path1[0] && path2[0] && (path1[1] != '\0' || path1[0] != '/')) + strcat(path, "/"); + strcat(path, path2); + if (!strlen(pattern)) { + dbg(0,"found %s\n",path); + pglob->gl_pathv=realloc(pglob->gl_pathv, (pglob->gl_pathc+1)*sizeof(char *)); + if (!pglob->gl_pathv) { + pglob->gl_pathc=0; + return GLOB_NOSPACE; + } + pglob->gl_pathv[pglob->gl_pathc++]=path; + return 0; + } + dbg(0,"searching for %s in %s\n",pattern,path); + flen=strcspn(pattern,"/"); + next=pattern+flen; + if (*next == '/') + next++; + fname=malloc(flen+1); + strncpy(fname, pattern, flen); + fname[flen]='\0'; + if (glob_requires_match(fname, 0)) { + DIR *dh; + struct dirent *de; + dbg(0,"in dir %s search for %s\n",path,fname); + dh=opendir(path); + if (dh) { + while ((de=readdir(dh))) { + if (fnmatch(fname,de->d_name,0) == 0) { + glob_recursive(path, de->d_name, next, flags, errfunc, pglob); + } + } + closedir(dh); + } + } else { + glob_recursive(path, fname, next, flags, errfunc, pglob); + } + free(fname); + free(path); + return 0; +} + +int +glob(const char *pattern, int flags, int (*errfunc) (const char *epath, int eerrno), glob_t *pglob) +{ + pglob->gl_pathc=0; + pglob->gl_pathv=NULL; + if (pattern[0] == '/') + return glob_recursive("/", "", pattern+1, flags, errfunc, pglob); + else + return glob_recursive("", "", pattern, flags, errfunc, pglob); +} + +#endif /* _WIN32 || _WIN32_WCE */ void globfree(glob_t *pglob) { @@ -106,5 +194,4 @@ void globfree(glob_t *pglob) pglob->gl_pathc = 0; } -#endif /* _WIN32 || _WIN32_WCE */ #endif /* HAVE_GLOB */ diff --git a/navit/support/wordexp/glob.h b/navit/support/wordexp/glob.h index 17f43eb2b..d36ac29e6 100644 --- a/navit/support/wordexp/glob.h +++ b/navit/support/wordexp/glob.h @@ -2,7 +2,6 @@ #define _GLOB_H_ #ifndef HAVE_GLOB -#if defined __MINGW32__ || defined _MSC_VER typedef struct { size_t gl_pathc; /* count of file names */ @@ -13,7 +12,8 @@ typedef struct { int glob(const char *pattern, int flags, int (*errfunc)(const char *epath, int eerrno), glob_t *pglob); void globfree(glob_t *pglob); -#endif /* defined __MINGW32__ || defined _MSC_VER */ +#define GLOB_NOSPACE 1 + #endif #endif /* _GLOB_H_ */ diff --git a/navit/support/wordexp/wordexp.c b/navit/support/wordexp/wordexp.c index 5e4f11992..d2ed3ff28 100644 --- a/navit/support/wordexp/wordexp.c +++ b/navit/support/wordexp/wordexp.c @@ -87,16 +87,13 @@ wordexp(const char *words, wordexp_t *we, int flags) int i; int error = 0; char *words_expanded; -#ifdef HAVE_API_WIN32_BASE glob_t pglob; -#endif assert(we != NULL); assert(words != NULL); /* expansion of ´$NAME´ or ´${NAME}´ */ words_expanded=expand_variables(words); -#ifdef HAVE_API_WIN32_BASE /* expansion of ´*´, ´?´ */ error=glob(words_expanded, 0, NULL, &pglob); if (!error) @@ -114,13 +111,10 @@ wordexp(const char *words, wordexp_t *we, int flags) } else { -#endif we->we_wordc = 1; we->we_wordv = malloc(sizeof(char*)); we->we_wordv[0] = words_expanded; -#ifdef HAVE_API_WIN32_BASE } -#endif return error; |