Sync util/recurse.c with that of sbase

This commit is contained in:
sin 2014-06-30 15:39:42 +01:00
parent 693de34f88
commit 968ccce951
3 changed files with 33 additions and 26 deletions

11
lsusb.c
View File

@ -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) {

18
ps.c
View File

@ -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);

View File

@ -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);
}