char *globing(char *str, t_global *glob, int tok) { char *new_str; if (str == NULL) return (NULL); new_str = malloc(1); new_str[0] = 0; if (tok & GLOB_DIR) new_str = glob_in_dir(str, new_str, tok); return (new_str); }
/* 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() */