xref: /third_party/toybox/toys/lsb/pidof.c (revision 0f66f451)
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
8USE_PIDOF(NEWTOY(pidof, "so:x", TOYFLAG_BIN))
9
10config 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    -o	Omit PID(s)
19    -s	Single shot, only return one pid
20    -x	Match shell scripts too
21*/
22
23#define FOR_pidof
24#include "toys.h"
25
26GLOBALS(
27  char *o;
28)
29
30static int print_pid(pid_t pid, char *name)
31{
32  sprintf(toybuf, "%d", (int)pid);
33  if (comma_scan(TT.o, toybuf, 0)) return 0;
34  xprintf(" %s"+!!toys.exitval, toybuf);
35  toys.exitval = 0;
36
37  return FLAG(s);
38}
39
40void 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