1 /* vi: set sw=4 ts=4: */
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.
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);
20 int pidof_main(int argc, char **argv)
25 #if ENABLE_FEATURE_PIDOF_OMIT
26 llist_t *omits = NULL; /* list of pids to omit */
27 opt_complementary = "o::";
30 /* do unconditional option parsing */
31 opt = getopt32(argc, argv, ""
32 USE_FEATURE_PIDOF_SINGLE ("s")
33 USE_FEATURE_PIDOF_OMIT("o:", &omits));
35 #if ENABLE_FEATURE_PIDOF_OMIT
38 char getppid_str[sizeof(int)*3 + 1];
39 llist_t * omits_p = omits;
41 /* are we asked to exclude the parent's process ID? */
42 if (!strncmp(omits_p->data, "%PPID", 5)) {
44 snprintf(getppid_str, sizeof(getppid_str), "%u", (unsigned)getppid());
45 llist_add_to(&omits_p, getppid_str);
47 omits_p = omits_p->link;
51 /* Looks like everything is set to go. */
52 while (optind < argc) {
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
62 llist_t *omits_p = omits;
64 if (xatoul(omits_p->data) == *pl) {
68 omits_p = omits_p->link;
73 printf(" %u" + first, (unsigned)*pl);
76 fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted);
78 if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE))
86 #if ENABLE_FEATURE_PIDOF_OMIT
87 if (ENABLE_FEATURE_CLEAN_UP)
88 llist_free(omits, NULL);
90 return fail ? EXIT_FAILURE : EXIT_SUCCESS;