Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/lib/libkern Even smaller and takes print function.



details:   https://anonhg.NetBSD.org/src/rev/ce755d7c637d
branches:  trunk
changeset: 828340:ce755d7c637d
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Dec 09 00:51:52 2017 +0000

description:
Even smaller and takes print function.

diffstat:

 sys/lib/libkern/hexdump.c |  19 +++++++++++--------
 sys/lib/libkern/libkern.h |   5 +++--
 2 files changed, 14 insertions(+), 10 deletions(-)

diffs (93 lines):

diff -r 49393d0c495a -r ce755d7c637d sys/lib/libkern/hexdump.c
--- a/sys/lib/libkern/hexdump.c Sat Dec 09 00:29:32 2017 +0000
+++ b/sys/lib/libkern/hexdump.c Sat Dec 09 00:51:52 2017 +0000
@@ -27,15 +27,17 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: hexdump.c,v 1.3 2017/12/08 23:57:57 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: hexdump.c,v 1.4 2017/12/09 00:51:52 christos Exp $");
 
 #ifdef DEBUG_HEXDUMP
 #include <stdio.h>
 #include <ctype.h>
 #include <string.h>
 #include <stdlib.h>
+#define RET int
 static const char hexdigits[] = "0123456789abcdef";
 #else
+#define RET void
 #include <lib/libkern/libkern.h>
 #include <sys/systm.h>
 #endif
@@ -46,20 +48,20 @@
 #define NL (BAR + 18)
 
 void
-hexdump(const char *msg, const void *ptr, size_t len)
+hexdump(RET (*pr)(const char *, ...) __printflike(1, 2), const char *msg,
+    const void *ptr, size_t len)
 {
        size_t i, p, q;
        const unsigned char *u = ptr;
        char buf[NL + 2];
 
        if (msg)
-               printf("%s: %zu bytes @ %p\n", msg, len, ptr);
+               (*pr)("%s: %zu bytes @ %p\n", msg, len, ptr);
 
        buf[BAR] = '|';
        buf[BAR + 1] = ' ';
        buf[NL] = '\n';
        buf[NL + 1] = '\0';
-       memset(buf, ' ', BAR);
         for (q = p = i = 0; i < len; i++) {
                unsigned char c = u[i];
                buf[p++] = hexdigits[(c >> 4) & 0xf];
@@ -72,14 +74,15 @@
 
                if (q == 16) {
                        q = p = 0;
-                       printf("%s", buf);
-                       memset(buf, ' ', BAR);
+                       (*pr)("%s", buf);
                }
         }
        if (q) {
+               while (p < BAR)
+                       buf[p++] = ' ';
                buf[ASC + q++] = '\n';
                buf[ASC + q] = '\0';
-               printf("%s", buf);
+               (*pr)("%s", buf);
        }
 }
 
@@ -87,7 +90,7 @@
 int
 main(int argc, char *argv[])
 {
-       hexdump("foo", main, atoi(argv[1]));
+       hexdump(printf, "foo", main, atoi(argv[1]));
        return 0;
 }
 #endif
diff -r 49393d0c495a -r ce755d7c637d sys/lib/libkern/libkern.h
--- a/sys/lib/libkern/libkern.h Sat Dec 09 00:29:32 2017 +0000
+++ b/sys/lib/libkern/libkern.h Sat Dec 09 00:51:52 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: libkern.h,v 1.125 2017/12/08 21:51:07 christos Exp $   */
+/*     $NetBSD: libkern.h,v 1.126 2017/12/09 00:51:52 christos Exp $   */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -448,7 +448,8 @@
     intmax_t, int *);
 uintmax_t strtou(const char * __restrict, char ** __restrict, int, uintmax_t,
     uintmax_t, int *);
-void    hexdump(const char *, const void *, size_t);
+void    hexdump(void (*)(const char *, ...) __printflike(1, 2),
+    const char *, const void *, size_t);
 
 int     snprintb(char *, size_t, const char *, uint64_t);
 int     snprintb_m(char *, size_t, const char *, uint64_t, size_t);



Home | Main Index | Thread Index | Old Index