2015-09-28 21:02:17 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-09-29 18:08:58 -04:00
|
|
|
#include <inttypes.h>
|
2015-09-28 21:02:17 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
2015-09-29 17:09:59 -04:00
|
|
|
static size_t bytes_per_line = 16;
|
|
|
|
static unsigned char radix = 'o';
|
|
|
|
static unsigned char type = 'o';
|
2015-09-28 21:02:17 -04:00
|
|
|
|
|
|
|
static void
|
2015-09-29 18:08:58 -04:00
|
|
|
printaddress(FILE *f, off_t addr)
|
2015-09-28 21:02:17 -04:00
|
|
|
{
|
2015-09-29 18:08:58 -04:00
|
|
|
char fmt[] = "%07j# ";
|
2015-09-28 21:02:17 -04:00
|
|
|
|
2015-09-29 17:09:59 -04:00
|
|
|
if (radix == 'n') {
|
|
|
|
fputc(' ', f);
|
2015-09-28 21:02:17 -04:00
|
|
|
} else {
|
2015-09-29 17:09:59 -04:00
|
|
|
fmt[4] = radix;
|
2015-09-29 18:08:58 -04:00
|
|
|
fprintf(f, fmt, (intmax_t)addr);
|
2015-09-28 21:02:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-09-29 17:09:59 -04:00
|
|
|
printchar(FILE *f, unsigned char c)
|
2015-09-28 21:02:17 -04:00
|
|
|
{
|
2015-09-29 17:09:59 -04:00
|
|
|
const char *namedict[] = {
|
|
|
|
"nul", "soh", "stx", "etx", "eot", "enq", "ack",
|
|
|
|
"bel", "bs", "ht", "nl", "vt", "ff", "cr",
|
|
|
|
"so", "si", "dle", "dc1", "dc2", "dc3", "dc4",
|
|
|
|
"nak", "syn", "etb", "can", "em", "sub", "esc",
|
|
|
|
"fs", "gs", "rs", "us", "sp",
|
|
|
|
};
|
|
|
|
const char *escdict[] = {
|
|
|
|
['\0'] = "\\0", ['\a'] = "\\a",
|
|
|
|
['\b'] = "\\b", ['\t'] = "\\t",
|
|
|
|
['\n'] = "\\n", ['\v'] = "\\v",
|
|
|
|
['\f'] = "\\f", ['\r'] = "\\r",
|
|
|
|
};
|
|
|
|
const char *fmtdict[] = {
|
|
|
|
['d'] = "%4hhd ", ['o'] = "%03hho ",
|
|
|
|
['u'] = "%3hhu ", ['x'] = "%02hhx ",
|
|
|
|
};
|
|
|
|
|
|
|
|
if (type != 'a' && type != 'c') {
|
|
|
|
fprintf(f, fmtdict[type], c);
|
|
|
|
} else {
|
|
|
|
switch (type) {
|
|
|
|
case 'a':
|
|
|
|
c &= ~128; /* clear high bit as required by standard */
|
|
|
|
if (c < LEN(namedict) || c == 127) {
|
2015-09-29 17:40:20 -04:00
|
|
|
fprintf(f, "%3s ", (c == 127) ? "del" : namedict[c]);
|
2015-09-29 17:09:59 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
if (strchr("\a\b\t\n\b\f\r\0", c)) {
|
|
|
|
fprintf(f, "%3s ", escdict[c]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fprintf(f, "%3c ", c);
|
2015-09-28 21:02:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-09-29 17:09:59 -04:00
|
|
|
od(FILE *in, char *in_name, FILE *out, char *out_name)
|
2015-09-28 21:02:17 -04:00
|
|
|
{
|
2015-09-29 18:08:58 -04:00
|
|
|
off_t addr;
|
|
|
|
size_t i, chunklen;
|
2015-09-28 21:02:17 -04:00
|
|
|
unsigned char buf[BUFSIZ];
|
|
|
|
|
2015-09-29 18:08:58 -04:00
|
|
|
for (addr = 0; (chunklen = fread(buf, 1, BUFSIZ, in)); ) {
|
|
|
|
for (i = 0; i < chunklen; ++i, ++addr) {
|
2015-09-28 21:02:17 -04:00
|
|
|
if ((addr % bytes_per_line) == 0) {
|
2015-09-29 17:09:59 -04:00
|
|
|
if (addr)
|
|
|
|
fputc('\n', out);
|
|
|
|
printaddress(out, addr);
|
2015-09-28 21:02:17 -04:00
|
|
|
}
|
2015-09-29 17:09:59 -04:00
|
|
|
printchar(out, buf[i]);
|
2015-09-28 21:02:17 -04:00
|
|
|
}
|
2015-09-29 17:09:59 -04:00
|
|
|
if (feof(in) || ferror(in) || ferror(out))
|
2015-09-28 21:02:17 -04:00
|
|
|
break;
|
|
|
|
}
|
2015-09-29 18:08:58 -04:00
|
|
|
fputc('\n', out);
|
2015-09-29 17:09:59 -04:00
|
|
|
if (radix != 'n') {
|
2015-09-29 18:08:58 -04:00
|
|
|
printaddress(out, addr);
|
|
|
|
fputc('\n', out);
|
2015-09-29 17:09:59 -04:00
|
|
|
}
|
2015-09-29 18:08:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: %s [-A d|o|x|n] [-t a|c|d|o|u|x] [file ...]\n", argv0);
|
2015-09-28 21:02:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
int ret = 0;
|
2015-09-29 17:09:59 -04:00
|
|
|
char *s;
|
2015-09-28 21:02:17 -04:00
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'A':
|
|
|
|
s = EARGF(usage());
|
2015-09-29 17:14:59 -04:00
|
|
|
if (strlen(s) != 1 || !strchr("doxn", s[0]))
|
2015-09-28 21:02:17 -04:00
|
|
|
usage();
|
2015-09-29 17:09:59 -04:00
|
|
|
radix = s[0];
|
2015-09-28 21:02:17 -04:00
|
|
|
break;
|
|
|
|
case 't':
|
2015-09-29 17:09:59 -04:00
|
|
|
s = EARGF(usage());
|
2015-09-29 17:14:59 -04:00
|
|
|
if (strlen(s) != 1 || !strchr("acdoux", s[0]))
|
2015-09-28 21:02:17 -04:00
|
|
|
usage();
|
2015-09-29 17:09:59 -04:00
|
|
|
type = s[0];
|
2015-09-28 21:02:17 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:09:59 -04:00
|
|
|
ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>") |
|
|
|
|
fshut(stderr, "<stderr>");
|
2015-09-28 21:02:17 -04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|