diff options
author | Rob Landley <rob@landley.net> | 2012-02-18 15:12:41 -0600 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2012-02-18 15:12:41 -0600 |
commit | ff9ee8fc15e1a41bffe06bfcee30368e7c117601 (patch) | |
tree | 4806b88fe328eb96c4a9f7df41dd5c8859a56117 /toys/pidof.c | |
parent | 3c1104ff57f22495a7711eabb4c0d3b5cda4a982 (diff) | |
download | toybox-ff9ee8fc15e1a41bffe06bfcee30368e7c117601.tar.gz |
Add killall by Andreas Heck, and factor out common pid code to lib.h.
Diffstat (limited to 'toys/pidof.c')
-rw-r--r-- | toys/pidof.c | 70 |
1 files changed, 10 insertions, 60 deletions
diff --git a/toys/pidof.c b/toys/pidof.c index c6f0d556..329b0084 100644 --- a/toys/pidof.c +++ b/toys/pidof.c @@ -7,7 +7,7 @@ * Not in SUSv4. * See http://opengroup.org/onlinepubs/9699919799/utilities/ -USE_PIDOF(NEWTOY(pidof, "e@d*c#b:a", TOYFLAG_USR|TOYFLAG_BIN)) +USE_PIDOF(NEWTOY(pidof, "", TOYFLAG_USR|TOYFLAG_BIN)) config PIDOF bool "pidof" @@ -20,80 +20,30 @@ config PIDOF #include "toys.h" +DEFINE_GLOBALS( + int matched; +) #define TT this.pidof -#define PATH_LEN 64 -#define PROC_DIR "/proc/" -#define CMD_LINE "/cmdline" - -static int matched = 0; - -static int for_each_pid(void (*callback) (const char *pid)) { - DIR *dp; - struct dirent *entry; - FILE *fp; - int n, pathpos; - char cmd[PATH_MAX]; - char path[PATH_LEN]; - char **curname; - - dp = opendir(PROC_DIR); - if (!dp) { - perror("opendir"); - return 1; - } - - while ((entry = readdir(dp))) { - if (!isdigit(entry->d_name[0])) continue; - strcpy(path, PROC_DIR); - pathpos = strlen(PROC_DIR); - - if (pathpos + strlen(entry->d_name) + 1 > PATH_LEN) continue; - - strcpy(&path[pathpos], entry->d_name); - pathpos += strlen(entry->d_name); - - if (pathpos + strlen(CMD_LINE) + 1 > PATH_LEN) continue; - strcpy(&path[pathpos], CMD_LINE); - - fp = fopen(path, "r"); - if (!fp) { - perror("fopen"); - continue; - } - - n = fread(cmd, 1, PATH_MAX, fp); - fclose(fp); - if (n == 0) continue; - - for (curname = toys.optargs; *curname; curname++) { - if (strcmp(basename(cmd), *curname) == 0) { - callback(entry->d_name); - } - } - } - - closedir(dp); - - return 0; -} static void print_pid (const char *pid) { - if (matched) putchar(' '); + if (TT.matched) putchar(' '); fputs(pid, stdout); - matched = 1; + TT.matched = 1; } void pidof_main(void) { int err; + TT.matched = 0; + if (!toys.optargs) exit(1); - err = for_each_pid(print_pid); + err = for_each_pid_with_name_in(toys.optargs, print_pid); if (err) exit(1); - if (!matched) + if (!TT.matched) exit(1); else putchar('\n'); |