ubase

suckless linux base utils
git clone git://git.2f30.org/ubase
Log | Files | Refs | README | LICENSE

commit 755a96237cdf369f73b5cc1b6b95e8e3d8d02ed8
parent 1003ebad2600da527975442aa60cfacb820ee2c7
Author: sin <sin@2f30.org>
Date:   Mon,  2 Sep 2013 16:14:20 +0300

Implement -o support for pidof(8)

Diffstat:
Mpidof.c | 64+++++++++++++++++++++++++++++++++++++++++++++++++++-------------
1 file changed, 51 insertions(+), 13 deletions(-)

diff --git a/pidof.c b/pidof.c @@ -1,8 +1,10 @@ /* See LICENSE file for copyright and license details. */ #include <sys/types.h> +#include <unistd.h> #include <dirent.h> #include <libgen.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include "proc.h" #include "util.h" @@ -10,9 +12,14 @@ static void usage(void) { - eprintf("usage: %s [-s] [program...]\n", argv0); + eprintf("usage: %s [-os] [program...]\n", argv0); } +static struct omit { + pid_t pid; + struct omit *next; +} *omithead; + int main(int argc, char *argv[]) { @@ -20,14 +27,19 @@ main(int argc, char *argv[]) struct dirent *entry; pid_t pid; struct procstat ps; - char cmdline[BUFSIZ], *cmd, *p; + char cmdline[BUFSIZ], *cmd, *p, *arg; int i, found = 0; - int sflag = 0; + int sflag = 0, oflag = 0; + struct omit *onode, *tmp; ARGBEGIN { case 's': sflag = 1; break; + case 'o': + oflag = 1; + arg = EARGF(usage()); + break; default: usage(); } ARGEND; @@ -35,23 +47,42 @@ main(int argc, char *argv[]) if (!argc) return 1; + for (p = strtok(arg, ","); p; p = strtok(NULL, ",")) { + onode = malloc(sizeof(*onode)); + if (!onode) + eprintf("malloc:"); + if (strcmp(p, "%PPID") == 0) + onode->pid = getppid(); + else + onode->pid = estrtol(p, 10); + onode->next = omithead; + omithead = onode; + } + if (!(dp = opendir("/proc"))) eprintf("opendir /proc:"); while ((entry = readdir(dp))) { if (!pidfile(entry->d_name)) continue; + pid = estrtol(entry->d_name, 10); + if (oflag) { + for (onode = omithead; onode; onode = onode->next) + if (onode->pid == pid) + break; + if (onode) + continue; + } + parsestat(pid, &ps); + if (parsecmdline(ps.pid, cmdline, + sizeof(cmdline)) < 0) { + cmd = ps.comm; + } else { + if ((p = strchr(cmdline, ' '))) + *p = '\0'; + cmd = basename(cmdline); + } for (i = 0; i < argc; i++) { - pid = estrtol(entry->d_name, 10); - parsestat(pid, &ps); - if (parsecmdline(ps.pid, cmdline, - sizeof(cmdline)) < 0) { - cmd = ps.comm; - } else { - if ((p = strchr(cmdline, ' '))) - *p = '\0'; - cmd = basename(cmdline); - } if (strcmp(cmd, argv[i]) == 0) { putword(entry->d_name); found++; @@ -67,5 +98,12 @@ out: closedir(dp); + onode = omithead; + while (onode) { + tmp = onode->next; + free(onode); + onode = tmp; + } + return 0; }