Mercurial > hg > toybox
comparison toys/lsb/pidof.c @ 744:43e6ec52aa29
Adding -s (single shot) and -o (omit pids) options to pidof
author | Elie De Brauwer <eliedebrauwer@gmail.com> |
---|---|
date | Sat, 08 Dec 2012 20:10:05 +0100 |
parents | 786841fdb1e0 |
children | 0faab963ea92 |
comparison
equal
deleted
inserted
replaced
743:54e0818da46d | 744:43e6ec52aa29 |
---|---|
1 /* pidof.c - Print the Process IDs of all processes with the given names. | 1 /* pidof.c - Print the Process IDs of all processes with the given names. |
2 * | 2 * |
3 * Copyright 2012 Andreas Heck <aheck@gmx.de> | 3 * Copyright 2012 Andreas Heck <aheck@gmx.de> |
4 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com> | |
4 * | 5 * |
5 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/pidof.html | 6 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/pidof.html |
6 | 7 |
7 USE_PIDOF(NEWTOY(pidof, "<1", TOYFLAG_USR|TOYFLAG_BIN)) | 8 USE_PIDOF(NEWTOY(pidof, "so:<1", TOYFLAG_USR|TOYFLAG_BIN)) |
8 | 9 |
9 config PIDOF | 10 config PIDOF |
10 bool "pidof" | 11 bool "pidof" |
11 default y | 12 default y |
12 help | 13 help |
13 usage: pidof [NAME]... | 14 usage: pidof [-s] [-o omitpid[,omitpid..]] [NAME]... |
14 | 15 |
15 Print the PIDs of all processes with the given names. | 16 Print the PIDs of all processes with the given names. |
17 -s single shot, only return one pid. | |
18 -o omits processes with specified PID | |
16 */ | 19 */ |
17 | 20 |
21 #define FOR_pidof | |
18 #include "toys.h" | 22 #include "toys.h" |
19 | 23 |
20 static void print_pid(pid_t pid) | 24 GLOBALS( |
25 char *omit; | |
26 ) | |
27 | |
28 static int print_pid(pid_t pid) | |
21 { | 29 { |
30 | |
31 if (toys.optflags & FLAG_o) | |
32 { | |
33 char * res; | |
34 int len; | |
35 snprintf(toybuf, sizeof(toybuf), "%d", pid); | |
36 len = strlen(toybuf); | |
37 res = strstr(TT.omit, toybuf); | |
38 if (res && | |
39 (res == TT.omit || res[-1] == ',') && | |
40 (res[len] == ',' || res[len] == 0)) | |
41 // Found in omit string | |
42 return 1; | |
43 } | |
44 | |
22 xprintf("%s%ld", toys.exitval ? "" : " ", (long)pid); | 45 xprintf("%s%ld", toys.exitval ? "" : " ", (long)pid); |
23 toys.exitval = 0; | 46 toys.exitval = 0; |
47 | |
48 if (toys.optflags & FLAG_s) | |
49 return 0; | |
50 | |
51 return 1; | |
24 } | 52 } |
25 | 53 |
26 void pidof_main(void) | 54 void pidof_main(void) |
27 { | 55 { |
28 toys.exitval = 1; | 56 toys.exitval = 1; |