]> granicus.if.org Git - strace/commitdiff
tests: add print_quoted_string function to libtests
authorJayRJoshi <jay.r.joshi100@gmail.com>
Wed, 23 Mar 2016 13:34:15 +0000 (19:04 +0530)
committerDmitry V. Levin <ldv@altlinux.org>
Wed, 23 Mar 2016 13:34:15 +0000 (13:34 +0000)
* tests/tests.h (print_quoted_string): New prototype.
* tests/print_quoted_string.c: New file.
* tests/Makefile.am (libtests_a_SOURCES): Add it.

tests/Makefile.am
tests/print_quoted_string.c [new file with mode: 0644]
tests/tests.h

index 3005382180faafd7bd1d6a9a28bb0f8852a69f5b..add44efe7f7582c0d6972dcf23f0966e4f198dbe 100644 (file)
@@ -46,6 +46,7 @@ libtests_a_SOURCES = \
        hexdump_strdup.c \
        hexquote_strndup.c \
        inode_of_sockfd.c \
+       print_quoted_string.c \
        tail_alloc.c \
        tests.h \
        tprintf.c \
diff --git a/tests/print_quoted_string.c b/tests/print_quoted_string.c
new file mode 100644 (file)
index 0000000..18a9ccf
--- /dev/null
@@ -0,0 +1,67 @@
+#include "tests.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+/*
+ * Based on string_quote() from util.c.
+ * Assumes instr is NUL-terminated.
+ */
+
+void
+print_quoted_string(const char *instr)
+{
+       const unsigned char *str = (const unsigned char*) instr;
+       int c;
+
+       while ((c = *(str++))) {
+               switch (c) {
+                       case '\"':
+                               printf("\\\"");
+                               break;
+                       case '\\':
+                               printf("\\\\");
+                               break;
+                       case '\f':
+                               printf("\\f");
+                               break;
+                       case '\n':
+                               printf("\\n");
+                               break;
+                       case '\r':
+                               printf("\\r");
+                               break;
+                       case '\t':
+                               printf("\\t");
+                               break;
+                       case '\v':
+                               printf("\\v");
+                               break;
+                       default:
+                               if (c >= ' ' && c <= 0x7e)
+                                       putchar(c);
+                               else {
+                                       putchar('\\');
+
+                                       char c1 = '0' + (c & 0x7);
+                                       char c2 = '0' + ((c >> 3) & 0x7);
+                                       char c3 = '0' + (c >> 6);
+
+                                       if (*str >= '0' && *str <= '9') {
+                                               /* Print \octal */
+                                               putchar(c3);
+                                               putchar(c2);
+                                       } else {
+                                               /* Print \[[o]o]o */
+                                               if (c3 != '0')
+                                                       putchar(c3);
+                                               if (c3 != '0' || c2 != '0')
+                                                       putchar(c2);
+                                       }
+                                       putchar(c1);
+                               }
+                               break;
+               }
+       }
+
+}
index 826f8b2ff23b7eed4ccbb3df461e4faba014de39..d8632ba3171362a81c30d79dec2a91cb6a174461 100644 (file)
@@ -75,6 +75,9 @@ const char *hexquote_strndup(const char *, size_t);
 /* Return inode number of socket descriptor. */
 unsigned long inode_of_sockfd(int);
 
+/* Print string in quoted form. */
+void print_quoted_string(const char *str);
+
 # define ARRAY_SIZE(arg) ((unsigned int) (sizeof(arg) / sizeof((arg)[0])))
 # define LENGTH_OF(arg) ((unsigned int) sizeof(arg) - 1)