[hackers] [ubase] Sync util/recurse.c with that of sbase || sin

From: <git_AT_suckless.org>
Date: Mon, 30 Jun 2014 16:45:25 +0200

commit 968ccce951388dd05e41e7e689e5552e7f70282a
Author: sin <sin_AT_2f30.org>
Date: Mon Jun 30 15:39:42 2014 +0100

    Sync util/recurse.c with that of sbase

diff --git a/lsusb.c b/lsusb.c
index 2bcc4af..231bdd9 100644
--- a/lsusb.c
+++ b/lsusb.c
_AT_@ -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"
 
_AT_@ -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
");
+ if (strlcat(path, "/uevent", sizeof(path)) >= sizeof(path))
+ eprintf("path too long
");
+
         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
_AT_@ -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"
 
_AT_@ -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
");
+ 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
_AT_@ -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;
_AT_@ -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, '
Received on Mon Jun 30 2014 - 16:45:25 CEST

This archive was generated by hypermail 2.3.0 : Mon Jun 30 2014 - 16:48:08 CEST