aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--procps/pidof.c58
1 files changed, 19 insertions, 39 deletions
diff --git a/procps/pidof.c b/procps/pidof.c
index 28c5c04e2..1d9718945 100644
--- a/procps/pidof.c
+++ b/procps/pidof.c
@@ -9,54 +9,38 @@
9 9
10#include "busybox.h" 10#include "busybox.h"
11 11
12#if ENABLE_FEATURE_PIDOF_SINGLE 12enum {
13#define _SINGLE_COMPL(a) a 13 USE_FEATURE_PIDOF_SINGLE(OPTBIT_SINGLE,)
14#define SINGLE (1<<0) 14 USE_FEATURE_PIDOF_OMIT( OPTBIT_OMIT ,)
15#else 15 OPT_SINGLE = USE_FEATURE_PIDOF_SINGLE((1<<OPTBIT_SINGLE)) + 0,
16#define _SINGLE_COMPL(a) 16 OPT_OMIT = USE_FEATURE_PIDOF_OMIT( (1<<OPTBIT_OMIT )) + 0,
17#define SINGLE 0 17};
18#endif
19
20#if ENABLE_FEATURE_PIDOF_OMIT
21# define _OMIT_COMPL(a) a
22# define _OMIT(a) ,a
23# if ENABLE_FEATURE_PIDOF_SINGLE
24# define OMIT (1<<1)
25# else
26# define OMIT (1<<0)
27# endif
28#else
29# define _OMIT_COMPL(a) ""
30# define _OMIT(a)
31# define OMIT (0)
32# define omitted (0)
33#endif
34 18
35int pidof_main(int argc, char **argv) 19int pidof_main(int argc, char **argv)
36{ 20{
37 unsigned n = 0; 21 unsigned first = 1;
38 unsigned fail = 1; 22 unsigned fail = 1;
39 unsigned long int opt; 23 unsigned opt;
40#if ENABLE_FEATURE_PIDOF_OMIT 24#if ENABLE_FEATURE_PIDOF_OMIT
41 llist_t *omits = NULL; /* list of pids to omit */ 25 llist_t *omits = NULL; /* list of pids to omit */
42 opt_complementary = _OMIT_COMPL("o::"); 26 opt_complementary = "o::";
43#endif 27#endif
44 28
45 /* do unconditional option parsing */ 29 /* do unconditional option parsing */
46 opt = getopt32(argc, argv, 30 opt = getopt32(argc, argv, ""
47 _SINGLE_COMPL("s") _OMIT_COMPL("o:") 31 USE_FEATURE_PIDOF_SINGLE ("s")
48 _OMIT(&omits)); 32 USE_FEATURE_PIDOF_OMIT("o:", &omits));
49 33
50#if ENABLE_FEATURE_PIDOF_OMIT 34#if ENABLE_FEATURE_PIDOF_OMIT
51 /* fill omit list. */ 35 /* fill omit list. */
52 { 36 {
53 char getppid_str[32]; 37 char getppid_str[sizeof(int)*3 + 1];
54 llist_t * omits_p = omits; 38 llist_t * omits_p = omits;
55 while (omits_p) { 39 while (omits_p) {
56 /* are we asked to exclude the parent's process ID? */ 40 /* are we asked to exclude the parent's process ID? */
57 if (!strncmp(omits_p->data, "%PPID", 5)) { 41 if (!strncmp(omits_p->data, "%PPID", 5)) {
58 llist_pop(&omits_p); 42 llist_pop(&omits_p);
59 snprintf(getppid_str, sizeof(getppid_str), "%ld", (long)getppid()); 43 snprintf(getppid_str, sizeof(getppid_str), "%u", (unsigned)getppid());
60 llist_add_to(&omits_p, getppid_str); 44 llist_add_to(&omits_p, getppid_str);
61 } 45 }
62 omits_p = omits_p->link; 46 omits_p = omits_p->link;
@@ -71,9 +55,9 @@ int pidof_main(int argc, char **argv)
71 /* reverse the pidlist like GNU pidof does. */ 55 /* reverse the pidlist like GNU pidof does. */
72 pidList = pidlist_reverse(find_pid_by_name(argv[optind])); 56 pidList = pidlist_reverse(find_pid_by_name(argv[optind]));
73 for (pl = pidList; *pl; pl++) { 57 for (pl = pidList; *pl; pl++) {
58 SKIP_FEATURE_PIDOF_OMIT(const) unsigned omitted = 0;
74#if ENABLE_FEATURE_PIDOF_OMIT 59#if ENABLE_FEATURE_PIDOF_OMIT
75 unsigned omitted = 0; 60 if (opt & OPT_OMIT) {
76 if (opt & OMIT) {
77 llist_t *omits_p = omits; 61 llist_t *omits_p = omits;
78 while (omits_p) { 62 while (omits_p) {
79 if (xatoul(omits_p->data) == *pl) { 63 if (xatoul(omits_p->data) == *pl) {
@@ -85,16 +69,12 @@ int pidof_main(int argc, char **argv)
85 } 69 }
86#endif 70#endif
87 if (!omitted) { 71 if (!omitted) {
88 if (n) { 72 printf(" %u" + first, (unsigned)*pl);
89 putchar(' '); 73 first = 0;
90 } else {
91 n = 1;
92 }
93 printf("%u", (unsigned)*pl);
94 } 74 }
95 fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted); 75 fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted);
96 76
97 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & SINGLE)) 77 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE))
98 break; 78 break;
99 } 79 }
100 free(pidList); 80 free(pidList);