Properly handle partial chunks in od(1)

Grab the remaining bytes and fill them up with zeroes in a
temporary buffer.
This commit is contained in:
FRIGN 2015-10-26 12:29:02 +01:00 committed by sin
parent 914991f5f6
commit 1eff1e8214

16
od.c
View File

@ -93,6 +93,7 @@ printline(unsigned char *line, size_t len, off_t addr)
struct type *t = NULL; struct type *t = NULL;
size_t i; size_t i;
int first = 1; int first = 1;
unsigned char *tmp;
if (TAILQ_EMPTY(&head)) if (TAILQ_EMPTY(&head))
goto once; goto once;
@ -104,10 +105,17 @@ once:
} else { } else {
printf("%*c", (addr_format == 'n') ? 1 : 7, ' '); printf("%*c", (addr_format == 'n') ? 1 : 7, ' ');
} }
for (i = 0; i < len; ) { for (i = 0; i < len; i += MIN(len - i, t ? t->len : 4)) {
printchunk(line + i, t ? t->format : 'o', if (len - i < (t ? t->len : 4)) {
MIN(len - i, t ? t->len : 4)); tmp = ecalloc(t ? t->len : 4, 1);
i += MIN(len - i, t ? t->len : 4); 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);
}
} }
fputc('\n', stdout); fputc('\n', stdout);
if (TAILQ_EMPTY(&head) || (!len && !first)) if (TAILQ_EMPTY(&head) || (!len && !first))