From aec1ddcd754c778a713b8cd5593c0a304db9d50b Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Tue, 31 Aug 2004 22:03:49 +0000 Subject: [PATCH] Add atoi(). Add a hex_dump() argument that specifies whether to include ASCII. Mark vsnprintf(), vprintk() with PRINTF_FORMAT. --- src/lib/lib.c | 57 ++++++++++++++++++++++++++++++++++++++++++--------- src/lib/lib.h | 9 +++++--- 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/lib/lib.c b/src/lib/lib.c index 25b4cc1..6e676cd 100644 --- a/src/lib/lib.c +++ b/src/lib/lib.c @@ -198,6 +198,38 @@ strtok_r (char *s, const char *delimiters, char **save_ptr) *save_ptr = s; return token; } + +int +atoi (const char *s) +{ + bool negative; + int value; + + /* Skip white space. */ + while (isspace (*s)) + s++; + + /* Parse sign. */ + negative = false; + if (*s == '+') + s++; + else if (*s == '-') + { + negative = true; + s++; + } + + /* Parse digits. We always initially parse the value as + negative, and then make it positive later, because the + negative range of an int is bigger than the positive range + on a 2's complement system. */ + for (value = 0; isdigit (*s); s++) + value = value * 10 - (*s - '0'); + if (!negative) + value = -value; + + return value; +} static void vprintf_core (const char *format, va_list args, @@ -734,7 +766,7 @@ vprintf_core (const char *format, va_list args, } void -hex_dump (const void *buffer, size_t size) +hex_dump (const void *buffer, size_t size, bool ascii) { const size_t n_per_line = 16; const uint8_t *p = buffer; @@ -747,15 +779,20 @@ hex_dump (const void *buffer, size_t size) printk ("%08zx", ofs); n = size >= n_per_line ? n_per_line : size; for (i = 0; i < n; i++) - printk ("%c%02x", i == n / 2 ? '-' : ' ', (unsigned) p[i]); - for (; i < n_per_line; i++) - printk (" "); - printk (" |"); - for (i = 0; i < n; i++) - printk ("%c", isprint (p[i]) ? p[i] : '.'); - for (; i < n_per_line; i++) - printk (" "); - printk ("|\n"); + printk ("%c%02x", i == n_per_line / 2 ? '-' : ' ', (unsigned) p[i]); + + if (ascii) + { + for (; i < n_per_line; i++) + printk (" "); + printk (" |"); + for (i = 0; i < n; i++) + printk ("%c", isprint (p[i]) ? p[i] : '.'); + for (; i < n_per_line; i++) + printk (" "); + printk ("|"); + } + printk ("\n"); p += n; ofs += n; diff --git a/src/lib/lib.h b/src/lib/lib.h index e163d87..7129139 100644 --- a/src/lib/lib.h +++ b/src/lib/lib.h @@ -2,6 +2,7 @@ #define HEADER_LIB_H 1 #include +#include #include #include "debug.h" @@ -21,12 +22,14 @@ size_t strlen (const char *); int strcmp (const char *, const char *); char *strtok_r (char *, const char *, char **); -void vprintk (const char *, va_list); +int atoi (const char *); + +void vprintk (const char *, va_list) PRINTF_FORMAT (1, 0); void printk (const char *, ...) PRINTF_FORMAT (1, 2); -int vsnprintf (char *, size_t, const char *, va_list); +int vsnprintf (char *, size_t, const char *, va_list) PRINTF_FORMAT (3, 0); int snprintf (char *, size_t, const char *, ...) PRINTF_FORMAT (3, 4); -void hex_dump (const void *, size_t size); +void hex_dump (const void *, size_t size, bool ascii); static inline int isdigit (int c) { return c >= '0' && c <= '9'; } static inline int isprint (int c) { return c >= 32 && c < 127; } -- 2.30.2