aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

pidof.c (2548B)


      1 /* see LICENSE file for copyright and license details */
      2 
      3 #include <sys/types.h>
      4 
      5 #include <dirent.h>
      6 #include <libgen.h>
      7 #include <limits.h>
      8 #include <stdio.h>
      9 #include <stdlib.h>
     10 #include <string.h>
     11 #include <unistd.h>
     12 
     13 #include "proc.h"
     14 #include "queue.h"
     15 #include "util.h"
     16 
     17 struct pidentry {
     18   pid_t pid;
     19   SLIST_ENTRY(pidentry) entry;
     20 };
     21 
     22 static SLIST_HEAD(, pidentry) omitpid_head;
     23 
     24 static void
     25 usage(void)
     26 {
     27   eprintf("usage: %s [-o pid1,pid2,...pidN] [-s] [program...]\n", argv0);
     28 }
     29 
     30 /* what main sets for the walk of the process table */
     31 struct pidof {
     32   int    argc;
     33   char **argv;
     34   int    sflag, oflag, found;
     35 };
     36 
     37 static int
     38 each(const struct procinfo *p, void *arg)
     39 {
     40   struct pidof    *w = arg;
     41   struct pidentry *pe;
     42   char             cmdline[BUFSIZ], *cmd, *cmdbase, *sp;
     43   char             word[16];
     44   int              i;
     45 
     46   if (w->oflag) {
     47     SLIST_FOREACH(pe, &omitpid_head, entry)
     48     if (pe->pid == p->pid)
     49       return 0;
     50   }
     51   if (proc_args(p->pid, cmdline, sizeof(cmdline)) < 0) {
     52     cmd     = (char *)p->comm;
     53     cmdbase = cmd;
     54   } else {
     55     if ((sp = strchr(cmdline, ' ')))
     56       *sp = '\0';
     57     cmd     = cmdline;
     58     cmdbase = basename(cmdline);
     59   }
     60   /* workaround for login shells */
     61   if (cmd[0] == '-')
     62     cmd++;
     63   snprintf(word, sizeof(word), "%d", (int)p->pid);
     64   for (i = 0; i < w->argc; i++) {
     65     if (strcmp(cmd, w->argv[i]) == 0 || strcmp(cmdbase, w->argv[i]) == 0) {
     66       putword(stdout, word);
     67       w->found++;
     68       if (w->sflag)
     69         return 1;
     70     }
     71   }
     72   return 0;
     73 }
     74 
     75 // ?man pidof: find process ids
     76 // ?man arguments: -o pid1
     77 // ?man find the process identity numbers of running programs
     78 int
     79 main(int argc, char *argv[])
     80 {
     81   char            *p, *arg = NULL;
     82   struct pidof     w = {0};
     83   struct pidentry *pe;
     84 
     85   ARGBEGIN
     86   {
     87     // ?man -s: silent mode or print summary
     88     case 's':
     89       w.sflag = 1;
     90       break;
     91     // ?man -o:str: specify output file
     92     case 'o':
     93       w.oflag = 1;
     94       arg     = EARGF(usage());
     95       break;
     96     default:
     97       usage();
     98   }
     99   ARGEND;
    100 
    101   if (!argc)
    102     return 1;
    103   w.argc = argc;
    104   w.argv = argv;
    105 
    106   SLIST_INIT(&omitpid_head);
    107 
    108   if (w.oflag) {
    109     for (p = strtok(arg, ","); p; p = strtok(NULL, ",")) {
    110       pe = emalloc(sizeof(*pe));
    111       if (strcmp(p, "%PPID") == 0)
    112         pe->pid = getppid();
    113       else
    114         pe->pid = estrtol(p, 10);
    115       SLIST_INSERT_HEAD(&omitpid_head, pe, entry);
    116     }
    117   }
    118 
    119   if (proc_each(each, &w) < 0)
    120     eprintf("the process table cannot be read:");
    121   if (w.found)
    122     putchar('\n');
    123 
    124   return w.found ? 0 : 1;
    125 }