diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2023-04-16 19:55:27 +0200 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2023-04-16 19:55:27 +0200 |
commit | 36cd029a27aaba9fc3e84c68d79fcf887806e51b (patch) | |
tree | f564969a39494945648a2802319b0b7ef8cd4ccd | |
parent | 6638fe908eedd0d3fd7531a9af2ed10dd18ed5c1 (diff) | |
download | abase-36cd029a27aaba9fc3e84c68d79fcf887806e51b.tar.gz abase-36cd029a27aaba9fc3e84c68d79fcf887806e51b.tar.bz2 |
added a simple hexdump
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | hexdump.1 | 27 | ||||
-rw-r--r-- | hexdump.c | 98 | ||||
-rw-r--r-- | more.c | 3 |
4 files changed, 129 insertions, 2 deletions
@@ -14,7 +14,8 @@ LIBUTILSRC =\ LIB = $(LIBUTF) $(LIBUTIL) BIN =\ - more + more\ + hexdump LIBUTILOBJ = $(LIBUTILSRC:.c=.o) OBJ = $(BIN:=.o) $(LIBUTFOBJ) $(LIBUTILOBJ) diff --git a/hexdump.1 b/hexdump.1 new file mode 100644 index 0000000..247064d --- /dev/null +++ b/hexdump.1 @@ -0,0 +1,27 @@ +.Dd 2023-04-14 +.Dt HEXDUMP 1 +.Os abase +.Sh NAME +.Nm hexdump +.Nd display file contents in hex and/or ASCII +.Sh SYNOPSIS +.Nm +.Sh DESCRIPTION +.Nm +prints hexadecimal dump of +.Ar path +to stdout. +.Nm +.Sh OPTIONS +.sp +.RE +\fB\-C\fP +.RS 4 +Canonical hex+ASCII format. 16 bytes per line, print hex code area on +the left and printable characters on the right. +.RE +.sp +\fB\-v\fP +.RS 4 +Display all data, no squezing of consecutive identical lines of bytes. + diff --git a/hexdump.c b/hexdump.c new file mode 100644 index 0000000..552e4c6 --- /dev/null +++ b/hexdump.c @@ -0,0 +1,98 @@ +/* See LICENSE file for copyright and license details. */ +#include <fcntl.h> +#include <unistd.h> +#include <stdio.h> +#include <ctype.h> + +#include "util.h" + +static void +usage(void) +{ + eprintf("usage: %s [-C] [file ...]\n", argv0); +} + +#define BYTES_PER_LINE 16 + +int +main(int argc, char *argv[]) +{ + int fd; + char c; + int nread; + int pos; + int lastpos; + int i; + char buf[BYTES_PER_LINE]; + + ARGBEGIN { + case 'v': + break; + case 'C': + break; + default: + usage(); + } ARGEND + + if (argc < 1 && isatty(STDIN_FILENO)) { + usage(); + } + + if (isatty(STDIN_FILENO)) { + if ((fd = open(*argv, O_RDONLY)) < 0) { + weprintf("open %s:", *argv); + return 0; + + } + } else { + fd = STDIN_FILENO; + } + + lastpos = 0; + pos = 0; + while ((nread=read(fd, &c, 1)) >= 0) { + if (nread > 0) { + if (pos % BYTES_PER_LINE == 0) { + printf("%08x ", pos); + } + printf(" %02x", c & 0xFF ); + buf[pos%BYTES_PER_LINE] = c; + pos++; + } else { + if (lastpos == 0) { + lastpos = pos; + } + printf(" "); + pos++; + } + if (pos % BYTES_PER_LINE == BYTES_PER_LINE/2) { + printf(" "); + } + if (pos % BYTES_PER_LINE == 0) { + printf(" |"); + if (nread == 0) { + pos = lastpos; + } + for (i = 0; i <= (pos+BYTES_PER_LINE-1) % BYTES_PER_LINE; i++) { + if (isprint(buf[i]) ) { + putchar(buf[i]); + } else { + putchar('.'); + } + } + puts("|"); + if (nread == 0) { + goto END; + } + } + } +END: + printf("%08x\n", pos); + + + if (fd > STDERR_FILENO) { + close(fd); + } + + return 0; +} @@ -30,8 +30,9 @@ main(int argc, char *argv[]) usage(); } ARGEND - if (argc < 1 && isatty(STDIN_FILENO)) + if (argc < 1 && isatty(STDIN_FILENO)) { usage(); + } if (isatty(STDIN_FILENO)) { if ((fd = open(*argv, O_RDONLY)) < 0) { |