aboutsummaryrefslogtreecommitdiff
path: root/procps/pidof.c
diff options
context:
space:
mode:
authoraldot <aldot@69ca8d6d-28ef-0310-b511-8ec308f3f277>2005-10-06 15:37:02 +0000
committeraldot <aldot@69ca8d6d-28ef-0310-b511-8ec308f3f277>2005-10-06 15:37:02 +0000
commit40235c8154a53a749956b46b44b760e6ae0e15fa (patch)
treea74704314c1dcffbd472702e265966678a986a7c /procps/pidof.c
parent18c5544ad99fde3af59ac8b4ca690cf27c952d2b (diff)
downloadbusybox-w32-40235c8154a53a749956b46b44b760e6ae0e15fa.tar.gz
busybox-w32-40235c8154a53a749956b46b44b760e6ae0e15fa.tar.bz2
busybox-w32-40235c8154a53a749956b46b44b760e6ae0e15fa.zip
- pidof(8): make -s optional and optional -o; closes #168
first cut. git-svn-id: svn://busybox.net/trunk/busybox@11793 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'procps/pidof.c')
-rw-r--r--procps/pidof.c126
1 files changed, 96 insertions, 30 deletions
diff --git a/procps/pidof.c b/procps/pidof.c
index 413864a37..6f959e4ad 100644
--- a/procps/pidof.c
+++ b/procps/pidof.c
@@ -4,20 +4,7 @@
4 * 4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> 5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 7 * Licensed under the GPL v2, see the file LICENSE in this tarball.
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */ 8 */
22 9
23 10
@@ -31,41 +18,120 @@
31#include <unistd.h> 18#include <unistd.h>
32#include "busybox.h" 19#include "busybox.h"
33 20
21#if ENABLE_FEATURE_PIDOF_SINGLE
22#define _SINGLE_COMPL(a) a
23#define SINGLE (1<<0)
24#else
25#define _SINGLE_COMPL(a)
26#define SINGLE (0)
27#endif
28
29#if ENABLE_FEATURE_PIDOF_OMIT
30#define _OMIT_COMPL(a) a
31#define _OMIT(a) ,a
32static llist_t *omits; /* list of pids to omit */
33#if ENABLE_FEATURE_PIDOF_SINGLE
34#define OMIT (1<<1)
35#else
36#define OMIT (1<<0)
37#endif
38#else
39#define _OMIT_COMPL(a) ""
40#define _OMIT(a)
41#define OMIT (0)
42#endif
34 43
35extern int pidof_main(int argc, char **argv) 44extern int pidof_main(int argc, char **argv)
36{ 45{
37 int opt, n = 0; 46 int n = 0;
38 int single_flag = 0;
39 int fail = 1; 47 int fail = 1;
48 unsigned long int opt;
49#if ENABLE_FEATURE_PIDOF_OMIT
50 bb_opt_complementally = _OMIT_COMPL("o*");
51#endif
40 52
41 /* do normal option parsing */ 53 /* do option parsing */
42 while ((opt = getopt(argc, argv, "s")) > 0) { 54 if ((opt = bb_getopt_ulflags(argc, argv,
43 switch (opt) { 55 _SINGLE_COMPL("s") _OMIT_COMPL("o:")
44 case 's': 56 _OMIT(&omits)))
45 single_flag = 1; 57 > 0) {
46 break; 58#if ENABLE_FEATURE_PIDOF_SINGLE
47 default: 59 if (!(opt & SINGLE))
48 bb_show_usage(); 60#endif
49 } 61#if ENABLE_FEATURE_PIDOF_OMIT
62 if (!(opt & OMIT))
63#endif
64 bb_show_usage();
50 } 65 }
51 66
52 /* Looks like everything is set to go. */ 67#if ENABLE_FEATURE_PIDOF_OMIT
68 /* fill omit list. */
69 {
70 RESERVE_CONFIG_BUFFER(getppid_str, 32);
71 llist_t * omits_p = omits;
72 while (omits_p) {
73 /* are we asked to exclude the parent's process ID? */
74 if (omits_p->data[0] == '%') {
75 if (!strncmp(omits_p->data, "%PPID", 5)) {
76 /* yes, exclude ppid */
77 snprintf(getppid_str, sizeof(getppid_str), "%ld", getppid());
78// omits_p->data = getppid_str;
79#if 0
80 } else {
81 bb_error_msg_and_die("illegal omit pid value (%s)!\n",
82 omits_p->data);
83#endif
84 }
85 } else {
86 /* no, not talking about ppid but deal with usual case (pid). */
87 snprintf(getppid_str, sizeof(getppid_str), "%ld",
88 strtol(omits_p->data, NULL, 10));
89 }
90 omits_p->data = getppid_str;
91 omits_p = omits_p->link;
92 }
93 RELEASE_CONFIG_BUFFER(getppid_str);
94 }
95#endif
96 /* Looks like everything is set to go. */
53 while(optind < argc) { 97 while(optind < argc) {
54 long *pidList; 98 long *pidList;
55 long *pl; 99 long *pl;
56 100
57 pidList = find_pid_by_name(argv[optind]); 101 /* reverse the pidlist like GNU pidof does. */
102 pidList = pidlist_reverse(find_pid_by_name(argv[optind]));
58 for(pl = pidList; *pl > 0; pl++) { 103 for(pl = pidList; *pl > 0; pl++) {
104#if ENABLE_FEATURE_PIDOF_OMIT
105 unsigned short omitted = 0;
106 if (opt & OMIT) {
107 llist_t *omits_p = omits;
108 while (omits_p)
109 if (strtol(omits_p->data, NULL, 10) == *pl) {
110 omitted = 1; break;
111 } else
112 omits_p = omits_p->link;
113 }
114 if (!omitted)
115#endif
59 printf("%s%ld", (n++ ? " " : ""), *pl); 116 printf("%s%ld", (n++ ? " " : ""), *pl);
117#if ENABLE_FEATURE_PIDOF_OMIT
118 fail = omitted;
119#else
60 fail = 0; 120 fail = 0;
61 if (single_flag) 121#endif
122#if ENABLE_FEATURE_PIDOF_SINGLE
123 if (opt & SINGLE)
62 break; 124 break;
125#endif
63 } 126 }
64 free(pidList); 127 free(pidList);
65 optind++; 128 optind++;
66
67 } 129 }
68 printf("\n"); 130 putchar('\n');
69 131
132#if ENABLE_FEATURE_PIDOF_OMIT
133 if (ENABLE_FEATURE_CLEAN_UP)
134 llist_free(omits);
135#endif
70 return fail ? EXIT_FAILURE : EXIT_SUCCESS; 136 return fail ? EXIT_FAILURE : EXIT_SUCCESS;
71} 137}