Rewrite od(1)
Looking at the old code, it became clear that the desired functionality with the t-flag could not be added unless the underlying data-structures were reworked. Thus the only way to be successful was to rewrite the whole thing. od(1) allows giving arbitrarily many type-specs per call, both via -t x1o2... and -t x1 -t o2 and intermixed. This fortunately is easy to parse. Now, to be flexible, it should not only support types of integral length. Erroring out like this is inacceptable: $ echo -n "shrek"| od -t u3 od: invalid type string ‘u3’; this system doesn't provide a 3-byte integral type Thus, this new od(1) just collects the bytes until shortly before printing, when the numbers are written into a long long with the proper offset. The bytes per line are just the lcm of all given type-lengths and >= 16. They are equal to 16 for all types that are possible to print using the old od(1)'s. Endianness is of course also supported, needs some testing though, especially on Big Endian systems.
This commit is contained in:
parent
7b9988c7ff
commit
092c95b66c
2
README
2
README
|
@ -56,7 +56,7 @@ The following tools are implemented:
|
||||||
=*|o nice .
|
=*|o nice .
|
||||||
#*|o nl .
|
#*|o nl .
|
||||||
=*|o nohup .
|
=*|o nohup .
|
||||||
od -t
|
=* o od .
|
||||||
#*|o paste .
|
#*|o paste .
|
||||||
=*|x printenv .
|
=*|x printenv .
|
||||||
#*|o printf .
|
#*|o printf .
|
||||||
|
|
12
od.1
12
od.1
|
@ -31,3 +31,15 @@ he\fIx\fRadecimal. If unspecified, the default is octal.
|
||||||
.It Fl v
|
.It Fl v
|
||||||
Always set. Write all input data, including duplicate lines.
|
Always set. Write all input data, including duplicate lines.
|
||||||
.El
|
.El
|
||||||
|
.Sh STANDARDS
|
||||||
|
The
|
||||||
|
.Nm
|
||||||
|
utility is compliant with the
|
||||||
|
.St -p1003.1-2013
|
||||||
|
specification.
|
||||||
|
.Pp
|
||||||
|
The
|
||||||
|
.Op Fl v
|
||||||
|
flag is enabled by default and the 'd' parameter for the
|
||||||
|
.Op Fl t
|
||||||
|
flag is interpreted as 'u'.
|
||||||
|
|
225
od.c
225
od.c
|
@ -1,33 +1,44 @@
|
||||||
/* See LICENSE file for copyright and license details. */
|
/* See LICENSE file for copyright and license details. */
|
||||||
#include <ctype.h>
|
#include <endian.h>
|
||||||
#include <inttypes.h>
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "queue.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
static size_t bytes_per_line = 16;
|
struct type {
|
||||||
static off_t maxbytes = -1;
|
unsigned char format;
|
||||||
|
unsigned int len;
|
||||||
|
TAILQ_ENTRY(type) entry;
|
||||||
|
};
|
||||||
|
|
||||||
|
static TAILQ_HEAD(head, type) head = TAILQ_HEAD_INITIALIZER(head);
|
||||||
|
static unsigned char addr_format = 'o';
|
||||||
static off_t skip = 0;
|
static off_t skip = 0;
|
||||||
static unsigned char radix = 'o';
|
static off_t max = -1;
|
||||||
static unsigned char type = 'o';
|
static size_t linelen = 1;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
printaddress(FILE *f, off_t addr)
|
printaddress(off_t addr)
|
||||||
{
|
{
|
||||||
char fmt[] = "%07j#";
|
char fmt[] = "%07j#";
|
||||||
|
|
||||||
if (radix == 'n') {
|
if (addr_format == 'n') {
|
||||||
fputc(' ', f);
|
fputc(' ', stdout);
|
||||||
} else {
|
} else {
|
||||||
fmt[4] = radix;
|
fmt[4] = addr_format;
|
||||||
fprintf(f, fmt, (intmax_t)addr);
|
printf(fmt, (intmax_t)addr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
printchar(FILE *f, unsigned char c)
|
printchunk(unsigned char *s, unsigned char format, size_t len) {
|
||||||
{
|
long long res, basefac;
|
||||||
|
size_t i;
|
||||||
|
char fmt[] = " %0*ll#";
|
||||||
|
|
||||||
const char *namedict[] = {
|
const char *namedict[] = {
|
||||||
"nul", "soh", "stx", "etx", "eot", "enq", "ack",
|
"nul", "soh", "stx", "etx", "eot", "enq", "ack",
|
||||||
"bel", "bs", "ht", "nl", "vt", "ff", "cr",
|
"bel", "bs", "ht", "nl", "vt", "ff", "cr",
|
||||||
|
@ -41,72 +52,127 @@ printchar(FILE *f, unsigned char c)
|
||||||
['\n'] = "\\n", ['\v'] = "\\v",
|
['\n'] = "\\n", ['\v'] = "\\v",
|
||||||
['\f'] = "\\f", ['\r'] = "\\r",
|
['\f'] = "\\f", ['\r'] = "\\r",
|
||||||
};
|
};
|
||||||
const char *fmtdict[] = {
|
|
||||||
['d'] = "%4hhd ", ['o'] = "%03hho ",
|
|
||||||
['u'] = "%3hhu ", ['x'] = "%02hhx ",
|
|
||||||
};
|
|
||||||
|
|
||||||
switch (type) {
|
switch (format) {
|
||||||
case 'a':
|
case 'a':
|
||||||
c &= ~128; /* clear high bit as required by standard */
|
*s &= ~128; /* clear high bit as required by standard */
|
||||||
if (c < LEN(namedict) || c == 127) {
|
if (*s < LEN(namedict) || *s == 127) {
|
||||||
fprintf(f, "%3s ", (c == 127) ? "del" : namedict[c]);
|
printf(" %3s", (*s == 127) ? "del" : namedict[*s]);
|
||||||
} else {
|
} else {
|
||||||
fprintf(f, "%3c ", c);
|
printf(" %3c", *s);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'c':
|
case 'c':
|
||||||
if (strchr("\a\b\t\n\v\f\r\0", c)) {
|
if (strchr("\a\b\t\n\v\f\r\0", *s)) {
|
||||||
fprintf(f, "%3s ", escdict[c]);
|
printf(" %3s", escdict[*s]);
|
||||||
} else {
|
} else {
|
||||||
fprintf(f, "%3c ", c);
|
printf(" %3c", *s);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(f, fmtdict[type], c);
|
res = 0;
|
||||||
|
basefac = 1;
|
||||||
|
#if __BYTE_ORDER == __BIG_ENDIAN
|
||||||
|
for (i = len; i; i--) {
|
||||||
|
res += s[i - 1] * basefac;
|
||||||
|
#else
|
||||||
|
for (i = 0; i < len; i++) {
|
||||||
|
res += s[i] * basefac;
|
||||||
|
#endif
|
||||||
|
basefac <<= 8;
|
||||||
|
}
|
||||||
|
fmt[6] = format;
|
||||||
|
printf(fmt, (int)(3 * len + len - 1), res);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
od(FILE *in, char *in_name, FILE *out, char *out_name)
|
printline(unsigned char *line, size_t len, off_t addr)
|
||||||
{
|
{
|
||||||
off_t addr;
|
struct type *t = NULL;
|
||||||
size_t i, chunklen;
|
size_t i;
|
||||||
unsigned char buf[BUFSIZ];
|
int first = 1;
|
||||||
|
|
||||||
for (addr = 0; (chunklen = fread(buf, 1, BUFSIZ, in)); ) {
|
if (TAILQ_EMPTY(&head))
|
||||||
for (i = 0; i < chunklen && (maxbytes == -1 ||
|
goto once;
|
||||||
(addr - skip) < maxbytes); ++i, ++addr) {
|
TAILQ_FOREACH(t, &head, entry) {
|
||||||
if (addr - skip < 0)
|
once:
|
||||||
continue;
|
if (first) {
|
||||||
if (((addr - skip) % bytes_per_line) == 0) {
|
printaddress(addr);
|
||||||
if (addr - skip)
|
first = 0;
|
||||||
fputc('\n', out);
|
} else {
|
||||||
printaddress(out, addr);
|
printf("%*c", (addr_format == 'n') ? 1 : 7, ' ');
|
||||||
}
|
}
|
||||||
printchar(out, buf[i]);
|
for (i = 0; i < len; ) {
|
||||||
|
printchunk(line + i, t ? t->format : 'o',
|
||||||
|
MIN(len - i, t ? t->len : 4));
|
||||||
|
i += MIN(len - i, t ? t->len : 4);
|
||||||
}
|
}
|
||||||
if (feof(in) || ferror(in) || ferror(out))
|
fputc('\n', stdout);
|
||||||
|
if (TAILQ_EMPTY(&head) || (!len && !first))
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (addr - skip > 0)
|
|
||||||
fputc('\n', out);
|
|
||||||
if (radix != 'n') {
|
|
||||||
printaddress(out, MAX(addr, skip));
|
|
||||||
fputc('\n', out);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
od(FILE *fp, char *fname, int last)
|
||||||
|
{
|
||||||
|
static unsigned char *line;
|
||||||
|
static size_t lineoff;
|
||||||
|
size_t i;
|
||||||
|
unsigned char buf[BUFSIZ];
|
||||||
|
static off_t addr;
|
||||||
|
size_t buflen;
|
||||||
|
|
||||||
|
while (skip - addr) {
|
||||||
|
buflen = fread(buf, 1, MIN(skip - addr, BUFSIZ), fp);
|
||||||
|
addr += buflen;
|
||||||
|
if (feof(fp) || ferror(fp))
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!line)
|
||||||
|
line = emalloc(linelen);
|
||||||
|
|
||||||
|
while ((buflen = fread(buf, 1, max >= 0 ?
|
||||||
|
max - (addr - skip) : BUFSIZ, fp))) {
|
||||||
|
for (i = 0; i < buflen; i++, addr++) {
|
||||||
|
line[lineoff++] = buf[i];
|
||||||
|
if (lineoff == linelen) {
|
||||||
|
printline(line, lineoff, addr - lineoff + 1);
|
||||||
|
lineoff = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (lineoff)
|
||||||
|
printline(line, lineoff, addr - lineoff);
|
||||||
|
printline((unsigned char *)"", 0, addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
lcm(unsigned int a, unsigned int b)
|
||||||
|
{
|
||||||
|
unsigned int c, d, e;
|
||||||
|
|
||||||
|
for (c = a, d = b; c ;) {
|
||||||
|
e = c;
|
||||||
|
c = d % c;
|
||||||
|
d = e;
|
||||||
|
}
|
||||||
|
|
||||||
|
return a / d * b;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
eprintf("usage: %s [-A d|o|x|n] [-t a|c|d|o|u|x] [-v] [file ...]\n", argv0);
|
eprintf("usage: %s", argv0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
|
struct type *t;
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
char *s;
|
char *s;
|
||||||
|
|
||||||
|
@ -115,31 +181,78 @@ 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();
|
||||||
radix = s[0];
|
addr_format = s[0];
|
||||||
break;
|
break;
|
||||||
case 'j':
|
case 'j':
|
||||||
if ((skip = parseoffset(EARGF(usage()))) < 0)
|
if ((skip = parseoffset(EARGF(usage()))) < 0)
|
||||||
return 1;
|
usage();
|
||||||
break;
|
break;
|
||||||
case 'N':
|
case 'N':
|
||||||
if ((maxbytes = parseoffset(EARGF(usage()))) < 0)
|
if ((max = parseoffset(EARGF(usage()))) < 0)
|
||||||
return 1;
|
usage();
|
||||||
break;
|
break;
|
||||||
case 't':
|
case 't':
|
||||||
s = EARGF(usage());
|
s = EARGF(usage());
|
||||||
if (strlen(s) != 1 || !strchr("acdoux", s[0]))
|
for (; *s; s++) {
|
||||||
|
t = emalloc(sizeof(struct type));
|
||||||
|
switch (*s) {
|
||||||
|
case 'a':
|
||||||
|
case 'c':
|
||||||
|
t->format = *s;
|
||||||
|
t->len = 1;
|
||||||
|
TAILQ_INSERT_TAIL(&head, t, entry);
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
case 'o':
|
||||||
|
case 'u':
|
||||||
|
case 'x':
|
||||||
|
t->format = *s;
|
||||||
|
/* todo: allow multiple digits */
|
||||||
|
if (*(s+1) > '0' || *(s+1) <= '9') {
|
||||||
|
t->len = *(s+1) - '0';
|
||||||
|
s++;
|
||||||
|
} else {
|
||||||
|
switch (*(s + 1)) {
|
||||||
|
case 'C':
|
||||||
|
t->len = sizeof(char);
|
||||||
|
break;
|
||||||
|
case 'S':
|
||||||
|
t->len = sizeof(short);
|
||||||
|
break;
|
||||||
|
case 'I':
|
||||||
|
t->len = sizeof(int);
|
||||||
|
break;
|
||||||
|
case 'L':
|
||||||
|
t->len = sizeof(long);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
t->len = 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TAILQ_INSERT_TAIL(&head, t, entry);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
usage();
|
usage();
|
||||||
type = s[0];
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'v':
|
case 'v':
|
||||||
/* Always set. Use "uniq -f 1 -c" to handle duplicate lines. */
|
/* always set - use uniq(1) to handle duplicate lines */
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
usage();
|
usage();
|
||||||
} ARGEND;
|
} ARGEND;
|
||||||
|
|
||||||
|
/* line length is lcm of type lengths and >= 16 by doubling */
|
||||||
|
TAILQ_FOREACH(t, &head, entry)
|
||||||
|
linelen = lcm(linelen, t->len);
|
||||||
|
if (TAILQ_EMPTY(&head))
|
||||||
|
linelen = 16;
|
||||||
|
while (linelen < 16)
|
||||||
|
linelen *= 2;
|
||||||
|
|
||||||
if (!argc) {
|
if (!argc) {
|
||||||
od(stdin, "<stdin>", stdout, "<stdout>");
|
od(stdin, "<stdin>", 1);
|
||||||
} else {
|
} else {
|
||||||
for (; *argv; argc--, argv++) {
|
for (; *argv; argc--, argv++) {
|
||||||
if (!strcmp(*argv, "-")) {
|
if (!strcmp(*argv, "-")) {
|
||||||
|
@ -150,7 +263,7 @@ main(int argc, char *argv[])
|
||||||
ret = 1;
|
ret = 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
od(fp, *argv, stdout, "<stdout>");
|
od(fp, *argv, (!*(argv + 1)));
|
||||||
if (fp != stdin && fshut(fp, *argv))
|
if (fp != stdin && fshut(fp, *argv))
|
||||||
ret = 1;
|
ret = 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user