Skip to content

Commit 9915e08

Browse files
jeffhostetlerdscho
authored andcommitted
t/helper/hexdump: add helper to print hexdump of stdin
Co-authored-by: Johannes Schindelin <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Jeff Hostetler <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d6d58ff commit 9915e08

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,7 @@ TEST_BUILTINS_OBJS += test-getcwd.o
729729
TEST_BUILTINS_OBJS += test-hash-speed.o
730730
TEST_BUILTINS_OBJS += test-hash.o
731731
TEST_BUILTINS_OBJS += test-hashmap.o
732+
TEST_BUILTINS_OBJS += test-hexdump.o
732733
TEST_BUILTINS_OBJS += test-index-version.o
733734
TEST_BUILTINS_OBJS += test-json-writer.o
734735
TEST_BUILTINS_OBJS += test-lazy-init-name-hash.o

t/helper/test-hexdump.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static struct test_cmd cmds[] = {
3838
{ "getcwd", cmd__getcwd },
3939
{ "hashmap", cmd__hashmap },
4040
{ "hash-speed", cmd__hash_speed },
41+
{ "hexdump", cmd__hexdump },
4142
{ "index-version", cmd__index_version },
4243
{ "json-writer", cmd__json_writer },
4344
{ "lazy-init-name-hash", cmd__lazy_init_name_hash },

t/helper/test-tool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ int cmd__genzeros(int argc, const char **argv);
2929
int cmd__getcwd(int argc, const char **argv);
3030
int cmd__hashmap(int argc, const char **argv);
3131
int cmd__hash_speed(int argc, const char **argv);
32+
int cmd__hexdump(int argc, const char **argv);
3233
int cmd__index_version(int argc, const char **argv);
3334
int cmd__json_writer(int argc, const char **argv);
3435
int cmd__lazy_init_name_hash(int argc, const char **argv);

0 commit comments

Comments
 (0)