suppress warnings about easch <applet>_main() having
[platform/upstream/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 int pidof_main(int argc, char **argv)
21 {
22         unsigned first = 1;
23         unsigned fail = 1;
24         unsigned opt;
25 #if ENABLE_FEATURE_PIDOF_OMIT
26         llist_t *omits = NULL; /* list of pids to omit */
27         opt_complementary = "o::";
28 #endif
29
30         /* do unconditional option parsing */
31         opt = getopt32(argc, argv, ""
32                         USE_FEATURE_PIDOF_SINGLE ("s")
33                         USE_FEATURE_PIDOF_OMIT("o:", &omits));
34
35 #if ENABLE_FEATURE_PIDOF_OMIT
36         /* fill omit list.  */
37         {
38                 char getppid_str[sizeof(int)*3 + 1];
39                 llist_t * omits_p = omits;
40                 while (omits_p) {
41                         /* are we asked to exclude the parent's process ID?  */
42                         if (!strncmp(omits_p->data, "%PPID", 5)) {
43                                 llist_pop(&omits_p);
44                                 snprintf(getppid_str, sizeof(getppid_str), "%u", (unsigned)getppid());
45                                 llist_add_to(&omits_p, getppid_str);
46                         }
47                         omits_p = omits_p->link;
48                 }
49         }
50 #endif
51         /* Looks like everything is set to go.  */
52         while (optind < argc) {
53                 pid_t *pidList;
54                 pid_t *pl;
55
56                 /* reverse the pidlist like GNU pidof does.  */
57                 pidList = pidlist_reverse(find_pid_by_name(argv[optind]));
58                 for (pl = pidList; *pl; pl++) {
59                         SKIP_FEATURE_PIDOF_OMIT(const) unsigned omitted = 0;
60 #if ENABLE_FEATURE_PIDOF_OMIT
61                         if (opt & OPT_OMIT) {
62                                 llist_t *omits_p = omits;
63                                 while (omits_p) {
64                                         if (xatoul(omits_p->data) == *pl) {
65                                                 omitted = 1;
66                                                 break;
67                                         } else
68                                                 omits_p = omits_p->link;
69                                 }
70                         }
71 #endif
72                         if (!omitted) {
73                                 printf(" %u" + first, (unsigned)*pl);
74                                 first = 0;
75                         }
76                         fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted);
77
78                         if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE))
79                                 break;
80                 }
81                 free(pidList);
82                 optind++;
83         }
84         putchar('\n');
85
86 #if ENABLE_FEATURE_PIDOF_OMIT
87         if (ENABLE_FEATURE_CLEAN_UP)
88                 llist_free(omits, NULL);
89 #endif
90         return fail ? EXIT_FAILURE : EXIT_SUCCESS;
91 }