ubase

suckless linux base utils
git clone git://git.2f30.org/ubase
Log | Files | Refs | README | LICENSE

commit 968ccce951388dd05e41e7e689e5552e7f70282a
parent 693de34f88385bc58b5db1fe25c934f1fcb64697
Author: sin <sin@2f30.org>
Date:   Mon, 30 Jun 2014 15:39:42 +0100

Sync util/recurse.c with that of sbase

Diffstat:
Mlsusb.c | 11++++++-----
Mps.c | 18+++++++++++-------
Mutil/recurse.c | 30++++++++++++++++--------------
3 files changed, 33 insertions(+), 26 deletions(-)

diff --git a/lsusb.c b/lsusb.c @@ -1,7 +1,7 @@ /* See LICENSE file for copyright and license details. */ +#include <limits.h> #include <stdio.h> #include <stdlib.h> -#include <limits.h> #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 @@ -1,14 +1,14 @@ /* See LICENSE file for copyright and license details. */ -#include <sys/sysinfo.h> -#include <sys/ioctl.h> #include <errno.h> -#include <unistd.h> +#include <limits.h> +#include <pwd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <limits.h> #include <time.h> -#include <pwd.h> +#include <sys/ioctl.h> +#include <sys/sysinfo.h> +#include <unistd.h> #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 @@ -1,17 +1,19 @@ /* See LICENSE file for copyright and license details. */ #include <dirent.h> -#include <errno.h> +#include <limits.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> #include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> #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); } -