From 968ccce951388dd05e41e7e689e5552e7f70282a Mon Sep 17 00:00:00 2001 From: sin Date: Mon, 30 Jun 2014 15:39:42 +0100 Subject: [PATCH] Sync util/recurse.c with that of sbase --- lsusb.c | 11 ++++++----- ps.c | 18 +++++++++++------- util/recurse.c | 30 ++++++++++++++++-------------- 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/lsusb.c b/lsusb.c index 2bcc4af..231bdd9 100644 --- a/lsusb.c +++ b/lsusb.c @@ -1,7 +1,7 @@ /* See LICENSE file for copyright and license details. */ +#include #include #include -#include #include "text.h" #include "util.h" @@ -29,15 +29,16 @@ static void lsusb(const char *file) { FILE *fp; - char *cwd; char path[PATH_MAX]; char *buf = NULL; size_t size = 0; unsigned int i = 0, busnum = 0, devnum = 0, pid = 0, vid = 0; - cwd = agetcwd(); - snprintf(path, sizeof(path), "%s/%s/uevent", cwd, file); - free(cwd); + if (strlcpy(path, file, sizeof(path)) >= sizeof(path)) + eprintf("path too long\n"); + if (strlcat(path, "/uevent", sizeof(path)) >= sizeof(path)) + eprintf("path too long\n"); + if (!(fp = fopen(path, "r"))) return; while (agetline(&buf, &size, fp) != -1) { diff --git a/ps.c b/ps.c index 196c2b2..2dadb08 100644 --- a/ps.c +++ b/ps.c @@ -1,14 +1,14 @@ /* See LICENSE file for copyright and license details. */ -#include -#include #include -#include +#include +#include #include #include #include -#include #include -#include +#include +#include +#include #include "proc.h" #include "util.h" @@ -169,12 +169,16 @@ psout(struct procstat *ps) static void psr(const char *file) { + char path[PATH_MAX], *p; struct procstat ps; pid_t pid; - if (!pidfile(file)) + if (strlcpy(path, file, sizeof(path)) >= sizeof(path)) + eprintf("path too long\n"); + p = basename(path); + if (pidfile(p) == 0) return; - pid = estrtol(file, 10); + pid = estrtol(p, 10); if (parsestat(pid, &ps) < 0) return; psout(&ps); diff --git a/util/recurse.c b/util/recurse.c index b3d1f8c..1db70d2 100644 --- a/util/recurse.c +++ b/util/recurse.c @@ -1,17 +1,19 @@ /* See LICENSE file for copyright and license details. */ #include -#include +#include +#include #include #include -#include #include +#include +#include #include "../util.h" void recurse(const char *path, void (*fn)(const char *)) { - char *cwd; + char buf[PATH_MAX], *p; struct dirent *d; struct stat st; DIR *dp; @@ -22,19 +24,19 @@ recurse(const char *path, void (*fn)(const char *)) eprintf("opendir %s:", path); } - cwd = agetcwd(); - if(chdir(path) == -1) - eprintf("chdir %s:", path); - while((d = readdir(dp))) { - if(strcmp(d->d_name, ".") && strcmp(d->d_name, "..")) - fn(d->d_name); + if (strcmp(d->d_name, ".") == 0 || + strcmp(d->d_name, "..") == 0) + continue; + strlcpy(buf, path, sizeof(buf)); + p = strrchr(buf, '\0'); + /* remove all trailing slashes */ + while (--p >= buf && *p == '/') *p ='\0'; + strlcat(buf, "/", sizeof(buf)); + if (strlcat(buf, d->d_name, sizeof(buf)) >= sizeof(buf)) + eprintf("path too long\n"); + fn(buf); } closedir(dp); - if(chdir(cwd) == -1) - eprintf("chdir %s:", cwd); - - free(cwd); } -