mirror of git://git.suckless.org/sbase
Implement -o support for pidof(8)
This commit is contained in:
parent
1003ebad26
commit
755a96237c
46
pidof.c
46
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,14 +47,32 @@ 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;
|
||||
for (i = 0; i < argc; i++) {
|
||||
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) {
|
||||
|
@ -52,6 +82,7 @@ main(int argc, char *argv[])
|
|||
*p = '\0';
|
||||
cmd = basename(cmdline);
|
||||
}
|
||||
for (i = 0; i < argc; i++) {
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue