[BusyBox] hexdump -C [PATCH]

Shaun Jackman sjackman at gmail.com
Tue Jul 19 23:37:21 UTC 2005


This patch adds the -C feature to hexdump and fixes a bug in dump.c
(bpad) that affects the -C switch. It also reduces overall code size
by 80 bytes on my system because it recodes bpad to use library
functions.

Cheers,
Shaun

2005-07-19  Shaun Jackman  <sjackman at gmail.com>

	* libbb/dump.c (bpad): Bug fix: update pr->nospace. Use
	library functions to reduce code size.
	* util-linux/hexdump.c (hexdump_main): Add the -C option.

Index: libbb/dump.c
===================================================================
--- libbb/dump.c	(revision 10867)
+++ libbb/dump.c	(working copy)
@@ -438,17 +438,16 @@
 
 static void bpad(PR * pr)
 {
-	register char *p1, *p2;
-
 	/*
 	 * remove all conversion flags; '-' is the only one valid
 	 * with %s, and it's not useful here.
 	 */
+	char *p = strchr(pr->fmt, '%') + 1;
+	int n = strspn(p, " -0+#");
 	pr->flags = F_BPAD;
 	*pr->cchar = 's';
-	for (p1 = pr->fmt; *p1 != '%'; ++p1);
-	for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1);
-	while ((*p2++ = *p1++) != 0);
+	pr->nospace -= n;
+	strcpy(p, p + n);
 }
 
 static const char conv_str[] =
Index: util-linux/hexdump.c
===================================================================
--- util-linux/hexdump.c	(revision 10867)
+++ util-linux/hexdump.c	(working copy)
@@ -47,9 +47,11 @@
 	fclose(fp);
 }
 
+#define NUM_FORMATS 6
 static const char * const add_strings[] = {
 			"\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"",		/* b */
 			"\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"",		/* c */
+			"\"%07.7_ax  \" 8/1 \"%02x \" \"  \" 8/1 \"%02x \" ", /* C */
 			"\"%07.7_ax \" 8/2 \"  %05u \" \"\\n\"",	/* d */
 			"\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"",		/* o */
 			"\"%07.7_ax \" 8/2 \"   %04x \" \"\\n\"",	/* x */
@@ -77,9 +79,11 @@
 
 	while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
 		if ((p = strchr(hexdump_opts, ch)) != NULL) {
-			if ((p - hexdump_opts) < 5) {
+			if ((p - hexdump_opts) < NUM_FORMATS) {
 				bb_dump_add(add_first);
 				bb_dump_add(add_strings[(int)(p - hexdump_opts)]);
+				if (ch == 'C')
+					bb_dump_add("\"  |\" 16/1 \"%_p\" \"|\\n\"");
 			} else {
 				/* Sae a little bit of space below by omitting the 'else's. */
 				if (ch == 'e') {
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: busybox-hexdump-C.diff
Url: http://lists.busybox.net/pipermail/busybox/attachments/20050719/88300b5d/attachment.diff 


More information about the busybox mailing list