/** * Return path(s) from a glob pattern. * @param con context * @param pattern glob pattern * @retval *acp no. of paths * @retval *avp array of paths * @return 0 on success */ static int poptGlob(/*@unused@*/ UNUSED(poptContext con), const char * pattern, /*@out@*/ int * acp, /*@out@*/ const char *** avp) /*@modifies *acp, *avp @*/ { const char * pat = pattern; int rc = 0; /* assume success */ /* XXX skip the attention marker. */ if (pat[0] == '@' && pat[1] != '(') pat++; #if defined(HAVE_GLOB_H) if (glob_pattern_p(pat, 0)) { glob_t _g, *pglob = &_g; if (!glob(pat, poptGlobFlags, poptGlob_error, pglob)) { if (acp) { *acp = (int) pglob->gl_pathc; pglob->gl_pathc = 0; } if (avp) { /*@-onlytrans@*/ *avp = (const char **) pglob->gl_pathv; /*@=onlytrans@*/ pglob->gl_pathv = NULL; } /*@-nullstate@*/ globfree(pglob); /*@=nullstate@*/ } else rc = POPT_ERROR_ERRNO; } else #endif /* HAVE_GLOB_H */ { if (acp) *acp = 1; if (avp && (*avp = calloc((size_t)(1 + 1), sizeof (**avp))) != NULL) (*avp)[0] = xstrdup(pat); } return rc; }
/** * Check for application match. * @param con context * @param s config application name * return 0 if config application matches */ static int configAppMatch(poptContext con, const char * s) /*@*/ { int rc = 1; if (con->appName == NULL) /* XXX can't happen. */ return rc; #if defined(HAVE_GLOB_H) && defined(HAVE_FNMATCH_H) if (glob_pattern_p(s, 1)) { /*@-bitwisesigned@*/ static int flags = FNM_PATHNAME | FNM_PERIOD; #ifdef FNM_EXTMATCH flags |= FNM_EXTMATCH; #endif /*@=bitwisesigned@*/ rc = fnmatch(s, con->appName, flags); } else #endif rc = strcmp(s, con->appName); return rc; }
/* 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, errfunc_t errfunc, glob_t *pglob) { #ifdef WIN32 HANDLE stream; WIN32_FIND_DATA findinfo; int first = 1; #else void *stream; #endif struct globlink { struct globlink *next; char *name; }; struct globlink *names = NULL; size_t nfound = 0; DEBUGOUT(_LtDebug(__FILE__, NULL, "glob_in_dir(%s, %s, %i,)\n", pattern, directory, flags)); if (!glob_pattern_p(pattern, !(flags & GLOB_NOESCAPE))) { stream = NULL; flags |= GLOB_NOCHECK; } else { flags |= GLOB_MAGCHAR; #ifdef WIN32 stream = FindFirstFile(directory, &findinfo); if (stream == INVALID_HANDLE_VALUE) stream = NULL; #else stream = (__glob_opendir_hook ? (*__glob_opendir_hook) (directory) : (void *) opendir(directory)); #endif if (stream == NULL) { if ((errfunc != NULL && (*errfunc) (directory, errno)) || (flags & GLOB_ERR)) return GLOB_ABEND; } else while (1) { const char *name; size_t len; #ifdef WIN32 if (first) { /* The FindFirstFile call actaully reads the first one */ first = 0; } else { if (!FindNextFile(stream, &findinfo)) break; } name = findinfo.cFileName; len = 0; #else 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; DEBUGOUT(_LtDebug0(__FILE__, NULL, "glob_in_dir(%s)\n", d->d_name)); if (!REAL_DIR_ENTRY(d)) { DEBUGOUT(_LtDebug0(__FILE__, NULL, "glob_in_dir() continue\n")); continue; } name = d->d_name; #ifdef HAVE_D_NAMLEN len = d->d_namlen; #else len = 0; #endif } #endif if (_Lesstif_fnmatch(pattern, name, (!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)) == 0) { struct globlink *new_w = (struct globlink *)XtMalloc(sizeof(struct globlink)); if (len == 0) len = strlen(name); new_w->name = (char *)XtMalloc(len + ((flags & GLOB_MARK) ? 1 : 0) + 1); if (new_w->name == NULL) goto memory_error; memcpy((void *) new_w->name, name, len); new_w->name[len] = '\0'; new_w->next = names; names = new_w; ++nfound; } else { DEBUGOUT(_LtDebug0(__FILE__, NULL, "glob_in_dir() failed fnmatch\n")); } } } DEBUGOUT(_LtDebug0(__FILE__, NULL, "glob_in_dir() found %d\n", nfound)); if (nfound == 0 && (flags & GLOB_NOCHECK)) { size_t len = strlen(pattern); nfound = 1; names = (struct globlink *)XtMalloc(sizeof(struct globlink)); names->next = NULL; names->name = (char *)XtMalloc(len + 1); if (names->name == NULL) goto memory_error; memcpy(names->name, pattern, len); names->name[len] = '\0'; } pglob->gl_pathv = (char **)XtRealloc((char *)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; while (names != NULL) { struct globlink *next = names->next; pglob->gl_pathv[pglob->gl_pathc++] = names->name; XtFree((char *)names); names = next; } pglob->gl_pathv[pglob->gl_pathc] = NULL; pglob->gl_flags = flags; if (stream != NULL) { int save = errno; #ifdef WIN32 FindClose(stream); #else if (__glob_closedir_hook) (*__glob_closedir_hook) (stream); else closedir((DIR *) stream); #endif errno = save; } return nfound == 0 ? GLOB_NOMATCH : 0; memory_error: { int save = errno; #ifdef WIN32 FindClose(stream); #else if (__glob_closedir_hook) (*__glob_closedir_hook) (stream); else closedir((DIR *) stream); #endif errno = save; } while (names != NULL) { if (names->name != NULL) XtFree((char *) names->name); names = names->next; } if (names) XtFree((char *)names); return GLOB_NOSPACE; } /* glob_in_dir() */
/* 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, it is called with the pathname that caused the error, and the `errno' value from the failing call; if it returns non-zero `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 _Lesstif_glob(const char *pattern, int flags, errfunc_t errfunc, glob_t *pglob) { char *dirname; const char *filename; size_t dirlen; int status; int oldcount; if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0) { errno = EINVAL; return -1; } DEBUGOUT(_LtDebug(__FILE__, NULL, "_Lesstif_glob(%s, %i)\n", pattern, flags)); /* Find the filename. */ filename = strrchr(pattern, '/'); if (filename == NULL) { dirname=XtMalloc(2); filename = pattern; strcpy(dirname, "."); dirlen = 0; } else if (filename == pattern) { /* "/pattern". */ dirname=XtMalloc(2); strcpy(dirname, "/"); dirlen = 1; ++filename; } else { dirlen = filename - pattern; dirname = XtMalloc(dirlen + 1); memcpy(dirname, pattern, dirlen); dirname[dirlen] = '\0'; ++filename; } if (filename[0] == '\0' && dirlen > 1) /* "pattern/". Expand "pattern", appending slashes. */ { int val = _Lesstif_glob(dirname, flags | GLOB_MARK, errfunc, pglob); if (val == 0) pglob->gl_flags = (pglob->gl_flags & ~GLOB_MARK) | (flags & GLOB_MARK); XtFree(dirname); return val; } if (!(flags & GLOB_APPEND)) { pglob->gl_pathc = 0; pglob->gl_pathv = NULL; } oldcount = pglob->gl_pathc; 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 the pattern in each directory found. */ glob_t dirs; register int i; DEBUGOUT(_LtDebug(__FILE__, NULL, "glob_pattern(%s, !(flags & GLOB_NOESCAPE))!=NULL\n", dirname)); status = _Lesstif_glob(dirname, ((flags & (GLOB_ERR | GLOB_NOCHECK | GLOB_NOESCAPE)) | GLOB_NOSORT), errfunc, &dirs); if (status != 0) { XtFree(dirname); return status; } /* We have successfully globbed the preceding directory name. For each name we found, call glob_in_dir on it and FILENAME, appending the results to PGLOB. */ for (i = 0; i < dirs.gl_pathc; ++i) { int oldcount; oldcount = pglob->gl_pathc; status = glob_in_dir(filename, dirs.gl_pathv[i], (flags | GLOB_APPEND) & ~GLOB_NOCHECK, errfunc, pglob); if (status == GLOB_NOMATCH) /* No matches in this directory. Try the next. */ continue; if (status != 0) { _Lesstif_globfree(&dirs); _Lesstif_globfree(pglob); XtFree(dirname); return status; } /* Stick the directory on the front of each name. */ if (prefix_array(dirs.gl_pathv[i], &pglob->gl_pathv[oldcount], pglob->gl_pathc - oldcount)) { _Lesstif_globfree(&dirs); _Lesstif_globfree(pglob); XtFree(dirname); return GLOB_NOSPACE; } } flags |= GLOB_MAGCHAR; if (pglob->gl_pathc == oldcount) { /* No matches. */ if (flags & GLOB_NOCHECK) { size_t len = strlen(pattern) + 1; char *patcopy = (char *)XtMalloc(len); if (patcopy == NULL) { XtFree(dirname); return GLOB_NOSPACE; } memcpy(patcopy, pattern, len); pglob->gl_pathv = (char **)XtRealloc((char *)pglob->gl_pathv, (pglob->gl_pathc + ((flags & GLOB_DOOFFS) ? pglob->gl_offs : 0) + 1 + 1) * sizeof(char *)); if (pglob->gl_pathv == NULL) { XtFree(patcopy); XtFree(dirname); return GLOB_NOSPACE; } if (flags & GLOB_DOOFFS) while (pglob->gl_pathc < pglob->gl_offs) pglob->gl_pathv[pglob->gl_pathc++] = NULL; pglob->gl_pathv[pglob->gl_pathc++] = patcopy; pglob->gl_pathv[pglob->gl_pathc] = NULL; pglob->gl_flags = flags; } else { XtFree(dirname); return GLOB_NOMATCH; } } } /* if (glob_pattern_p(dirname, !(flags & GLOB_NOESCAPE))) */ else { DEBUGOUT(_LtDebug(__FILE__, NULL, "glob_pattern(%s, !(flags & GLOB_NOESCAPE))=NULL\n", dirname)); status = glob_in_dir(filename, dirname, flags, errfunc, pglob); if (status != 0) { XtFree(dirname); return status; } if (dirlen > 0) { /* Stick the directory on the front of each name. */ if (prefix_array(dirname, &pglob->gl_pathv[oldcount], pglob->gl_pathc - oldcount)) { _Lesstif_globfree(pglob); XtFree(dirname); return GLOB_NOSPACE; } } } /* else */ if (!(flags & GLOB_NOSORT)) /* Sort the vector. */ qsort((void *) & pglob->gl_pathv[oldcount], pglob->gl_pathc - oldcount, sizeof(char *), collated_compare); if (flags & GLOB_MARK) { /* Append slashes to directory names. glob_in_dir has already allocated the extra character for us. This must be done after the sorting to avoid screwing up the order (`"./" > "../"' but `"." < ".."'). */ int i; struct stat st; DEBUGOUT(_LtDebug(__FILE__, NULL, "_Lesstif_glob(): GLOB_NOSORT\n")); for (i = oldcount; i < pglob->gl_pathc; ++i) if (LTstat(pglob->gl_pathv[i], &st) == 0 && S_ISDIR(st.st_mode)) strcat(pglob->gl_pathv[i], "/"); } XtFree(dirname); return 0; } /* _Lesstif_glob() */