add ext4,vfat and tar.bz2
[u-tools.git] / u-tools / apps / busybox / procps / pidof.c
blob7805044330948efea5e26a73ea78cc260aa04315
1 /* vi: set sw=4 ts=4: */
2 /*
3 * pidof implementation for busybox
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under the GPL version 2, see the file LICENSE in this tarball.
8 */
10 #include "libbb.h"
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,
19 int pidof_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
20 int pidof_main(int argc UNUSED_PARAM, char **argv)
22 unsigned first = 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
29 /* do unconditional option parsing */
30 opt = getopt32(argv, ""
31 USE_FEATURE_PIDOF_SINGLE ("s")
32 USE_FEATURE_PIDOF_OMIT("o:", &omits));
34 #if ENABLE_FEATURE_PIDOF_OMIT
35 /* fill omit list. */
37 llist_t *omits_p = omits;
38 while (omits_p) {
39 /* are we asked to exclude the parent's process ID? */
40 if (strcmp(omits_p->data, "%PPID") == 0) {
41 omits_p->data = utoa((unsigned)getppid());
43 omits_p = omits_p->link;
46 #endif
47 /* Looks like everything is set to go. */
48 argv += optind;
49 while (*argv) {
50 pid_t *pidList;
51 pid_t *pl;
53 /* reverse the pidlist like GNU pidof does. */
54 pidList = pidlist_reverse(find_pid_by_name(*argv));
55 for (pl = pidList; *pl; pl++) {
56 #if ENABLE_FEATURE_PIDOF_OMIT
57 if (opt & OPT_OMIT) {
58 llist_t *omits_p = omits;
59 while (omits_p) {
60 if (xatoul(omits_p->data) == (unsigned long)(*pl)) {
61 goto omitting;
63 omits_p = omits_p->link;
66 #endif
67 printf(" %u" + first, (unsigned)*pl);
68 first = 0;
69 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE))
70 break;
71 #if ENABLE_FEATURE_PIDOF_OMIT
72 omitting: ;
73 #endif
75 free(pidList);
76 argv++;
78 if (!first)
79 bb_putchar('\n');
81 #if ENABLE_FEATURE_PIDOF_OMIT
82 if (ENABLE_FEATURE_CLEAN_UP)
83 llist_free(omits, NULL);
84 #endif
85 return first; /* 1 (failure) - no processes found */