summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2023-04-16 19:55:27 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2023-04-16 19:55:27 +0200
commit36cd029a27aaba9fc3e84c68d79fcf887806e51b (patch)
treef564969a39494945648a2802319b0b7ef8cd4ccd
parent6638fe908eedd0d3fd7531a9af2ed10dd18ed5c1 (diff)
downloadabase-36cd029a27aaba9fc3e84c68d79fcf887806e51b.tar.gz
abase-36cd029a27aaba9fc3e84c68d79fcf887806e51b.tar.bz2
added a simple hexdump
-rw-r--r--Makefile3
-rw-r--r--hexdump.127
-rw-r--r--hexdump.c98
-rw-r--r--more.c3
4 files changed, 129 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index 404ffd4..dd89fae 100644
--- a/Makefile
+++ b/Makefile
@@ -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;
+}
diff --git a/more.c b/more.c
index 7dbe5f6..9e15127 100644
--- a/more.c
+++ b/more.c
@@ -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) {