aboutsummaryrefslogtreecommitdiff
path: root/toys/pidof.c
blob: 329b0084fa86a3cc66c6e3d0825b5084ab86b76f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* vi: set sw=4 ts=4:
 *
 * pidof.c - Print the PIDs of all processes with the given names.
 *
 * Copyright 2012 Andreas Heck <aheck@gmx.de>
 *
 * Not in SUSv4.
 * See http://opengroup.org/onlinepubs/9699919799/utilities/

USE_PIDOF(NEWTOY(pidof, "", TOYFLAG_USR|TOYFLAG_BIN))

config PIDOF
	bool "pidof"
	default y
	help
	  usage: pidof [NAME]...

	  Print the PIDs of all processes with the given names.
*/

#include "toys.h"

DEFINE_GLOBALS(
		int matched;
)
#define TT this.pidof


static void print_pid (const char *pid) {
    if (TT.matched) putchar(' ');
    fputs(pid, stdout);
    TT.matched = 1;
}

void pidof_main(void)
{
    int err;

	TT.matched = 0;

    if (!toys.optargs) exit(1);

    err = for_each_pid_with_name_in(toys.optargs, print_pid);
    if (err) exit(1);

    if (!TT.matched)
        exit(1);
    else
        putchar('\n');
}