[hackers] [sbase] Use dynamic array in recurse() instead of PATH_MAX-array || FRIGN

From: <git_AT_suckless.org>
Date: Tue, 24 Mar 2015 23:54:06 +0100 (CET)

commit 903d43bbb85a2846030cdfaae8b6d7597b16a1d6
Author: FRIGN <dev_AT_frign.de>
Date: Tue Mar 3 00:11:41 2015 +0100

    Use dynamic array in recurse() instead of PATH_MAX-array
    
    Thanks Evan!

diff --git a/libutil/recurse.c b/libutil/recurse.c
index f30f160..295899a 100644
--- a/libutil/recurse.c
+++ b/libutil/recurse.c
_AT_@ -15,10 +15,11 @@ int recurse_follow = 'P';
 void
 recurse(const char *path, void (*fn)(const char *, int), int depth)
 {
- char buf[PATH_MAX];
         struct dirent *d;
         struct stat lst, st;
         DIR *dp;
+ size_t len;
+ char *buf;
 
         if (lstat(path, &lst) < 0)
                 eprintf("lstat %s:", path);
_AT_@ -31,17 +32,14 @@ recurse(const char *path, void (*fn)(const char *, int), int depth)
         if (!(dp = opendir(path)))
                 eprintf("opendir %s:", path);
 
+ len = strlen(path);
         while ((d = readdir(dp))) {
                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
                         continue;
- if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
- eprintf("path too long\n");
- 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");
+ buf = emalloc(len + (*(path + len) != '/') + strlen(d->d_name) + 1);
+ sprintf(buf, "%s%s%s", path, (*(path + len) == '/') ? "" : "/", d->d_name);
                 fn(buf, depth + 1);
+ free(buf);
         }
 
         closedir(dp);
Received on Tue Mar 24 2015 - 23:54:06 CET

This archive was generated by hypermail 2.3.0 : Wed Mar 25 2015 - 00:11:14 CET