On Thu, 2008-06-05 at 15:58 +0200, Denys Vlasenko wrote:
> On Wednesday 04 June 2008 20:29, Natanael Copa wrote:
> > Hi,
> > 
> > Attatched is a patch for the LOWER_UP flag in 'ip link/addr' output. 
> > Useful to check if network cable is inserted or not.
> > 
> > The code does an ugly ifdef to check if IFF_LOWER_UP is declared or not. 
> > On my zenwalk desktop it was not. It looks like iproute2 maintain their 
> > own copies of the linux headers so I suppose we cannot expect the flag 
> > be there. (should mabe be in one of the include files rather than in the 
> > .c file?)
> > 
> > The patch does increase the size a bit due to a macro. Maybe should have 
> > a config option for it?
> > 
> > on my hardened uclibc i386 build:
> > 
> > function                                             old     new   delta
> > ipaddr_list_or_flush                                2929    2981     +52
> > .rodata                                             7677    7683      +6
> > ------------------------------------------------------------------------------
> > (add/remove: 0/0 grow/shrink: 2/0 up/down: 58/0)               Total: 58 
> > bytes
> >    text       data        bss        dec        hex    filename
> >   47498        670       4244      52412       ccbc    busybox_old
> >   47556        670       4244      52470       ccf6    busybox_unstripped
> 
> Applied, thanks. (It also brought my attention to ~300+ bytes
> of bloat nearby, which I removed).

oh.... I guess we were working on the same thing then.

I'm attatching what I was looking at. I guess it does not apply anymore.

# make bloatcheck
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.ms                                              -      64     +64
static.ultra_modes1                                    -      56     +56
static.pm                                              -      32     +32
static.pio_modes                                       -      32     +32
static.dflags                                          -      32     +32
.rodata                                            31778   31768     -10
xbsd_print_disklabel                                 927     892     -35
arp_main                                            2474    2439     -35
ipaddr_list_or_flush                                2687    2455    -232
process_dev                                         4446       -   -4446
------------------------------------------------------------------------------
(add/remove: 9/1 grow/shrink: 1/4 up/down: 4432/-4758)       Total: -326
bytes
   text    data     bss     dec     hex filename
 135105    2173    4500  141778   229d2 busybox_old
 134715    2709    4500  141924   22a64 busybox_unstripped


> --
> vda

-nc
Index: networking/arp.c
===================================================================
--- networking/arp.c	(revision 22223)
+++ networking/arp.c	(working copy)
@@ -314,7 +314,18 @@
 		 char *hwa, char *mask, char *dev)
 {
 	const struct hwtype *xhw;
-
+	static const struct mask_string pm[] = {
+		{ ATF_PERM, "PERM" },
+		{ ATF_PUBL, "PUP" },
+#ifdef HAVE_ATF_MAGIC
+		{ ATF_MAGIC, "AUTO" },
+#endif
+#ifdef HAVE_ATF_DONTPUB
+		{ ATF_DONTPUB, "DONTPUB" },
+#endif
+		{ ATF_USETRAILERS, "TRAIL" },
+		{ 0, NULL }
+	};
 	xhw = get_hwntype(type);
 	if (xhw == NULL)
 		xhw = get_hwtype(DFLT_HW);
@@ -333,22 +344,8 @@
 	if (arp_flags & ATF_NETMASK)
 		printf("netmask %s ", mask);
 
-	if (arp_flags & ATF_PERM)
-		printf("PERM ");
-	if (arp_flags & ATF_PUBL)
-		printf("PUP ");
-#ifdef HAVE_ATF_MAGIC
-	if (arp_flags & ATF_MAGIC)
-		printf("AUTO ");
-#endif
-#ifdef HAVE_ATF_DONTPUB
-	if (arp_flags & ATF_DONTPUB)
-		printf("DONTPUB ");
-#endif
-	if (arp_flags & ATF_USETRAILERS)
-		printf("TRAIL ");
-
-	printf("on %s\n", dev);
+	print_flags(pm, arp_flags, " ");
+	printf(" on %s\n", dev);
 }
 
 /* Display the contents of the ARP cache in the kernel. */
Index: networking/libiproute/ipaddress.c
===================================================================
--- networking/libiproute/ipaddress.c	(revision 22223)
+++ networking/libiproute/ipaddress.c	(working copy)
@@ -18,6 +18,10 @@
 #include "rt_names.h"
 #include "utils.h"
 
+#ifndef IFF_LOWER_UP
+/* from linux/if.h */
+#define IFF_LOWER_UP	0x10000		/* driver signals L1 up*/
+#endif
 
 typedef struct filter_t {
 	char *label;
@@ -41,16 +45,13 @@
 
 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);
+#define _PF(f) { .mask = IFF_##f, .string = #f }
+	static struct mask_string ms[] = {
+	_PF(LOOPBACK),
+	_PF(BROADCAST),
+	_PF(POINTOPOINT),
+	_PF(MULTICAST),
+	_PF(NOARP),
 #if 0
 	_PF(ALLMULTI);
 	_PF(PROMISC);
@@ -62,10 +63,17 @@
 	_PF(PORTSEL);
 	_PF(NOTRAILERS);
 #endif
-	_PF(UP);
+	_PF(UP),
+	_PF(LOWER_UP),
 #undef _PF
+		{ 0, NULL }
+	};
+	bb_putchar('<');
+	flags &= ~IFF_RUNNING;
+
+	flags = print_flags(ms, flags, ",");
 	if (flags)
-		printf("%x", flags);
+		printf(",%x", flags);
 	if (mdown)
 		printf(",M-DOWN");
 	printf("> ");
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: miscutils/hdparm.c
===================================================================
--- miscutils/hdparm.c	(revision 22223)
+++ 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 mask_string 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 mask_string dma_1word_modes[] = {
+				{ 0x100, "*" },
+				{ 1, "sdma0" },
+				{ 0x200, "*" },
+				{ 2, "sdma1" },
+				{ 0x400, "*" },
+				{ 4, "sdma2" },
+				{ 0xf800, "*" },
+				{ 0xf8, "sdma?" },
+				{ 0, NULL },
+			};
+			static const struct mask_string 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 mask_string 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 mask_string 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: 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"
Index: util-linux/fdisk_osf.c
===================================================================
--- util-linux/fdisk_osf.c	(revision 22223)
+++ util-linux/fdisk_osf.c	(working copy)
@@ -500,7 +500,12 @@
 	struct xbsd_disklabel *lp = &xbsd_dlabel;
 	struct xbsd_partition *pp;
 	int i, j;
-
+	static const struct mask_string dflags[] = {
+		{ BSD_D_REMOVABLE, "removable" },
+		{ BSD_D_ECC, "ecc" },
+		{ BSD_D_BADSECT, "badsect" },
+		{ 0, NULL },
+	};
 	if (show_all) {
 #if defined(__alpha__)
 		printf("# %s:\n", disk_device);
@@ -513,13 +518,8 @@
 			printf("type: %d\n", lp->d_type);
 		printf("disk: %.*s\n", (int) sizeof(lp->d_typename), lp->d_typename);
 		printf("label: %.*s\n", (int) sizeof(lp->d_packname), lp->d_packname);
-		printf("flags:");
-		if (lp->d_flags & BSD_D_REMOVABLE)
-			printf(" removable");
-		if (lp->d_flags & BSD_D_ECC)
-			printf(" ecc");
-		if (lp->d_flags & BSD_D_BADSECT)
-			printf(" badsect");
+		printf("flags: ");
+		print_flags(dflags, lp->d_flags, " "); 
 		bb_putchar('\n');
 		/* On various machines the fields of *lp are short/int/long */
 		/* In order to avoid problems, we cast them all to long. */
_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox

Reply via email to