[PATCH] Created od.

From: Greg Reagle <greg.reagle_AT_umbc.edu>
Date: Mon, 28 Sep 2015 21:02:17 -0400

---
 LICENSE  |    2 +-
 Makefile |    1 +
 od.1     |   25 ++++++++++++++++
 od.c     |   97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 124 insertions(+), 1 deletion(-)
 create mode 100644 od.1
 create mode 100644 od.c
diff --git a/LICENSE b/LICENSE
index 5ebeb0c..1f42551 100644
--- a/LICENSE
+++ b/LICENSE
_AT_@ -54,7 +54,7 @@ Authors/contributors include:
 © 2014 Ari Malinen <ari.malinen_AT_gmail.com>
 © 2014 Brandon Mulcahy <brandon_AT_jangler.info>
 © 2014 Adria Garriga <rhaps0dy_AT_installgentoo.com>
-© 2014 Greg Reagle <greg.reagle_AT_umbc.edu>
+© 2014,2015 Greg Reagle <greg.reagle_AT_umbc.edu>
 © 2015 Tai Chi Minh Ralph Eastwood <tcmreastwood_AT_gmail.com>
 © 2015 Quentin Rameau <quinq_AT_quinq.eu.org>
 © 2015 Dionysis Grigoropoulos <info_AT_erethon.com>
diff --git a/Makefile b/Makefile
index c5cb2e4..9f60a13 100644
--- a/Makefile
+++ b/Makefile
_AT_@ -115,6 +115,7 @@ BIN =\
 	nice\
 	nl\
 	nohup\
+	od\
 	paste\
 	printenv\
 	printf\
diff --git a/od.1 b/od.1
new file mode 100644
index 0000000..783799b
--- /dev/null
+++ b/od.1
_AT_@ -0,0 +1,25 @@
+.Dd September 28, 2015
+.Dt OD 1
+.Os sbase
+.Sh NAME
+.Nm od
+.Nd octal dump
+.Sh SYNOPSIS
+.Nm
+.Op Fl A Ar d|o|x|n
+.Op Ar file...
+.Sh DESCRIPTION
+.Nm
+writes an octal dump of
+.Ar file(s)
+to standard output.  If no
+.Ar file(s)
+are specified then
+.Nm
+is a filter, i.e. reads from standard input.
+.Sh OPTIONS
+.Bl -tag -width Ds
+.It Fl A Ar d|o|x|n
+
+Display the address in base \fId\fRecimal | \fIo\fRctal | he\fIx\fRadecimal |
+\fIn\fRone.  If unspecified, the default is octal.
diff --git a/od.c b/od.c
new file mode 100644
index 0000000..a766d59
--- /dev/null
+++ b/od.c
_AT_@ -0,0 +1,97 @@
+/* See LICENSE file for copyright and license details. */
+#include <string.h>
+
+#include "stdlib.h"
+#include "util.h"
+
+static char *address_radix = "o";
+
+static void
+usage(void)
+{
+	eprintf("usage: %s [-A d|o|x|n] [file ...]\n", argv0);
+}
+
+void
+print_address(FILE *f, char radix, size_t addr)
+{
+	switch (radix) {
+	case 'x':
+		fprintf(f, "%.7zx ", addr);
+		break;
+	case 'd':
+		fprintf(f, "%.7zd ", addr);
+		break;
+	case 'o':
+		fprintf(f, "%.7zo ", addr);
+		break;
+	case 'n':
+		fprintf(f, "%s", " ");
+		break;
+	default:
+		usage();
+	}
+}
+
+void
+od(FILE *fp_in, const char *s_in, FILE *fp_out, const char *s_out)
+{
+	unsigned char buf[BUFSIZ];
+	size_t addr;
+	size_t n; /*actual size of the buffer*/
+	size_t i; /*tracks index within the buffer*/
+	const size_t bytes_per_line = 16;
+
+	addr = 0;
+	while ((n = fread(buf, 1, 5, fp_in))) {
+		for (i=0; i<n; ++i, ++addr) {
+			if ((addr % bytes_per_line) == 0) {
+				if (addr != 0) fprintf(fp_out, "%s", "\n");
+				print_address(fp_out, address_radix[0], addr);
+			}
+			fprintf(fp_out, "%.2hhx ", buf[i]);
+		}
+		if (feof(fp_in) || ferror(fp_in) || ferror(fp_out))
+			break;
+	}
+	fprintf(fp_out, "\n%.7zx \n", addr);
+}
+
+int
+main(int argc, char *argv[])
+{
+	FILE *fp;
+	int ret = 0;
+
+	ARGBEGIN {
+	case 'A':
+		address_radix = EARGF(usage());
+		if ((strlen(address_radix) > 1) || (!strchr("doxn", address_radix[0])))
+			usage();
+		break;
+	default:
+		usage();
+	} ARGEND;
+
+	if (!argc) {
+		od(stdin, "<stdin>", stdout, "<stdout>");
+	} else {
+		for (; *argv; argc--, argv++) {
+			if (!strcmp(*argv, "-")) {
+				*argv = "<stdin>";
+				fp = stdin;
+			} else if (!(fp = fopen(*argv, "r"))) {
+				weprintf("fopen %s:", *argv);
+				ret = 1;
+				continue;
+			}
+			od(fp, *argv, stdout, "<stdout>");
+			if (fp != stdin && fshut(fp, *argv))
+				ret = 1;
+		}
+	}
+
+	ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
+
+	return ret;
+}
-- 
1.7.10.4
--------------060000000200090208010406--
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Tue Sep 29 2015 - 05:00:11 CEST