diff options
author | Tom Smeding <tom.smeding@gmail.com> | 2019-11-04 12:21:38 +0100 |
---|---|---|
committer | Tom Smeding <tom.smeding@gmail.com> | 2019-11-04 12:21:38 +0100 |
commit | fa1ae45491a8e41ccde4e0b366c37ec2a067bae9 (patch) | |
tree | 0a54d3746f3472aaad06b903829cb241850a39f7 | |
parent | cf6fbbec9e2a04217a135924fd2bf209be488223 (diff) |
Let xxd take stream argument
-rw-r--r-- | client.c | 2 | ||||
-rw-r--r-- | icmp.h | 1 | ||||
-rw-r--r-- | server.c | 4 | ||||
-rw-r--r-- | util.c | 20 | ||||
-rw-r--r-- | util.h | 3 |
5 files changed, 16 insertions, 14 deletions
@@ -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); } } } @@ -1,5 +1,6 @@ #pragma once +#include <stddef.h> #include <stdint.h> @@ -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); } } @@ -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; } @@ -1,12 +1,13 @@ #pragma once +#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <unistd.h> 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); |