]> git.ipfire.org Git - thirdparty/git.git/commitdiff
t/helper/hexdump: add helper to print hexdump of stdin
authorJeff Hostetler <jeffhost@microsoft.com>
Thu, 26 May 2022 21:47:20 +0000 (21:47 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 26 May 2022 22:59:27 +0000 (15:59 -0700)
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>
Makefile
t/helper/test-hexdump.c [new file with mode: 0644]
t/helper/test-tool.c
t/helper/test-tool.h

index 5f1623baadde79a0771e7601dcea3c8f2b989ed9..5afa194aac6cf069e495105b343cc404ae66641b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -729,6 +729,7 @@ TEST_BUILTINS_OBJS += test-getcwd.o
 TEST_BUILTINS_OBJS += test-hash-speed.o
 TEST_BUILTINS_OBJS += test-hash.o
 TEST_BUILTINS_OBJS += test-hashmap.o
+TEST_BUILTINS_OBJS += test-hexdump.o
 TEST_BUILTINS_OBJS += test-index-version.o
 TEST_BUILTINS_OBJS += test-json-writer.o
 TEST_BUILTINS_OBJS += test-lazy-init-name-hash.o
diff --git a/t/helper/test-hexdump.c b/t/helper/test-hexdump.c
new file mode 100644 (file)
index 0000000..811e89c
--- /dev/null
@@ -0,0 +1,30 @@
+#include "test-tool.h"
+#include "git-compat-util.h"
+
+/*
+ * Read stdin and print a hexdump to stdout.
+ */
+int cmd__hexdump(int argc, const char **argv)
+{
+       char buf[1024];
+       ssize_t i, len;
+       int have_data = 0;
+
+       for (;;) {
+               len = xread(0, buf, sizeof(buf));
+               if (len < 0)
+                       die_errno("failure reading stdin");
+               if (!len)
+                       break;
+
+               have_data = 1;
+
+               for (i = 0; i < len; i++)
+                       printf("%02x ", (unsigned char)buf[i]);
+       }
+
+       if (have_data)
+               putchar('\n');
+
+       return 0;
+}
index 0424f7adf5d69cc2596a58ee3ef33f1826dfc621..88c4b28cdfa721a541d155a76a6e1859f3563efe 100644 (file)
@@ -38,6 +38,7 @@ static struct test_cmd cmds[] = {
        { "getcwd", cmd__getcwd },
        { "hashmap", cmd__hashmap },
        { "hash-speed", cmd__hash_speed },
+       { "hexdump", cmd__hexdump },
        { "index-version", cmd__index_version },
        { "json-writer", cmd__json_writer },
        { "lazy-init-name-hash", cmd__lazy_init_name_hash },
index c876e8246fbdff9ddf43e5b4ec46944406f9d33d..511f6251bf5b68fbfd7a3b18f1e2a51fea9357de 100644 (file)
@@ -29,6 +29,7 @@ int cmd__genzeros(int argc, const char **argv);
 int cmd__getcwd(int argc, const char **argv);
 int cmd__hashmap(int argc, const char **argv);
 int cmd__hash_speed(int argc, const char **argv);
+int cmd__hexdump(int argc, const char **argv);
 int cmd__index_version(int argc, const char **argv);
 int cmd__json_writer(int argc, const char **argv);
 int cmd__lazy_init_name_hash(int argc, const char **argv);