aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/pending.c25
1 files changed, 10 insertions, 15 deletions
diff --git a/lib/pending.c b/lib/pending.c
index 9e250854..a06912ee 100644
--- a/lib/pending.c
+++ b/lib/pending.c
@@ -6,33 +6,28 @@
#include "toys.h"
// Execute a callback for each PID that matches a process name from a list.
-void for_each_pid_with_name_in(char **names, int (*callback)(pid_t pid, char *name))
+void name_to_pid(char **names, int (*callback)(pid_t pid, char *name))
{
DIR *dp;
struct dirent *entry;
- char cmd[sizeof(toybuf)], path[64];
- char **curname;
if (!(dp = opendir("/proc"))) perror_exit("opendir");
while ((entry = readdir(dp))) {
int fd, n;
+ unsigned u;
+ char *cmd, **curname;
- if (!isdigit(*entry->d_name)) continue;
-
- if (sizeof(path) <= snprintf(path, sizeof(path), "/proc/%s/cmdline",
- entry->d_name)) continue;
-
- if (-1 == (fd=open(path, O_RDONLY))) continue;
- n = read(fd, cmd, sizeof(cmd));
- close(fd);
- if (n<1) continue;
+ if (!(u = atoi(entry->d_name))) continue;
+ sprintf(libbuf, "/proc/%u/cmdline", u);
+ if (!(cmd = readfile(libbuf, libbuf, sizeof(libbuf)))) continue;
for (curname = names; *curname; curname++)
- if (!strcmp(basename(cmd), *curname))
- if (!callback(atol(entry->d_name), *curname)) goto done;
+ if (*curname == '/' ? !strcmp(cmd, *curname)
+ : !strcmp(basename(cmd), basename(*curname))
+ if (!callback(u, *curname)) break;
+ if (*curname) break;
}
-done:
closedir(dp);
}