2015-09-29 01:02:17 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-10-08 23:46:56 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2015-09-29 01:02:17 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-10-08 23:46:56 +00:00
|
|
|
#include "queue.h"
|
2015-09-29 01:02:17 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-10-08 23:46:56 +00:00
|
|
|
struct type {
|
|
|
|
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';
|
2015-09-29 23:50:56 +00:00
|
|
|
static off_t skip = 0;
|
2015-10-08 23:46:56 +00:00
|
|
|
static off_t max = -1;
|
|
|
|
static size_t linelen = 1;
|
2015-10-25 22:26:49 +00:00
|
|
|
static int big_endian;
|
2015-09-29 01:02:17 +00:00
|
|
|
|
|
|
|
static void
|
2015-10-08 23:46:56 +00:00
|
|
|
printaddress(off_t addr)
|
2015-09-29 01:02:17 +00:00
|
|
|
{
|
2015-10-08 23:46:56 +00:00
|
|
|
char fmt[] = "%07j#";
|
2015-09-29 01:02:17 +00:00
|
|
|
|
2015-10-08 23:46:56 +00:00
|
|
|
if (addr_format == 'n') {
|
|
|
|
fputc(' ', stdout);
|
2015-09-29 01:02:17 +00:00
|
|
|
} else {
|
2015-10-08 23:46:56 +00:00
|
|
|
fmt[4] = addr_format;
|
|
|
|
printf(fmt, (intmax_t)addr);
|
2015-09-29 01:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-10-26 11:56:37 +00:00
|
|
|
printchunk(unsigned char *s, unsigned char format, size_t len)
|
|
|
|
{
|
2015-10-08 23:46:56 +00:00
|
|
|
long long res, basefac;
|
|
|
|
size_t i;
|
2015-10-25 22:26:49 +00:00
|
|
|
char fmt[] = " %#*ll#";
|
2015-10-08 23:46:56 +00:00
|
|
|
|
2015-09-29 21:09:59 +00: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",
|
|
|
|
};
|
|
|
|
|
2015-10-08 23:46:56 +00:00
|
|
|
switch (format) {
|
2015-09-29 23:50:56 +00:00
|
|
|
case 'a':
|
2015-10-08 23:46:56 +00:00
|
|
|
*s &= ~128; /* clear high bit as required by standard */
|
|
|
|
if (*s < LEN(namedict) || *s == 127) {
|
|
|
|
printf(" %3s", (*s == 127) ? "del" : namedict[*s]);
|
2015-09-29 23:50:56 +00:00
|
|
|
} else {
|
2015-10-08 23:46:56 +00:00
|
|
|
printf(" %3c", *s);
|
2015-09-29 23:50:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'c':
|
2015-10-08 23:46:56 +00:00
|
|
|
if (strchr("\a\b\t\n\v\f\r\0", *s)) {
|
|
|
|
printf(" %3s", escdict[*s]);
|
2015-09-29 23:50:56 +00:00
|
|
|
} else {
|
2015-10-08 23:46:56 +00:00
|
|
|
printf(" %3c", *s);
|
2015-09-29 21:09:59 +00:00
|
|
|
}
|
2015-09-29 23:50:56 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-10-26 11:13:34 +00:00
|
|
|
if (big_endian) {
|
|
|
|
for (res = 0, basefac = 1, i = len; i; i--) {
|
|
|
|
res += s[i - 1] * basefac;
|
2015-10-25 22:26:49 +00:00
|
|
|
basefac <<= 8;
|
|
|
|
}
|
|
|
|
} else {
|
2015-10-26 11:13:34 +00:00
|
|
|
for (res = 0, basefac = 1, i = 0; i < len; i++) {
|
|
|
|
res += s[i] * basefac;
|
2015-10-25 22:26:49 +00:00
|
|
|
basefac <<= 8;
|
|
|
|
}
|
2015-10-08 23:46:56 +00:00
|
|
|
}
|
2015-10-25 23:08:59 +00:00
|
|
|
fmt[2] = big_endian ? '-' : ' ';
|
2015-10-08 23:46:56 +00:00
|
|
|
fmt[6] = format;
|
|
|
|
printf(fmt, (int)(3 * len + len - 1), res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
printline(unsigned char *line, size_t len, off_t addr)
|
|
|
|
{
|
|
|
|
struct type *t = NULL;
|
|
|
|
size_t i;
|
|
|
|
int first = 1;
|
2015-10-26 11:29:02 +00:00
|
|
|
unsigned char *tmp;
|
2015-10-08 23:46:56 +00:00
|
|
|
|
|
|
|
if (TAILQ_EMPTY(&head))
|
|
|
|
goto once;
|
|
|
|
TAILQ_FOREACH(t, &head, entry) {
|
|
|
|
once:
|
|
|
|
if (first) {
|
|
|
|
printaddress(addr);
|
|
|
|
first = 0;
|
|
|
|
} else {
|
|
|
|
printf("%*c", (addr_format == 'n') ? 1 : 7, ' ');
|
|
|
|
}
|
2015-10-26 11:29:02 +00:00
|
|
|
for (i = 0; i < len; i += MIN(len - i, t ? t->len : 4)) {
|
|
|
|
if (len - i < (t ? t->len : 4)) {
|
|
|
|
tmp = ecalloc(t ? t->len : 4, 1);
|
|
|
|
memcpy(tmp, line + i, len - i);
|
|
|
|
printchunk(tmp, t ? t->format : 'o',
|
|
|
|
t ? t->len : 4);
|
|
|
|
free(tmp);
|
|
|
|
} else {
|
|
|
|
printchunk(line + i, t ? t->format : 'o',
|
|
|
|
t ? t->len : 4);
|
|
|
|
}
|
2015-10-08 23:46:56 +00:00
|
|
|
}
|
|
|
|
fputc('\n', stdout);
|
|
|
|
if (TAILQ_EMPTY(&head) || (!len && !first))
|
|
|
|
break;
|
2015-09-29 01:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-10-08 23:46:56 +00:00
|
|
|
od(FILE *fp, char *fname, int last)
|
2015-09-29 01:02:17 +00:00
|
|
|
{
|
2015-10-08 23:46:56 +00:00
|
|
|
static unsigned char *line;
|
|
|
|
static size_t lineoff;
|
|
|
|
size_t i;
|
2015-09-29 01:02:17 +00:00
|
|
|
unsigned char buf[BUFSIZ];
|
2015-10-08 23:46:56 +00:00
|
|
|
static off_t addr;
|
|
|
|
size_t buflen;
|
2015-09-29 01:02:17 +00:00
|
|
|
|
2015-10-26 16:21:15 +00:00
|
|
|
while (skip - addr > 0) {
|
2015-10-08 23:46:56 +00:00
|
|
|
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;
|
2015-09-29 01:02:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-26 16:21:15 +00:00
|
|
|
if (lineoff && last)
|
2015-10-08 23:46:56 +00:00
|
|
|
printline(line, lineoff, addr - lineoff);
|
2015-10-26 16:21:15 +00:00
|
|
|
if (last)
|
|
|
|
printline((unsigned char *)"", 0, addr);
|
2015-10-08 23:46:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2015-09-29 21:09:59 +00:00
|
|
|
}
|
2015-10-08 23:46:56 +00:00
|
|
|
|
|
|
|
return a / d * b;
|
2015-09-29 22:08:58 +00:00
|
|
|
}
|
|
|
|
|
2016-07-08 17:24:07 +00:00
|
|
|
static void
|
|
|
|
addtype(char format, int len)
|
|
|
|
{
|
|
|
|
struct type *t;
|
|
|
|
|
|
|
|
t = emalloc(sizeof(*t));
|
|
|
|
t->format = format;
|
|
|
|
t->len = len;
|
|
|
|
TAILQ_INSERT_TAIL(&head, t, entry);
|
|
|
|
}
|
|
|
|
|
2015-09-29 22:08:58 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2016-07-08 17:24:07 +00:00
|
|
|
eprintf("usage: %s [-bdosvx] [-A addressformat] [-E | -e] [-j skip] "
|
|
|
|
"[-t outputformat] [file ...]\n", argv0);
|
2015-09-29 01:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FILE *fp;
|
2015-10-08 23:46:56 +00:00
|
|
|
struct type *t;
|
2016-07-08 17:24:08 +00:00
|
|
|
int ret = 0, len;
|
2015-09-29 21:09:59 +00:00
|
|
|
char *s;
|
2015-09-29 01:02:17 +00:00
|
|
|
|
2015-10-26 11:13:34 +00:00
|
|
|
big_endian = (*(uint16_t *)"\0\xff" == 0xff);
|
2015-10-25 22:26:49 +00:00
|
|
|
|
2015-09-29 01:02:17 +00:00
|
|
|
ARGBEGIN {
|
|
|
|
case 'A':
|
|
|
|
s = EARGF(usage());
|
2015-09-29 21:14:59 +00:00
|
|
|
if (strlen(s) != 1 || !strchr("doxn", s[0]))
|
2015-09-29 01:02:17 +00:00
|
|
|
usage();
|
2015-10-08 23:46:56 +00:00
|
|
|
addr_format = s[0];
|
2015-09-29 01:02:17 +00:00
|
|
|
break;
|
2016-07-08 17:24:07 +00:00
|
|
|
case 'b':
|
|
|
|
addtype('o', 1);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
addtype('u', 2);
|
|
|
|
break;
|
2015-10-25 22:26:49 +00:00
|
|
|
case 'E':
|
|
|
|
case 'e':
|
|
|
|
big_endian = (ARGC() == 'E');
|
|
|
|
break;
|
2015-09-29 23:50:56 +00:00
|
|
|
case 'j':
|
2015-09-30 17:13:32 +00:00
|
|
|
if ((skip = parseoffset(EARGF(usage()))) < 0)
|
2015-10-08 23:46:56 +00:00
|
|
|
usage();
|
2015-09-29 23:50:56 +00:00
|
|
|
break;
|
|
|
|
case 'N':
|
2015-10-08 23:46:56 +00:00
|
|
|
if ((max = parseoffset(EARGF(usage()))) < 0)
|
|
|
|
usage();
|
2015-09-29 23:50:56 +00:00
|
|
|
break;
|
2016-07-08 17:24:07 +00:00
|
|
|
case 'o':
|
|
|
|
addtype('o', 2);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
addtype('d', 2);
|
|
|
|
break;
|
2015-09-29 01:02:17 +00:00
|
|
|
case 't':
|
2015-09-29 21:09:59 +00:00
|
|
|
s = EARGF(usage());
|
2015-10-08 23:46:56 +00:00
|
|
|
for (; *s; s++) {
|
|
|
|
switch (*s) {
|
|
|
|
case 'a':
|
|
|
|
case 'c':
|
2016-07-08 17:24:07 +00:00
|
|
|
addtype(*s, 1);
|
2015-10-08 23:46:56 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
case 'o':
|
|
|
|
case 'u':
|
|
|
|
case 'x':
|
|
|
|
/* todo: allow multiple digits */
|
2015-10-26 11:52:14 +00:00
|
|
|
if (*(s+1) > '0' && *(s+1) <= '9') {
|
2016-07-08 17:24:08 +00:00
|
|
|
len = *(s+1) - '0';
|
2015-10-08 23:46:56 +00:00
|
|
|
} else {
|
2016-07-08 17:24:08 +00:00
|
|
|
switch (*(s+1)) {
|
2015-10-08 23:46:56 +00:00
|
|
|
case 'C':
|
2016-07-08 17:24:08 +00:00
|
|
|
len = sizeof(char);
|
2015-10-08 23:46:56 +00:00
|
|
|
break;
|
|
|
|
case 'S':
|
2016-07-08 17:24:08 +00:00
|
|
|
len = sizeof(short);
|
2015-10-08 23:46:56 +00:00
|
|
|
break;
|
|
|
|
case 'I':
|
2016-07-08 17:24:08 +00:00
|
|
|
len = sizeof(int);
|
2015-10-08 23:46:56 +00:00
|
|
|
break;
|
|
|
|
case 'L':
|
2016-07-08 17:24:08 +00:00
|
|
|
len = sizeof(long);
|
2015-10-08 23:46:56 +00:00
|
|
|
break;
|
|
|
|
default:
|
2016-07-08 17:24:08 +00:00
|
|
|
len = sizeof(int);
|
2015-10-08 23:46:56 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-08 17:24:08 +00:00
|
|
|
addtype(*s++, len);
|
2015-10-08 23:46:56 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
2015-09-29 01:02:17 +00:00
|
|
|
break;
|
Implement od(1) v-flag
If this flag is not given, od(1) automatically replaces duplicate
adjacent lines with an '*' for each reoccurence.
If this flag is set, thus, no such filtering occurs.
In this case this would mean having to somehow keep the last printed
line in some backbuffer, building the next line and then doing the
necessary comparisons. This basically means that we duplicate the
functionality provided with uniq(1).
So instead of
$ od -t a > dump
you'd rather do
$ od -t a | uniq -f 1 -c > dump
Skipping the first field is necessary, as the addresses obviously differ.
Now, I was thinking hard why this flag even exists. If POSIX mandated
to add the address before the asterisk, so we know the offset of duplicate
occurrences, this would make sense. However, this is not the case.
Using uniq(1) also gives nicer output:
~ $ echo "111111111111111111111111111111111111111111111111" | od -t a -v | uniq -f 1 -c
3 0000000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0000060 nl
1 0000061
in comparison to
$ echo "111111111111111111111111111111111111111111111111" | od -t a
0000000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
*
0000060 nl
0000061
Before working on od(1), I didn't even know it would filter out
duplicate adjacent lines like that. This is also a matter of
predictability.
Concluding, the v-flag is implicitly set and users urged to just
use the existing tools provided by the system.
I don't think we would break scripts either. Firstly, it's rather
unlikely to have duplicate lines exactly matching the line-length of
od(1). Secondly, even if a script did that specifically, in the worst
case there would be a counting error or something.
Given od(1) is mostly used interactively, we can safely assume this
feature is for the benefit of the users.
Ditch this legacy POSIX crap!
Please enter the commit message for your changes. Lines starting
2015-09-30 10:54:24 +00:00
|
|
|
case 'v':
|
2015-10-08 23:46:56 +00:00
|
|
|
/* always set - use uniq(1) to handle duplicate lines */
|
Implement od(1) v-flag
If this flag is not given, od(1) automatically replaces duplicate
adjacent lines with an '*' for each reoccurence.
If this flag is set, thus, no such filtering occurs.
In this case this would mean having to somehow keep the last printed
line in some backbuffer, building the next line and then doing the
necessary comparisons. This basically means that we duplicate the
functionality provided with uniq(1).
So instead of
$ od -t a > dump
you'd rather do
$ od -t a | uniq -f 1 -c > dump
Skipping the first field is necessary, as the addresses obviously differ.
Now, I was thinking hard why this flag even exists. If POSIX mandated
to add the address before the asterisk, so we know the offset of duplicate
occurrences, this would make sense. However, this is not the case.
Using uniq(1) also gives nicer output:
~ $ echo "111111111111111111111111111111111111111111111111" | od -t a -v | uniq -f 1 -c
3 0000000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0000060 nl
1 0000061
in comparison to
$ echo "111111111111111111111111111111111111111111111111" | od -t a
0000000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
*
0000060 nl
0000061
Before working on od(1), I didn't even know it would filter out
duplicate adjacent lines like that. This is also a matter of
predictability.
Concluding, the v-flag is implicitly set and users urged to just
use the existing tools provided by the system.
I don't think we would break scripts either. Firstly, it's rather
unlikely to have duplicate lines exactly matching the line-length of
od(1). Secondly, even if a script did that specifically, in the worst
case there would be a counting error or something.
Given od(1) is mostly used interactively, we can safely assume this
feature is for the benefit of the users.
Ditch this legacy POSIX crap!
Please enter the commit message for your changes. Lines starting
2015-09-30 10:54:24 +00:00
|
|
|
break;
|
2016-07-08 17:24:07 +00:00
|
|
|
case 'x':
|
|
|
|
addtype('x', 2);
|
|
|
|
break;
|
2015-09-29 01:02:17 +00:00
|
|
|
default:
|
|
|
|
usage();
|
2015-11-01 10:16:49 +00:00
|
|
|
} ARGEND
|
2015-09-29 01:02:17 +00:00
|
|
|
|
2015-10-08 23:46:56 +00:00
|
|
|
/* 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;
|
|
|
|
|
2015-09-29 01:02:17 +00:00
|
|
|
if (!argc) {
|
2015-10-08 23:46:56 +00:00
|
|
|
od(stdin, "<stdin>", 1);
|
2015-09-29 01:02:17 +00:00
|
|
|
} 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;
|
|
|
|
}
|
2015-10-08 23:46:56 +00:00
|
|
|
od(fp, *argv, (!*(argv + 1)));
|
2015-09-29 01:02:17 +00:00
|
|
|
if (fp != stdin && fshut(fp, *argv))
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-29 21:09:59 +00:00
|
|
|
ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>") |
|
|
|
|
fshut(stderr, "<stderr>");
|
2015-09-29 01:02:17 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|