pidof: reduce #ifdef forest

This commit is contained in:
Denis Vlasenko 2006-11-01 09:17:47 +00:00
parent 35fb512728
commit 048c93cc55

View File

@ -9,54 +9,38 @@
#include "busybox.h" #include "busybox.h"
#if ENABLE_FEATURE_PIDOF_SINGLE enum {
#define _SINGLE_COMPL(a) a USE_FEATURE_PIDOF_SINGLE(OPTBIT_SINGLE,)
#define SINGLE (1<<0) USE_FEATURE_PIDOF_OMIT( OPTBIT_OMIT ,)
#else OPT_SINGLE = USE_FEATURE_PIDOF_SINGLE((1<<OPTBIT_SINGLE)) + 0,
#define _SINGLE_COMPL(a) OPT_OMIT = USE_FEATURE_PIDOF_OMIT( (1<<OPTBIT_OMIT )) + 0,
#define SINGLE 0 };
#endif
#if ENABLE_FEATURE_PIDOF_OMIT
# define _OMIT_COMPL(a) a
# define _OMIT(a) ,a
# if ENABLE_FEATURE_PIDOF_SINGLE
# define OMIT (1<<1)
# else
# define OMIT (1<<0)
# endif
#else
# define _OMIT_COMPL(a) ""
# define _OMIT(a)
# define OMIT (0)
# define omitted (0)
#endif
int pidof_main(int argc, char **argv) int pidof_main(int argc, char **argv)
{ {
unsigned n = 0; unsigned first = 1;
unsigned fail = 1; unsigned fail = 1;
unsigned long int opt; unsigned opt;
#if ENABLE_FEATURE_PIDOF_OMIT #if ENABLE_FEATURE_PIDOF_OMIT
llist_t *omits = NULL; /* list of pids to omit */ llist_t *omits = NULL; /* list of pids to omit */
opt_complementary = _OMIT_COMPL("o::"); opt_complementary = "o::";
#endif #endif
/* do unconditional option parsing */ /* do unconditional option parsing */
opt = getopt32(argc, argv, opt = getopt32(argc, argv, ""
_SINGLE_COMPL("s") _OMIT_COMPL("o:") USE_FEATURE_PIDOF_SINGLE ("s")
_OMIT(&omits)); USE_FEATURE_PIDOF_OMIT("o:", &omits));
#if ENABLE_FEATURE_PIDOF_OMIT #if ENABLE_FEATURE_PIDOF_OMIT
/* fill omit list. */ /* fill omit list. */
{ {
char getppid_str[32]; char getppid_str[sizeof(int)*3 + 1];
llist_t * omits_p = omits; llist_t * omits_p = omits;
while (omits_p) { while (omits_p) {
/* are we asked to exclude the parent's process ID? */ /* are we asked to exclude the parent's process ID? */
if (!strncmp(omits_p->data, "%PPID", 5)) { if (!strncmp(omits_p->data, "%PPID", 5)) {
llist_pop(&omits_p); llist_pop(&omits_p);
snprintf(getppid_str, sizeof(getppid_str), "%ld", (long)getppid()); snprintf(getppid_str, sizeof(getppid_str), "%u", (unsigned)getppid());
llist_add_to(&omits_p, getppid_str); llist_add_to(&omits_p, getppid_str);
} }
omits_p = omits_p->link; omits_p = omits_p->link;
@ -71,9 +55,9 @@ int pidof_main(int argc, char **argv)
/* reverse the pidlist like GNU pidof does. */ /* reverse the pidlist like GNU pidof does. */
pidList = pidlist_reverse(find_pid_by_name(argv[optind])); pidList = pidlist_reverse(find_pid_by_name(argv[optind]));
for (pl = pidList; *pl; pl++) { for (pl = pidList; *pl; pl++) {
SKIP_FEATURE_PIDOF_OMIT(const) unsigned omitted = 0;
#if ENABLE_FEATURE_PIDOF_OMIT #if ENABLE_FEATURE_PIDOF_OMIT
unsigned omitted = 0; if (opt & OPT_OMIT) {
if (opt & OMIT) {
llist_t *omits_p = omits; llist_t *omits_p = omits;
while (omits_p) { while (omits_p) {
if (xatoul(omits_p->data) == *pl) { if (xatoul(omits_p->data) == *pl) {
@ -85,16 +69,12 @@ int pidof_main(int argc, char **argv)
} }
#endif #endif
if (!omitted) { if (!omitted) {
if (n) { printf(" %u" + first, (unsigned)*pl);
putchar(' '); first = 0;
} else {
n = 1;
}
printf("%u", (unsigned)*pl);
} }
fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted); fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted);
if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & SINGLE)) if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE))
break; break;
} }
free(pidList); free(pidList);