From fa1ae45491a8e41ccde4e0b366c37ec2a067bae9 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Mon, 4 Nov 2019 12:21:38 +0100 Subject: Let xxd take stream argument --- client.c | 2 +- icmp.h | 1 + server.c | 4 ++-- util.c | 20 ++++++++++---------- util.h | 3 ++- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/client.c b/client.c index 0f8bde1..7c0eb71 100644 --- a/client.c +++ b/client.c @@ -52,7 +52,7 @@ int main(void) { if (FD_ISSET(d_fd, &inset) && icmpd_peek(d)) { struct icmpd_received msg = icmpd_recv(d); printf("Recv: %zu\n", msg.length); - xxd(msg.data, msg.length); + xxd(stdout, msg.data, msg.length); } } } diff --git a/icmp.h b/icmp.h index eed1714..c242207 100644 --- a/icmp.h +++ b/icmp.h @@ -1,5 +1,6 @@ #pragma once +#include #include diff --git a/server.c b/server.c index b99e4ea..2ace5b5 100644 --- a/server.c +++ b/server.c @@ -53,7 +53,7 @@ int main(void) { if (FD_ISSET(server_fd, &inset) && icmpd_peek(server)) { struct icmpd_received msg = icmpd_recv(server); printf("Server recv: %zu\n", msg.length); - xxd(msg.data, msg.length); + xxd(stdout, msg.data, msg.length); if (conn) { printf("Message received while already connected\n"); @@ -76,7 +76,7 @@ int main(void) { if (conn && FD_ISSET(conn_fd, &inset) && icmpd_peek(conn)) { struct icmpd_received msg = icmpd_recv(conn); printf("Connection recv: %zu\n", msg.length); - xxd(msg.data, msg.length); + xxd(stdout, msg.data, msg.length); free(msg.data); } } diff --git a/util.c b/util.c index 9c7e1a3..7bd9025 100644 --- a/util.c +++ b/util.c @@ -13,27 +13,27 @@ int uniqid(void) { return i++; } -void xxd(const void *buf_, size_t length) { +void xxd(FILE *stream, const void *buf_, size_t length) { unsigned char *buf = (unsigned char*)buf_; for (size_t cursor = 0; cursor < length;) { - printf("%08zx:", cursor); + fprintf(stream, "%08zx:", cursor); for (int i = 0; i < 16; i++) { - if (i % 2 == 0) printf(" "); - if (i % 8 == 0) printf(" "); - if (cursor + i < length) printf("%02x", (unsigned)buf[cursor + i]); - else printf(" "); + if (i % 2 == 0) fprintf(stream, " "); + if (i % 8 == 0) fprintf(stream, " "); + if (cursor + i < length) fprintf(stream, "%02x", (unsigned)buf[cursor + i]); + else fprintf(stream, " "); } - printf(" |"); + fprintf(stream, " |"); for (int i = 0; i < 16 && cursor + i < length; i++) { - if (isprint(buf[cursor + i])) printf("%c", buf[cursor + i]); - else printf("."); + if (isprint(buf[cursor + i])) fprintf(stream, "%c", buf[cursor + i]); + else fprintf(stream, "."); } - printf("|\n"); + fprintf(stream, "|\n"); cursor += 16; } diff --git a/util.h b/util.h index 88ad996..898f10e 100644 --- a/util.h +++ b/util.h @@ -1,12 +1,13 @@ #pragma once +#include #include #include #include int uniqid(void); -void xxd(const void *buf, size_t length); +void xxd(FILE *stream, const void *buf, size_t length); ssize_t readall(int fd, void *data, size_t length); ssize_t writeall(int fd, const void *data, size_t length); int maxi(int a, int b); -- cgit v1.2.3-70-g09d2