Refactor parts of od(1)

It was possible to make some sections of the code shorter.
Also fix a bug where the last printed address was always in hex
rather than depending on the radix chosen.
This commit is contained in:
FRIGN 2015-09-29 23:09:59 +02:00 committed by sin
parent fd0d1e4567
commit d49bce7fc8
1 changed files with 70 additions and 100 deletions

158
od.c
View File

@ -4,8 +4,9 @@
#include "util.h" #include "util.h"
static char addr_radix = 'o'; static size_t bytes_per_line = 16;
static char *type = "o"; static unsigned char radix = 'o';
static unsigned char type = 'o';
static void static void
usage(void) usage(void)
@ -14,120 +15,87 @@ usage(void)
} }
static void static void
print_address(FILE *f, size_t addr) printaddress(FILE *f, size_t addr)
{ {
switch (addr_radix) { char fmt[] = "%06z# ";
case 'x':
fprintf(f, "%06zx ", addr);
break;
case 'd':
fprintf(f, "%06zd ", addr);
break;
case 'n':
fprintf(f, "%s", " ");
break;
case 'o':
default:
fprintf(f, "%06zo ", addr);
break;
}
}
static char char_string[2]; if (radix == 'n') {
fputc(' ', f);
static const char *
escaped_char(unsigned char c)
{
switch (c) {
case '\0':
return "\\0";
case '\a':
return "\\a";
case '\b':
return "\\b";
case '\t':
return "\\t";
case '\n':
return "\\n";
case '\v':
return "\\v";
case '\f':
return "\\f";
case '\r':
return "\\r";
default:
char_string[0] = c;
char_string[1] = '\0';
return char_string;
}
}
static const char *
named_char(unsigned char c)
{
static const int table_size = 33;
static const char * named_chars[] = {"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"};
c &= ~128; /* clear high bit of byte, as required by standard */
if (c < table_size) {
return named_chars[c];
} else if (c == 127) {
return "del";
} else { } else {
char_string[0] = c; fmt[4] = radix;
char_string[1] = '\0'; fprintf(f, fmt, addr);
return char_string;
} }
} }
static void static void
print_content(FILE *f, char type, unsigned char cont) printchar(FILE *f, unsigned char c)
{ {
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",
[127] = "del"
};
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) { switch (type) {
case 'a': case 'a':
fprintf(f, "%3s ", named_char(cont)); c &= ~128; /* clear high bit as required by standard */
if (c < LEN(namedict) || c == 127) {
fprintf(f, "%3s ", namedict[c]);
return;
}
break; break;
case 'c': case 'c':
fprintf(f, "%3s ", escaped_char(cont)); if (strchr("\a\b\t\n\b\f\r\0", c)) {
break; fprintf(f, "%3s ", escdict[c]);
case 'd': return;
fprintf(f, "%4hhd ", cont); }
break;
case 'o':
fprintf(f, "%03hho ", cont);
break;
case 'u':
fprintf(f, "%3hhu ", cont);
break;
case 'x':
fprintf(f, "%02hhx ", cont);
break; break;
} }
fprintf(f, "%3c ", c);
}
} }
static void static void
od(FILE *fp_in, const char *name_in, FILE *fp_out, const char *name_out) od(FILE *in, char *in_name, FILE *out, char *out_name)
{ {
unsigned char buf[BUFSIZ]; unsigned char buf[BUFSIZ];
size_t addr, buf_size, i; char fmt[] = "\n%.6z#";
const size_t bytes_per_line = 16; off_t addr, bread, i;
addr = 0; addr = 0;
for (; (buf_size = fread(buf, 1, BUFSIZ, fp_in)); ) { for (; (bread = fread(buf, 1, BUFSIZ, in)); ) {
for (i = 0; i < buf_size; ++i, ++addr) { for (i = 0; i < bread; ++i, ++addr) {
if ((addr % bytes_per_line) == 0) { if ((addr % bytes_per_line) == 0) {
if (addr != 0) fprintf(fp_out, "%s", "\n"); if (addr)
print_address(fp_out, addr); fputc('\n', out);
printaddress(out, addr);
} }
print_content(fp_out, type[0], buf[i]); printchar(out, buf[i]);
} }
if (feof(fp_in) || ferror(fp_in) || ferror(fp_out)) if (feof(in) || ferror(in) || ferror(out))
break; break;
} }
fprintf(fp_out, "\n%.7zx \n", addr); if (radix != 'n') {
fmt[5] = radix;
fprintf(out, fmt, addr);
}
fputc('\n', out);
} }
int int
@ -142,12 +110,13 @@ main(int argc, char *argv[])
s = EARGF(usage()); s = EARGF(usage());
if (strlen(s) > 1 || !strchr("doxn", s[0])) if (strlen(s) > 1 || !strchr("doxn", s[0]))
usage(); usage();
addr_radix = s[0]; radix = s[0];
break; break;
case 't': case 't':
type = EARGF(usage()); s = EARGF(usage());
if (strlen(type) > 1 || !strchr("acdoux", type[0])) if (strlen(s) > 1 || !strchr("acdoux", s[0]))
usage(); usage();
type = s[0];
break; break;
default: default:
usage(); usage();
@ -171,7 +140,8 @@ main(int argc, char *argv[])
} }
} }
ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"); ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>") |
fshut(stderr, "<stderr>");
return ret; return ret;
} }