Hi,

I have looked more at the print_flags() I started with yesterday.

Attatched are some patches and they should probably be committed to svn
separatly (easier to revert in case they have bugs).

I tried to use print_flags() in arp.c and fdisk.c but they seemed to
increase the size so I chosed to not included them here. (ping me if
someone want to take a look at them anyway)

Patches are rebased from svn trunk this morning.

Bloatcheck on hardened uclibc gcc-3.4.6:

function                                             old     new   delta
hdparm_main                                         1067    4916   +3849
print_flags                                            -     135    +135
static.ultra_modes2                                    -      88     +88
static.dma_mword_modes                                 -      72     +72
static.dma_1word_modes                                 -      72     +72
static.ultra_modes1                                    -      56     +56
static.link_flags                                      -      56     +56
static.pio_modes                                       -      32     +32
static.dflags                                          -      32     +32
.rodata                                            31784   31770     -14
xbsd_print_disklabel                                 927     892     -35
ipaddr_list_or_flush                                2732    2455    -277
process_dev                                         4446       -   -4446
------------------------------------------------------------------------------
(add/remove: 8/1 grow/shrink: 1/3 up/down: 4392/-4772)       Total: -380
bytes
   text    data     bss     dec     hex filename
 135156    2173    4500  141829   22a05 busybox_old
 134728    2677    4500  141905   22a51 busybox_unstripped

-nc
Index: miscutils/hdparm.c
===================================================================
--- miscutils/hdparm.c	(revision 22247)
+++ miscutils/hdparm.c	(working copy)
@@ -1228,54 +1228,76 @@
 		if (id->tPIO >= 2) printf("pio2 ");
 	}
 	if (id->field_valid & 2) {
-		if (id->eide_pio_modes & 1) printf("pio3 ");
-		if (id->eide_pio_modes & 2) printf("pio4 ");
-		if (id->eide_pio_modes &~3) printf("pio? ");
+		static const struct flag_label pio_modes[] = {
+			{ 1, "pio3 "},
+			{ 2, "pio4 "},
+			{ ~3, "pio? "},
+			{ 0, NULL },
+		};
+		print_flags(pio_modes, id->eide_pio_modes, "");
 	}
 	if (id->capability & 1) {
 		if (id->dma_1word | id->dma_mword) {
+			static const struct flag_label dma_1word_modes[] = {
+				{ 0x100, "*" },
+				{ 1, "sdma0" },
+				{ 0x200, "*" },
+				{ 2, "sdma1" },
+				{ 0x400, "*" },
+				{ 4, "sdma2" },
+				{ 0xf800, "*" },
+				{ 0xf8, "sdma?" },
+				{ 0, NULL },
+			};
+			static const struct flag_label dma_mword_modes[] = {
+				{ 0x100, "*" },
+				{ 1, "mdma0 " },
+				{ 0x200, "*" },
+				{ 2, "mdma1 " },
+				{ 0x400, "*" },
+				{ 4, "mdma2 " },
+				{ 0xf800, "*" },
+				{ 0xf8, "mdma?" },
+				{ 0, NULL },
+			};
+
 			printf("\n DMA modes:  ");
-			if (id->dma_1word & 0x100) bb_putchar('*');
-			if (id->dma_1word & 1) printf("sdma0 ");
-			if (id->dma_1word & 0x200) bb_putchar('*');
-			if (id->dma_1word & 2) printf("sdma1 ");
-			if (id->dma_1word & 0x400) bb_putchar('*');
-			if (id->dma_1word & 4) printf("sdma2 ");
-			if (id->dma_1word & 0xf800) bb_putchar('*');
-			if (id->dma_1word & 0xf8) printf("sdma? ");
-			if (id->dma_mword & 0x100) bb_putchar('*');
-			if (id->dma_mword & 1) printf("mdma0 ");
-			if (id->dma_mword & 0x200) bb_putchar('*');
-			if (id->dma_mword & 2) printf("mdma1 ");
-			if (id->dma_mword & 0x400) bb_putchar('*');
-			if (id->dma_mword & 4) printf("mdma2 ");
-			if (id->dma_mword & 0xf800) bb_putchar('*');
-			if (id->dma_mword & 0xf8) printf("mdma? ");
+			print_flags(dma_1word_modes, id->dma_1word, "");
+			print_flags(dma_mword_modes, id->dma_mword, "");
 		}
 	}
 	if (((id->capability & 8) || (id->field_valid & 2)) && id->field_valid & 4) {
+		static const struct flag_label ultra_modes1[] = {
+			{ 0x100, "*" },
+			{ 0x001, "udma0 " },
+			{ 0x200, "*" },
+			{ 0x002, "udma1 " },
+			{ 0x400, "*" },
+			{ 0x004, "udma2 " },
+			{ 0, NULL },
+		};
+			
 		printf("\n UDMA modes: ");
-		if (id->dma_ultra & 0x100) bb_putchar('*');
-		if (id->dma_ultra & 0x001) printf("udma0 ");
-		if (id->dma_ultra & 0x200) bb_putchar('*');
-		if (id->dma_ultra & 0x002) printf("udma1 ");
-		if (id->dma_ultra & 0x400) bb_putchar('*');
-		if (id->dma_ultra & 0x004) printf("udma2 ");
+		print_flags(ultra_modes1, id->dma_ultra, "");
 #ifdef __NEW_HD_DRIVE_ID
 		if (id->hw_config & 0x2000) {
 #else /* !__NEW_HD_DRIVE_ID */
 		if (id->word93 & 0x2000) {
 #endif /* __NEW_HD_DRIVE_ID */
-			if (id->dma_ultra & 0x0800) bb_putchar('*');
-			if (id->dma_ultra & 0x0008) printf("udma3 ");
-			if (id->dma_ultra & 0x1000) bb_putchar('*');
-			if (id->dma_ultra & 0x0010) printf("udma4 ");
-			if (id->dma_ultra & 0x2000) bb_putchar('*');
-			if (id->dma_ultra & 0x0020) printf("udma5 ");
-			if (id->dma_ultra & 0x4000) bb_putchar('*');
-			if (id->dma_ultra & 0x0040) printf("udma6 ");
-			if (id->dma_ultra & 0x8000) bb_putchar('*');
-			if (id->dma_ultra & 0x0080) printf("udma7 ");
+			static struct flag_label ultra_modes2[] = {
+				{ 0x0800,"*" },
+				{ 0x0008,"udma3 " },
+				{ 0x1000,"*" },
+				{ 0x0010,"udma4 " },
+				{ 0x2000,"*" },
+				{ 0x0020,"udma5 " },
+				{ 0x4000,"*" },
+				{ 0x0040,"udma6 " },
+				{ 0x8000,"*" },
+				{ 0x0080,"udma7 " },
+				{ 0, NULL },
+			};
+			print_flags(ultra_modes2, id->dma_ultra, "");
 		}
 	}
 	printf("\n AdvancedPM=%s", (!(id_regs[83] & 8)) ? "no" : "yes");
Index: networking/libiproute/ipaddress.c
===================================================================
--- networking/libiproute/ipaddress.c	(revision 22247)
+++ networking/libiproute/ipaddress.c	(working copy)
@@ -45,30 +45,31 @@
 
 static void print_link_flags(unsigned flags, unsigned mdown)
 {
-	bb_putchar('<');
-	flags &= ~IFF_RUNNING;
-#define _PF(f) if (flags & IFF_##f) { \
-		  flags &= ~IFF_##f; \
-		  printf(#f "%s", flags ? "," : ""); }
-	_PF(LOOPBACK);
-	_PF(BROADCAST);
-	_PF(POINTOPOINT);
-	_PF(MULTICAST);
-	_PF(NOARP);
+	static const struct flag_label link_flags[] = {
+#define _PF(f) { IFF_##f, #f }
+		_PF(LOOPBACK),
+		_PF(BROADCAST),
+		_PF(POINTOPOINT),
+		_PF(MULTICAST),
+		_PF(NOARP),
 #if 0
-	_PF(ALLMULTI);
-	_PF(PROMISC);
-	_PF(MASTER);
-	_PF(SLAVE);
-	_PF(DEBUG);
-	_PF(DYNAMIC);
-	_PF(AUTOMEDIA);
-	_PF(PORTSEL);
-	_PF(NOTRAILERS);
+		_PF(ALLMULTI),
+		_PF(PROMISC),
+		_PF(MASTER),
+		_PF(SLAVE),
+		_PF(DEBUG),
+		_PF(DYNAMIC),
+		_PF(AUTOMEDIA),
+		_PF(PORTSEL),
+		_PF(NOTRAILERS),
 #endif
-	_PF(UP);
-	_PF(LOWER_UP);
+		_PF(UP),
+		_PF(LOWER_UP),
 #undef _PF
+	};
+	bb_putchar('<');
+	flags &= ~IFF_RUNNING;
+	flags = print_flags(link_flags, flags, ",");
 	if (flags)
 		printf("%x", flags);
 	if (mdown)
Index: libbb/print_flags.c
===================================================================
--- libbb/print_flags.c	(revision 0)
+++ libbb/print_flags.c	(revision 0)
@@ -0,0 +1,26 @@
+/* vi: set sw=4 ts=4: */
+/* Print string that matches bit masked flags
+ *
+ * Copyright (C) 2008 Natanael Copa <[EMAIL PROTECTED]>
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ */
+
+#include <libbb.h>
+
+/* returns a set with the flags not printed */
+int print_flags(const struct mask_string *ms, int flags, const char *separator)
+{
+	int need_separator = 0;
+	while (ms->string) {
+		if (flags & ms->mask) {
+			printf("%s%s", need_separator ? separator : "",
+				ms->string);
+			need_separator++;
+			flags &= ~ms->mask;
+		}
+		ms++;
+	}
+	return flags;
+}
+
Index: libbb/Kbuild
===================================================================
--- libbb/Kbuild	(revision 22223)
+++ libbb/Kbuild	(working copy)
@@ -68,6 +68,7 @@
 lib-y += perror_nomsg_and_die.o
 lib-y += pidfile.o
 lib-y += printable.o
+lib-y += print_flags.o
 lib-y += process_escape_sequence.o
 lib-y += procps.o
 lib-y += ptr_to_globals.o
Index: include/libbb.h
===================================================================
--- include/libbb.h	(revision 22223)
+++ include/libbb.h	(working copy)
@@ -1304,7 +1304,14 @@
 /* "sh" */
 #define DEFAULT_SHELL_SHORT_NAME     (bb_default_login_shell+6)
 
+struct mask_string {
+	int mask;
+	const char *string;
+};
 
+extern int print_flags(const struct mask_string *ms, int flags, const char *separator);
+
+
 #if ENABLE_FEATURE_DEVFS
 # define CURRENT_VC "/dev/vc/0"
 # define VC_1 "/dev/vc/1"
_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox

Reply via email to