- memset(&globbuf, 0, sizeof globbuf);
- glob(s, 0, NULL, &globbuf);
- /* Note: we check the first match against the filter list,
- * just in case the user specified a wildcard in the path. */
- if ((count = globbuf.gl_pathc) > 0) {
- if (filter_daemon_path(globbuf.gl_pathv[0])) {
- int slashes = 0;
- char *cp;
- /* Truncate original arg at glob's truncation point. */
- for (cp = globbuf.gl_pathv[0]; *cp; cp++) {
- if (*cp == '/')
- slashes++;
- }
- for (cp = s; *cp; cp++) {
- if (*cp == '/') {
- if (slashes-- <= 0) {
- *cp = '\0';
- break;
- }
- }
- }
- have_glob_results = 0;
- count = 1;
- } else
- have_glob_results = 1;
- } else {
- /* This truncates "s" at a filtered element, if present. */
- filter_daemon_path(s);
- have_glob_results = 0;
- count = 1;
- }
- if (count + argc > maxargs) {
- maxargs += count + MAX_ARGS;
- if (!(argv = realloc_array(argv, char *, maxargs)))
- out_of_memory("glob_expand");
- *argv_ptr = argv;
- *maxargs_ptr = maxargs;
- }
- if (have_glob_results) {
- int i;
- free(s);
- for (i = 0; i < count; i++) {
- if (!(argv[argc++] = strdup(globbuf.gl_pathv[i])))
- out_of_memory("glob_expand");
- }
+ if (glob.absize < MAXPATHLEN
+ && !(glob.arg_buf = realloc_array(glob.arg_buf, char, glob.absize = MAXPATHLEN)))
+ out_of_memory("glob_expand");
+ *glob.arg_buf = '\0';
+
+ glob.argc = save_argc = *argc_p;
+ glob.argv = *argv_p;
+ glob.maxargs = *maxargs_p;
+
+ if (glob.maxargs < MAX_ARGS
+ && !(glob.argv = realloc_array(glob.argv, char *, glob.maxargs = MAX_ARGS)))
+ out_of_memory("glob_expand");
+
+ glob_match(s, 0, -1);
+
+ /* The arg didn't match anything, so add the failed arg to the list. */
+ if (glob.argc == save_argc) {
+ ENSURE_MEMSPACE(glob.argv, char *, glob.maxargs, glob.argc + 1);
+ glob.argv[glob.argc++] = s;