find: remove VLAs
Laslo: Use ereallocarray and fix the style a bit
This commit is contained in:
parent
0b27c0c9a0
commit
d2bd40a589
23
find.c
23
find.c
@ -777,7 +777,7 @@ find_op(char *name)
|
|||||||
static void
|
static void
|
||||||
parse(int argc, char **argv)
|
parse(int argc, char **argv)
|
||||||
{
|
{
|
||||||
struct tok infix[2 * argc + 1], *stack[argc], *tok, *rpn, *out, **top;
|
struct tok *tok, *rpn, *out, **top, *infix, **stack;
|
||||||
struct op_info *op;
|
struct op_info *op;
|
||||||
struct pri_info *pri;
|
struct pri_info *pri;
|
||||||
char **arg;
|
char **arg;
|
||||||
@ -785,6 +785,9 @@ parse(int argc, char **argv)
|
|||||||
size_t ntok = 0;
|
size_t ntok = 0;
|
||||||
struct tok and = { .u.oinfo = find_op("-a"), .type = AND };
|
struct tok and = { .u.oinfo = find_op("-a"), .type = AND };
|
||||||
|
|
||||||
|
infix = ereallocarray(NULL, 2 * argc + 1, sizeof(*infix));
|
||||||
|
stack = ereallocarray(NULL, argc, sizeof(*stack));
|
||||||
|
|
||||||
gflags.print = 1;
|
gflags.print = 1;
|
||||||
|
|
||||||
/* convert argv to infix expression of tok, inserting in *tok */
|
/* convert argv to infix expression of tok, inserting in *tok */
|
||||||
@ -894,6 +897,9 @@ parse(int argc, char **argv)
|
|||||||
|
|
||||||
toks = rpn;
|
toks = rpn;
|
||||||
root = *top;
|
root = *top;
|
||||||
|
|
||||||
|
free(infix);
|
||||||
|
free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* for a primary, run and return result
|
/* for a primary, run and return result
|
||||||
@ -932,8 +938,11 @@ find(char *path, struct findhist *hist)
|
|||||||
DIR *dir;
|
DIR *dir;
|
||||||
struct dirent *de;
|
struct dirent *de;
|
||||||
struct findhist *f, cur;
|
struct findhist *f, cur;
|
||||||
size_t len = strlen(path) + 2; /* null and '/' */
|
size_t namelen, pathcap = 0, len;
|
||||||
struct arg arg = { path, &st, { NULL } };
|
struct arg arg = { path, &st, { NULL } };
|
||||||
|
char *p, *pathbuf = NULL;
|
||||||
|
|
||||||
|
len = strlen(path) + 2; /* \0 and '/' */
|
||||||
|
|
||||||
if ((gflags.l || (gflags.h && !hist) ? stat(path, &st) : lstat(path, &st)) < 0) {
|
if ((gflags.l || (gflags.h && !hist) ? stat(path, &st) : lstat(path, &st)) < 0) {
|
||||||
weprintf("failed to stat %s:", path);
|
weprintf("failed to stat %s:", path);
|
||||||
@ -975,18 +984,20 @@ find(char *path, struct findhist *hist)
|
|||||||
}
|
}
|
||||||
|
|
||||||
while (errno = 0, (de = readdir(dir))) {
|
while (errno = 0, (de = readdir(dir))) {
|
||||||
size_t pathcap = len + strlen(de->d_name);
|
|
||||||
char pathbuf[pathcap], *p;
|
|
||||||
|
|
||||||
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
|
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
|
||||||
continue;
|
continue;
|
||||||
|
namelen = strlen(de->d_name);
|
||||||
|
if (len + namelen > pathcap) {
|
||||||
|
pathcap = len + namelen;
|
||||||
|
pathbuf = erealloc(pathbuf, pathcap);
|
||||||
|
}
|
||||||
p = pathbuf + estrlcpy(pathbuf, path, pathcap);
|
p = pathbuf + estrlcpy(pathbuf, path, pathcap);
|
||||||
if (*--p != '/')
|
if (*--p != '/')
|
||||||
estrlcat(pathbuf, "/", pathcap);
|
estrlcat(pathbuf, "/", pathcap);
|
||||||
estrlcat(pathbuf, de->d_name, pathcap);
|
estrlcat(pathbuf, de->d_name, pathcap);
|
||||||
find(pathbuf, &cur);
|
find(pathbuf, &cur);
|
||||||
}
|
}
|
||||||
|
free(pathbuf);
|
||||||
if (errno) {
|
if (errno) {
|
||||||
weprintf("readdir %s:", path);
|
weprintf("readdir %s:", path);
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
|
Loading…
Reference in New Issue
Block a user