svn commit: trunk/busybox/procps

vda at busybox.net vda at busybox.net
Wed Nov 1 01:17:48 PST 2006


Author: vda
Date: 2006-11-01 01:17:47 -0800 (Wed, 01 Nov 2006)
New Revision: 16486

Log:
pidof: reduce #ifdef forest


Modified:
   trunk/busybox/procps/pidof.c


Changeset:
Modified: trunk/busybox/procps/pidof.c
===================================================================
--- trunk/busybox/procps/pidof.c	2006-11-01 09:16:49 UTC (rev 16485)
+++ trunk/busybox/procps/pidof.c	2006-11-01 09:17:47 UTC (rev 16486)
@@ -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
+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,
+};
 
-#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)
 {
-	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 @@
 		/* 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 @@
 			}
 #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);



More information about the busybox-cvs mailing list