]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
utils: add print_escape_buf to format and print arbitrary bytes
authorIvan Delalande <colona@arista.com>
Fri, 6 Oct 2017 23:48:19 +0000 (16:48 -0700)
committerStephen Hemminger <stephen@networkplumber.org>
Wed, 11 Oct 2017 18:04:47 +0000 (11:04 -0700)
Keep it as simple as possible for now: just escape anything that is not
isprint-able, is among the "escape" parameter or '\' as an octal escape
sequence. This should be pretty easy to extend if any other user needs
something more complex in the future.

Signed-off-by: Ivan Delalande <colona@arista.com>
include/utils.h
lib/utils.c

index 76addb3258f59fa939f464ea509b9fb44b919351..3d91c50db05b9f130472adc67c65e0be0f8834b9 100644 (file)
@@ -195,6 +195,8 @@ static inline void __jiffies_to_tv(struct timeval *tv, unsigned long jiffies)
        tv->tv_usec = tvusec - 1000000 * tv->tv_sec;
 }
 
+void print_escape_buf(const __u8 *buf, size_t len, const char *escape);
+
 int print_timestamp(FILE *fp);
 void print_nlmsg_timestamp(FILE *fp, const struct nlmsghdr *n);
 
index 632fd0ddac82e1a59e266621cc2eccd55c910c9c..ac155bf5a0447a60a2075a625c24b89405f2e6b0 100644 (file)
@@ -31,6 +31,7 @@
 #include <time.h>
 #include <sys/time.h>
 #include <errno.h>
+#include <ctype.h>
 
 #include "rt_names.h"
 #include "utils.h"
@@ -1047,6 +1048,20 @@ int addr64_n2a(__u64 addr, char *buff, size_t len)
        return written;
 }
 
+/* Print buffer and escape bytes that are !isprint or among 'escape' */
+void print_escape_buf(const __u8 *buf, size_t len, const char *escape)
+{
+       size_t i;
+
+       for (i = 0; i < len; ++i) {
+               if (isprint(buf[i]) && buf[i] != '\\' &&
+                   !strchr(escape, buf[i]))
+                       printf("%c", buf[i]);
+               else
+                       printf("\\%03o", buf[i]);
+       }
+}
+
 int print_timestamp(FILE *fp)
 {
        struct timeval tv;