t/helper/hexdump: add helper to print hexdump of stdin
Co-authored-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff Hostetler committed
May 26, 2022 at 21:47 UTC
9915e08f9bc4122a1d49101fff43be20a2ea6cb5
4 files changed
+33
Makefile
+1
@@ -729,6 +729,7 @@ TEST_BUILTINS_OBJS += test-getcwd.o
729
TEST_BUILTINS_OBJS += test-hash-speed.o
730
TEST_BUILTINS_OBJS += test-hash.o
731
TEST_BUILTINS_OBJS += test-hashmap.o
732
+TEST_BUILTINS_OBJS += test-hexdump.o
733
TEST_BUILTINS_OBJS += test-index-version.o
734
TEST_BUILTINS_OBJS += test-json-writer.o
735
TEST_BUILTINS_OBJS += test-lazy-init-name-hash.o
t/helper/test-hexdump.c
new
+30
@@ -0,0 +1,30 @@
1
+#include "test-tool.h"
2
+#include "git-compat-util.h"
3
+
4
+/*
5
+ * Read stdin and print a hexdump to stdout.
6
+ */
7
+int cmd__hexdump(int argc, const char **argv)
8
+{
9
+ char buf[1024];
10
+ ssize_t i, len;
11
+ int have_data = 0;
12
+
13
+ for (;;) {
14
+ len = xread(0, buf, sizeof(buf));
15
+ if (len < 0)
16
+ die_errno("failure reading stdin");
17
+ if (!len)
18
+ break;
19
+
20
+ have_data = 1;
21
+
22
+ for (i = 0; i < len; i++)
23
+ printf("%02x ", (unsigned char)buf[i]);
24
+ }
25
+
26
+ if (have_data)
27
+ putchar('\n');
28
+
29
+ return 0;
30
+}
t/helper/test-tool.c
+1
@@ -38,6 +38,7 @@ static struct test_cmd cmds[] = {
38
{ "getcwd", cmd__getcwd },
39
{ "hashmap", cmd__hashmap },
40
{ "hash-speed", cmd__hash_speed },
41
+ { "hexdump", cmd__hexdump },
42
{ "index-version", cmd__index_version },
43
{ "json-writer", cmd__json_writer },
44
{ "lazy-init-name-hash", cmd__lazy_init_name_hash },
t/helper/test-tool.h
+1
@@ -29,6 +29,7 @@ int cmd__genzeros(int argc, const char **argv);
29
int cmd__getcwd(int argc, const char **argv);
30
int cmd__hashmap(int argc, const char **argv);
31
int cmd__hash_speed(int argc, const char **argv);
32
+int cmd__hexdump(int argc, const char **argv);
33
int cmd__index_version(int argc, const char **argv);
34
int cmd__json_writer(int argc, const char **argv);
35
int cmd__lazy_init_name_hash(int argc, const char **argv);