Patchwork [2/6] Eliminate a couple of 3-line functions that barely wrap *printf calls

login
register
about
Submitter Patrick Georgi
Date 2011-01-27 14:07:39
Message ID <20110127141002.563214175@secunet.com>
Download mbox | patch
Permalink /patch/2563/
State Accepted
Headers show

Comments

Patrick Georgi - 2011-01-27 14:07:39
Signed-off-by: Patrick Georgi <patrick.georgi@secunet.com>
Stefan Reinauer - 2011-01-27 20:48:54
* Patrick Georgi <patrick.georgi@secunet.com> [110127 15:07]:
> Signed-off-by: Patrick Georgi <patrick.georgi@secunet.com>

Acked-by: Stefan Reinauer <stefan.reinauer@coreboot.org>

Patch

Index: nvramtool/hexdump.c
===================================================================
--- nvramtool.orig/hexdump.c
+++ nvramtool/hexdump.c
@@ -3,6 +3,7 @@ 
 \*****************************************************************************/
 
 #include "hexdump.h"
+#include <ctype.h>
 
 /* hexdump.c
  *
@@ -44,9 +45,6 @@ 
  */
 
 static void addrprint(FILE * outfile, uint64_t address, int width);
-static void hexprint(FILE * outfile, unsigned char byte);
-static void charprint(FILE * outfile, unsigned char byte,
-		      unsigned char nonprintable);
 
 /*--------------------------------------------------------------------------
  * hexdump
@@ -92,7 +90,7 @@  void hexdump(const void *mem, int bytes,
 
 		/* display the bytes in hex */
 		for (i = 0;;) {
-			hexprint(outfile, p[index++]);
+			fprintf(outfile, "%02x", p[index++]);
 
 			if (++i >= format->bytes_per_line)
 				break;
@@ -104,8 +102,8 @@  void hexdump(const void *mem, int bytes,
 		fprintf(outfile, format->sep3);
 
 		/* display the bytes as characters */
-		for (i = 0; i < format->bytes_per_line; i++)
-			charprint(outfile, p[index++], format->nonprintable);
+		for (i = 0; i < format->bytes_per_line; i++, index++)
+			fputc(isprint(p[index])?p[index]:format->nonprintable, outfile);
 
 		fprintf(outfile, "\n");
 	}
@@ -120,7 +118,7 @@  void hexdump(const void *mem, int bytes,
 
 	/* display bytes for last line in hex */
 	for (i = 0; i < bytes_left; i++) {
-		hexprint(outfile, p[index++]);
+		fprintf(outfile, "%02x", p[index++]);
 		fprintf(outfile, format->sep2);
 	}
 
@@ -140,7 +138,7 @@  void hexdump(const void *mem, int bytes,
 
 	/* display bytes for last line as characters */
 	for (i = 0; i < bytes_left; i++)
-		charprint(outfile, p[index++], format->nonprintable);
+		fputc(isprint(p[index])?p[index++]:format->nonprintable, outfile);
 
 	/* pad the rest of the character area with spaces */
 	for (; i < format->bytes_per_line; i++)
@@ -188,38 +186,3 @@  static void addrprint(FILE * outfile, ui
 	}
 }
 
-/*--------------------------------------------------------------------------
- * hexprint
- *
- * Display a byte as a two digit hex value.
- *
- * parameters:
- *     outfile: the place where the output should be written
- *     byte:    the byte to display
- *--------------------------------------------------------------------------*/
-static void hexprint(FILE * outfile, unsigned char byte)
-{
-	static const char tbl[] = {
-		'0', '1', '2', '3', '4', '5', '6', '7',
-		'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
-	};
-
-	fprintf(outfile, "%c%c", tbl[byte >> 4], tbl[byte & 0x0f]);
-}
-
-/*--------------------------------------------------------------------------
- * charprint
- *
- * Display a byte as its character representation.
- *
- * parameters:
- *     outfile:         the place where the output should be written
- *     byte:            the byte to display
- *     nonprintable:    a substitute character to display if the byte
- *                      represents a nonprintable character
- *--------------------------------------------------------------------------*/
-static void charprint(FILE * outfile, unsigned char byte,
-		      unsigned char nonprintable)
-{
-	fprintf(outfile, "%c", ((byte >= 0x20) && (byte <= 0x7e)) ? byte : nonprintable);
-}
Index: nvramtool/lbtable.c
===================================================================
--- nvramtool.orig/lbtable.c
+++ nvramtool/lbtable.c
@@ -88,7 +88,6 @@  static void print_defaults_record(const
 static void print_unknown_record(const struct lb_record *cmos_item);
 static void option_checksum_print_fn(const struct lb_record *rec);
 static void string_print_fn(const struct lb_record *rec);
-static void uint64_to_hex_string(char str[], uint64_t n);
 
 static const char memory_desc[] =
     "    This shows information about system memory.\n";
@@ -1058,7 +1057,6 @@  static const struct lb_record *next_cmos
  ****************************************************************************/
 static void memory_print_fn(const struct lb_record *rec)
 {
-	char start_str[19], end_str[19], size_str[19];
 	const struct lb_memory *p;
 	const char *mem_type;
 	const struct lb_memory_range *ranges;
@@ -1096,13 +1094,10 @@  static void memory_print_fn(const struct
 		size = unpack_lb64(ranges[i].size);
 		start = unpack_lb64(ranges[i].start);
 		end = start + size - 1;
-		uint64_to_hex_string(start_str, start);
-		uint64_to_hex_string(end_str, end);
-		uint64_to_hex_string(size_str, size);
 		printf("%s memory:\n"
-		       "    from physical addresses %s to %s\n"
-		       "    size is %s bytes (%lld in decimal)\n",
-		       mem_type, start_str, end_str, size_str,
+		       "    from physical addresses 0x%016llx to 0x%016llx\n"
+		       "    size is 0x%016llx bytes (%lld in decimal)\n",
+		       mem_type, start, end, size,
 		       (unsigned long long)size);
 
 		if (++i >= entries)
@@ -1327,24 +1322,3 @@  static void string_print_fn(const struct
 	printf("%s\n", p->string);
 }
 
-/****************************************************************************
- * uint64_to_hex_string
- *
- * Convert the 64-bit integer 'n' to its hexadecimal string representation,
- * storing the result in 's'.  's' must point to a buffer at least 19 bytes
- * long.  The result is displayed with as many leading zeros as needed to
- * make a 16-digit hex number including a 0x prefix (example: the number 1
- * will be displayed as "0x0000000000000001").
- ****************************************************************************/
-static void uint64_to_hex_string(char str[], uint64_t n)
-{
-	int chars_printed;
-
-	str[0] = '0';
-	str[1] = 'x';
-
-	/* Print the result right-justified with leading spaces in a
-	 * 16-character field. */
-	chars_printed = sprintf(&str[2], "%016llx", (unsigned long long)n);
-	assert(chars_printed == 16);
-}