Add -j and -N to od(1) and update README

With parseoffset(), it's rather trivial to implement POSIX' rather
obscure commandments.
The -j and -N-flags should be ready to go!
This commit is contained in:
FRIGN 2015-09-30 01:50:56 +02:00 committed by sin
parent 64929039e9
commit f8f2a56852
2 changed files with 35 additions and 23 deletions

2
README
View File

@ -54,7 +54,7 @@ The following tools are implemented:
=*|o nice . =*|o nice .
#*|o nl . #*|o nl .
=*|o nohup . =*|o nohup .
od a lot od -t, -v
#*|o paste . #*|o paste .
=*|x printenv . =*|x printenv .
#*|o printf . #*|o printf .

32
od.c
View File

@ -1,4 +1,5 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <ctype.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -6,6 +7,8 @@
#include "util.h" #include "util.h"
static size_t bytes_per_line = 16; static size_t bytes_per_line = 16;
static off_t maxbytes = -1;
static off_t skip = 0;
static unsigned char radix = 'o'; static unsigned char radix = 'o';
static unsigned char type = 'o'; static unsigned char type = 'o';
@ -43,25 +46,24 @@ printchar(FILE *f, unsigned char c)
['u'] = "%3hhu ", ['x'] = "%02hhx ", ['u'] = "%3hhu ", ['x'] = "%02hhx ",
}; };
if (type != 'a' && type != 'c') {
fprintf(f, fmtdict[type], c);
} else {
switch (type) { switch (type) {
case 'a': case 'a':
c &= ~128; /* clear high bit as required by standard */ c &= ~128; /* clear high bit as required by standard */
if (c < LEN(namedict) || c == 127) { if (c < LEN(namedict) || c == 127) {
fprintf(f, "%3s ", (c == 127) ? "del" : namedict[c]); fprintf(f, "%3s ", (c == 127) ? "del" : namedict[c]);
return; } else {
fprintf(f, "%3c ", c);
} }
break; break;
case 'c': case 'c':
if (strchr("\a\b\t\n\b\f\r\0", c)) { if (strchr("\a\b\t\n\b\f\r\0", c)) {
fprintf(f, "%3s ", escdict[c]); fprintf(f, "%3s ", escdict[c]);
return; } else {
fprintf(f, "%3c ", c);
} }
break; break;
} default:
fprintf(f, "%3c ", c); fprintf(f, fmtdict[type], c);
} }
} }
@ -73,9 +75,12 @@ od(FILE *in, char *in_name, FILE *out, char *out_name)
unsigned char buf[BUFSIZ]; unsigned char buf[BUFSIZ];
for (addr = 0; (chunklen = fread(buf, 1, BUFSIZ, in)); ) { for (addr = 0; (chunklen = fread(buf, 1, BUFSIZ, in)); ) {
for (i = 0; i < chunklen; ++i, ++addr) { for (i = 0; i < chunklen && (maxbytes == -1 ||
if ((addr % bytes_per_line) == 0) { (addr - skip) < maxbytes); ++i, ++addr) {
if (addr) if (addr - skip < 0)
continue;
if (((addr - skip) % bytes_per_line) == 0) {
if (addr - skip)
fputc('\n', out); fputc('\n', out);
printaddress(out, addr); printaddress(out, addr);
} }
@ -84,6 +89,7 @@ od(FILE *in, char *in_name, FILE *out, char *out_name)
if (feof(in) || ferror(in) || ferror(out)) if (feof(in) || ferror(in) || ferror(out))
break; break;
} }
if (addr)
fputc('\n', out); fputc('\n', out);
if (radix != 'n') { if (radix != 'n') {
printaddress(out, addr); printaddress(out, addr);
@ -111,6 +117,12 @@ main(int argc, char *argv[])
usage(); usage();
radix = s[0]; radix = s[0];
break; break;
case 'j':
skip = parseoffset(EARGF(usage()));
break;
case 'N':
maxbytes = parseoffset(EARGF(usage()));
break;
case 't': case 't':
s = EARGF(usage()); s = EARGF(usage());
if (strlen(s) != 1 || !strchr("acdoux", s[0])) if (strlen(s) != 1 || !strchr("acdoux", s[0]))