mirror of https://github.com/sheumann/hush.git
In glob, factor out "stat" call and structure to a separate function. This reduces stack usage in the recursive glob function.
This commit is contained in:
parent
95bcfc3b6e
commit
432950e315
13
shell/glob.c
13
shell/glob.c
|
@ -43,6 +43,11 @@ static int cmp_func(const void * a, const void * b)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int is_directory(const char *path) {
|
||||||
|
struct stat statr;
|
||||||
|
return !stat(path, &statr) && S_ISDIR(statr.st_mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Like `glob', but PATTERN is a final pathname component,
|
/* Like `glob', but PATTERN is a final pathname component,
|
||||||
and matches are searched for in DIRECTORY.
|
and matches are searched for in DIRECTORY.
|
||||||
|
@ -107,8 +112,7 @@ static int glob_in_dir(const char *pattern, const char *directory, int flags,
|
||||||
goto memory_error;
|
goto memory_error;
|
||||||
build_fullname(ptr, directory, ep->d_name);
|
build_fullname(ptr, directory, ep->d_name);
|
||||||
if (flags & GLOB_ONLYDIR) {
|
if (flags & GLOB_ONLYDIR) {
|
||||||
struct stat statr;
|
if (!is_directory(ptr))
|
||||||
if (stat(ptr, &statr) || !S_ISDIR(statr.st_mode))
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (fnmatch(pattern, ep->d_name, fnm_flags) == 0)
|
if (fnmatch(pattern, ep->d_name, fnm_flags) == 0)
|
||||||
|
@ -156,7 +160,6 @@ int glob(const char *pattern, int flags, int (*errfunc)(const char * epath, int
|
||||||
char * filename;
|
char * filename;
|
||||||
char * dirname;
|
char * dirname;
|
||||||
size_t oldcount;
|
size_t oldcount;
|
||||||
struct stat statr;
|
|
||||||
|
|
||||||
size_t i; /* tmp variables are declared here to save a bit of object space */
|
size_t i; /* tmp variables are declared here to save a bit of object space */
|
||||||
int j, k; /* */
|
int j, k; /* */
|
||||||
|
@ -321,7 +324,7 @@ int glob(const char *pattern, int flags, int (*errfunc)(const char * epath, int
|
||||||
if (flags & GLOB_NOCHECK)
|
if (flags & GLOB_NOCHECK)
|
||||||
{
|
{
|
||||||
for (i = 0; i < dirs.gl_pathc; i++) {
|
for (i = 0; i < dirs.gl_pathc; i++) {
|
||||||
if (stat(dirs.gl_pathv[i], &statr) || !S_ISDIR(statr.st_mode))
|
if (!is_directory(dirs.gl_pathv[i]))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* stat is okay, we will add the entry, but before let's resize the pathv */
|
/* stat is okay, we will add the entry, but before let's resize the pathv */
|
||||||
|
@ -358,7 +361,7 @@ int glob(const char *pattern, int flags, int (*errfunc)(const char * epath, int
|
||||||
|
|
||||||
if (flags & GLOB_MARK) {
|
if (flags & GLOB_MARK) {
|
||||||
for (i = oldcount; i < pglob->gl_pathc + pglob->gl_offs; i++)
|
for (i = oldcount; i < pglob->gl_pathc + pglob->gl_offs; i++)
|
||||||
if (!stat(pglob->gl_pathv[i], &statr) && S_ISDIR(statr.st_mode)) {
|
if (is_directory(pglob->gl_pathv[i])) {
|
||||||
size_t len = strlen(pglob->gl_pathv[i]) + 2;
|
size_t len = strlen(pglob->gl_pathv[i]) + 2;
|
||||||
ptr = realloc(pglob->gl_pathv[i], len);
|
ptr = realloc(pglob->gl_pathv[i], len);
|
||||||
if (ptr == NULL) {
|
if (ptr == NULL) {
|
||||||
|
|
Loading…
Reference in New Issue