1 /* pidof.c - Print the Process IDs of all processes with the given names.
2 *
3 * Copyright 2012 Andreas Heck <aheck@gmx.de>
4 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
5 *
6 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/pidof.html
7
8 USE_PIDOF(NEWTOY(pidof, "<1so:x", TOYFLAG_BIN))
9
10 config PIDOF
11 bool "pidof"
12 default y
13 help
14 usage: pidof [-s] [-o omitpid[,omitpid...]] [NAME]...
15
16 Print the PIDs of all processes with the given names.
17
18 -s Single shot, only return one pid
19 -o Omit PID(s)
20 -x Match shell scripts too
21 */
22
23 #define FOR_pidof
24 #include "toys.h"
25
GLOBALS(char * omit;)26 GLOBALS(
27 char *omit;
28 )
29
30 static int print_pid(pid_t pid, char *name)
31 {
32 sprintf(toybuf, "%d", (int)pid);
33 if (comma_scan(TT.omit, toybuf, 0)) return 0;
34 xprintf(" %s"+!!toys.exitval, toybuf);
35 toys.exitval = 0;
36
37 return toys.optflags & FLAG_s;
38 }
39
pidof_main(void)40 void pidof_main(void)
41 {
42 toys.exitval = 1;
43 names_to_pid(toys.optargs, print_pid, FLAG(x));
44 if (!toys.exitval) xputc('\n');
45 }
46