pidof: reduce #ifdef forest
[oweals/busybox.git] / procps / pidof.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * pidof implementation for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under the GPL version 2, see the file LICENSE in this tarball.
8  */
9
10 #include "busybox.h"
11
12 enum {
13         USE_FEATURE_PIDOF_SINGLE(OPTBIT_SINGLE,)
14         USE_FEATURE_PIDOF_OMIT(  OPTBIT_OMIT  ,)
15         OPT_SINGLE = USE_FEATURE_PIDOF_SINGLE((1<<OPTBIT_SINGLE)) + 0,
16         OPT_OMIT   = USE_FEATURE_PIDOF_OMIT(  (1<<OPTBIT_OMIT  )) + 0,
17 };
18
19 int pidof_main(int argc, char **argv)
20 {
21         unsigned first = 1;
22         unsigned fail = 1;
23         unsigned opt;
24 #if ENABLE_FEATURE_PIDOF_OMIT
25         llist_t *omits = NULL; /* list of pids to omit */
26         opt_complementary = "o::";
27 #endif
28
29         /* do unconditional option parsing */
30         opt = getopt32(argc, argv, ""
31                         USE_FEATURE_PIDOF_SINGLE ("s")
32                         USE_FEATURE_PIDOF_OMIT("o:", &omits));
33
34 #if ENABLE_FEATURE_PIDOF_OMIT
35         /* fill omit list.  */
36         {
37                 char getppid_str[sizeof(int)*3 + 1];
38                 llist_t * omits_p = omits;
39                 while (omits_p) {
40                         /* are we asked to exclude the parent's process ID?  */
41                         if (!strncmp(omits_p->data, "%PPID", 5)) {
42                                 llist_pop(&omits_p);
43                                 snprintf(getppid_str, sizeof(getppid_str), "%u", (unsigned)getppid());
44                                 llist_add_to(&omits_p, getppid_str);
45                         }
46                         omits_p = omits_p->link;
47                 }
48         }
49 #endif
50         /* Looks like everything is set to go.  */
51         while (optind < argc) {
52                 pid_t *pidList;
53                 pid_t *pl;
54
55                 /* reverse the pidlist like GNU pidof does.  */
56                 pidList = pidlist_reverse(find_pid_by_name(argv[optind]));
57                 for (pl = pidList; *pl; pl++) {
58                         SKIP_FEATURE_PIDOF_OMIT(const) unsigned omitted = 0;
59 #if ENABLE_FEATURE_PIDOF_OMIT
60                         if (opt & OPT_OMIT) {
61                                 llist_t *omits_p = omits;
62                                 while (omits_p) {
63                                         if (xatoul(omits_p->data) == *pl) {
64                                                 omitted = 1;
65                                                 break;
66                                         } else
67                                                 omits_p = omits_p->link;
68                                 }
69                         }
70 #endif
71                         if (!omitted) {
72                                 printf(" %u" + first, (unsigned)*pl);
73                                 first = 0;
74                         }
75                         fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted);
76
77                         if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE))
78                                 break;
79                 }
80                 free(pidList);
81                 optind++;
82         }
83         putchar('\n');
84
85 #if ENABLE_FEATURE_PIDOF_OMIT
86         if (ENABLE_FEATURE_CLEAN_UP)
87                 llist_free(omits, NULL);
88 #endif
89         return fail ? EXIT_FAILURE : EXIT_SUCCESS;
90 }