source: MondoRescue/branches/2.2.5/mindi-busybox/procps/pidof.c@ 1765

Last change on this file since 1765 was 1765, checked in by Bruno Cornec, 16 years ago

Update to busybox 1.7.2

File size: 2.0 KB
RevLine 
[821]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 *
[1765]7 * Licensed under the GPL version 2, see the file LICENSE in this tarball.
[821]8 */
9
[1765]10#include "libbb.h"
[821]11
[1765]12enum {
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};
[821]18
[1765]19int pidof_main(int argc, char **argv);
[821]20int pidof_main(int argc, char **argv)
21{
[1765]22 unsigned first = 1;
23 unsigned opt;
[821]24#if ENABLE_FEATURE_PIDOF_OMIT
[1765]25 char ppid_str[sizeof(int)*3 + 1];
[821]26 llist_t *omits = NULL; /* list of pids to omit */
[1765]27 opt_complementary = "o::";
[821]28#endif
29
30 /* do unconditional option parsing */
[1765]31 opt = getopt32(argv, ""
32 USE_FEATURE_PIDOF_SINGLE ("s")
33 USE_FEATURE_PIDOF_OMIT("o:", &omits));
[821]34
35#if ENABLE_FEATURE_PIDOF_OMIT
36 /* fill omit list. */
37 {
[1765]38 llist_t *omits_p = omits;
[821]39 while (omits_p) {
40 /* are we asked to exclude the parent's process ID? */
[1765]41 if (strcmp(omits_p->data, "%PPID") == 0) {
42 sprintf(ppid_str, "%u", (unsigned)getppid());
43 omits_p->data = ppid_str;
[821]44 }
45 omits_p = omits_p->link;
46 }
47 }
48#endif
49 /* Looks like everything is set to go. */
[1765]50 while (optind < argc) {
51 pid_t *pidList;
52 pid_t *pl;
[821]53
54 /* reverse the pidlist like GNU pidof does. */
55 pidList = pidlist_reverse(find_pid_by_name(argv[optind]));
[1765]56 for (pl = pidList; *pl; pl++) {
[821]57#if ENABLE_FEATURE_PIDOF_OMIT
[1765]58 if (opt & OPT_OMIT) {
[821]59 llist_t *omits_p = omits;
[1765]60 while (omits_p) {
61 if (xatoul(omits_p->data) == *pl) {
62 goto omitting;
63 }
64 omits_p = omits_p->link;
65 }
[821]66 }
67#endif
[1765]68 printf(" %u" + first, (unsigned)*pl);
69 first = 0;
70 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE))
[821]71 break;
[1765]72#if ENABLE_FEATURE_PIDOF_OMIT
73 omitting: ;
74#endif
[821]75 }
76 free(pidList);
77 optind++;
78 }
79 putchar('\n');
80
81#if ENABLE_FEATURE_PIDOF_OMIT
82 if (ENABLE_FEATURE_CLEAN_UP)
83 llist_free(omits, NULL);
84#endif
[1765]85 return first; /* 1 (failure) - no processes found */
[821]86}
Note: See TracBrowser for help on using the repository browser.