pidof: reduce #ifdef forest
authorDenis Vlasenko <vda.linux@googlemail.com>
Wed, 1 Nov 2006 09:17:47 +0000 (09:17 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Wed, 1 Nov 2006 09:17:47 +0000 (09:17 -0000)
procps/pidof.c

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