Change license statements (and clean up headers) on some of the files that
[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 #if ENABLE_FEATURE_PIDOF_SINGLE
13 #define _SINGLE_COMPL(a) a
14 #define SINGLE (1<<0)
15 #else
16 #define _SINGLE_COMPL(a)
17 #define SINGLE (0)
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
35 int pidof_main(int argc, char **argv)
36 {
37         unsigned n = 0;
38         unsigned fail = 1;
39         unsigned long int opt;
40 #if ENABLE_FEATURE_PIDOF_OMIT
41         llist_t *omits = NULL; /* list of pids to omit */
42         bb_opt_complementally = _OMIT_COMPL("o::");
43 #endif
44
45         /* do unconditional option parsing */
46         opt = bb_getopt_ulflags(argc, argv,
47                                         _SINGLE_COMPL("s") _OMIT_COMPL("o:")
48                                         _OMIT(&omits));
49
50 #if ENABLE_FEATURE_PIDOF_OMIT
51         /* fill omit list.  */
52         {
53                 char getppid_str[32];
54                 llist_t * omits_p = omits;
55                 while (omits_p) {
56                         /* are we asked to exclude the parent's process ID?  */
57                         if (!strncmp(omits_p->data, "%PPID", 5)) {
58                                 llist_pop(&omits_p);
59                                 snprintf(getppid_str, sizeof(getppid_str), "%d", getppid());
60                                 llist_add_to(&omits_p, getppid_str);
61                         }
62                         omits_p = omits_p->link;
63                 }
64         }
65 #endif
66         /* Looks like everything is set to go.  */
67         while(optind < argc) {
68                 long *pidList;
69                 long *pl;
70
71                 /* reverse the pidlist like GNU pidof does.  */
72                 pidList = pidlist_reverse(find_pid_by_name(argv[optind]));
73                 for(pl = pidList; *pl > 0; pl++) {
74 #if ENABLE_FEATURE_PIDOF_OMIT
75                         unsigned omitted = 0;
76                         if (opt & OMIT) {
77                                 llist_t *omits_p = omits;
78                                 while (omits_p)
79                                         if (strtol(omits_p->data, NULL, 10) == *pl) {
80                                                 omitted = 1; break;
81                                         } else
82                                                 omits_p = omits_p->link;
83                         }
84 #endif
85                         if (!omitted) {
86                                 if (n) {
87                                         putchar(' ');
88                                 } else {
89                                         n = 1;
90                                 }
91                                 printf("%ld", *pl);
92                         }
93                         fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted);
94
95                         if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & SINGLE))
96                                 break;
97                 }
98                 free(pidList);
99                 optind++;
100         }
101         putchar('\n');
102
103 #if ENABLE_FEATURE_PIDOF_OMIT
104         if (ENABLE_FEATURE_CLEAN_UP)
105                 llist_free(omits, NULL);
106 #endif
107         return fail ? EXIT_FAILURE : EXIT_SUCCESS;
108 }