aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/lib.c25
-rw-r--r--lib/lib.h1
-rw-r--r--lib/pending.c25
-rw-r--r--lib/pending.h5
4 files changed, 26 insertions, 30 deletions
diff --git a/lib/lib.c b/lib/lib.c
index 16c36b02..6a9a0bdd 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -710,3 +710,28 @@ void mode_to_string(mode_t mode, char *buf)
else c = '-';
*buf = c;
}
+
+// Execute a callback for each PID that matches a process name from a list.
+void names_to_pid(char **names, int (*callback)(pid_t pid, char *name))
+{
+ DIR *dp;
+ struct dirent *entry;
+
+ if (!(dp = opendir("/proc"))) perror_exit("opendir");
+
+ while ((entry = readdir(dp))) {
+ unsigned u;
+ char *cmd, **curname;
+
+ 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 (**curname == '/' ? !strcmp(cmd, *curname)
+ : !strcmp(basename(cmd), basename(*curname)))
+ if (callback(u, *curname)) break;
+ if (*curname) break;
+ }
+ closedir(dp);
+}
diff --git a/lib/lib.h b/lib/lib.h
index 6edfa0fc..3d326499 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -177,5 +177,6 @@ char *num_to_sig(int sig);
mode_t string_to_mode(char *mode_str, mode_t base);
void mode_to_string(mode_t mode, char *buf);
+void names_to_pid(char **names, int (*callback)(pid_t pid, char *name));
#include "lib/pending.h"
diff --git a/lib/pending.c b/lib/pending.c
index df3207ef..741c5cc1 100644
--- a/lib/pending.c
+++ b/lib/pending.c
@@ -5,31 +5,6 @@
#include "toys.h"
-// Execute a callback for each PID that matches a process name from a list.
-void names_to_pid(char **names, int (*callback)(pid_t pid, char *name))
-{
- DIR *dp;
- struct dirent *entry;
-
- if (!(dp = opendir("/proc"))) perror_exit("opendir");
-
- while ((entry = readdir(dp))) {
- unsigned u;
- char *cmd, **curname;
-
- 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 (**curname == '/' ? !strcmp(cmd, *curname)
- : !strcmp(basename(cmd), basename(*curname)))
- if (callback(u, *curname)) break;
- if (*curname) break;
- }
- closedir(dp);
-}
-
void daemonize(void)
{
int fd = open("/dev/null", O_RDWR);
diff --git a/lib/pending.h b/lib/pending.h
index 43885719..67605c0b 100644
--- a/lib/pending.h
+++ b/lib/pending.h
@@ -8,11 +8,6 @@
typedef float FLOAT;
//#endif
-// libc generally has this, but the headers are screwed up
-ssize_t getline(char **lineptr, size_t *n, FILE *stream);
-
-void names_to_pid(char **names, int (*callback)(pid_t pid, char *name));
-
// password.c
#define MAX_SALT_LEN 20 //3 for id, 16 for key, 1 for '\0'
#define SYS_FIRST_ID 100