Use dynamic array in recurse() instead of PATH_MAX-array

Thanks Evan!
This commit is contained in:
FRIGN 2015-03-03 00:11:41 +01:00
parent 8dc92fbd6c
commit 903d43bbb8

View File

@ -15,10 +15,11 @@ int recurse_follow = 'P';
void void
recurse(const char *path, void (*fn)(const char *, int), int depth) recurse(const char *path, void (*fn)(const char *, int), int depth)
{ {
char buf[PATH_MAX];
struct dirent *d; struct dirent *d;
struct stat lst, st; struct stat lst, st;
DIR *dp; DIR *dp;
size_t len;
char *buf;
if (lstat(path, &lst) < 0) if (lstat(path, &lst) < 0)
eprintf("lstat %s:", path); eprintf("lstat %s:", path);
@ -31,17 +32,14 @@ recurse(const char *path, void (*fn)(const char *, int), int depth)
if (!(dp = opendir(path))) if (!(dp = opendir(path)))
eprintf("opendir %s:", path); eprintf("opendir %s:", path);
len = strlen(path);
while ((d = readdir(dp))) { while ((d = readdir(dp))) {
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue; continue;
if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) buf = emalloc(len + (*(path + len) != '/') + strlen(d->d_name) + 1);
eprintf("path too long\n"); sprintf(buf, "%s%s%s", path, (*(path + len) == '/') ? "" : "/", d->d_name);
if (buf[strlen(buf) - 1] != '/')
if (strlcat(buf, "/", sizeof(buf)) >= sizeof(buf))
eprintf("path too long\n");
if (strlcat(buf, d->d_name, sizeof(buf)) >= sizeof(buf))
eprintf("path too long\n");
fn(buf, depth + 1); fn(buf, depth + 1);
free(buf);
} }
closedir(dp); closedir(dp);