[hackers] [sbase] Add -j and -N to od(1) and update README || FRIGN
commit f8f2a56852511dab0658d4fbcdcc8a0088de1e08
Author: FRIGN <dev_AT_frign.de>
AuthorDate: Wed Sep 30 01:50:56 2015 +0200
Commit: sin <sin_AT_2f30.org>
CommitDate: Wed Sep 30 19:44:10 2015 +0100
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!
diff --git a/README b/README
index 9c2834c..0fde83a 100644
--- a/README
+++ b/README
_AT_@ -54,7 +54,7 @@ The following tools are implemented:
=*|o nice .
#*|o nl .
=*|o nohup .
- od a lot
+ od -t, -v
#*|o paste .
=*|x printenv .
#*|o printf .
diff --git a/od.c b/od.c
index c444421..effe5f5 100644
--- a/od.c
+++ b/od.c
_AT_@ -1,4 +1,5 @@
/* See LICENSE file for copyright and license details. */
+#include <ctype.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
_AT_@ -6,6 +7,8 @@
#include "util.h"
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 type = 'o';
_AT_@ -43,25 +46,24 @@ printchar(FILE *f, unsigned char c)
['u'] = "%3hhu ", ['x'] = "%02hhx ",
};
- if (type != 'a' && type != 'c') {
- fprintf(f, fmtdict[type], c);
- } else {
- switch (type) {
- case 'a':
- c &= ~128; /* clear high bit as required by standard */
- if (c < LEN(namedict) || c == 127) {
- fprintf(f, "%3s ", (c == 127) ? "del" : namedict[c]);
- return;
- }
- break;
- case 'c':
- if (strchr("\a\b\t\n\b\f\r\0", c)) {
- fprintf(f, "%3s ", escdict[c]);
- return;
- }
- break;
+ switch (type) {
+ case 'a':
+ c &= ~128; /* clear high bit as required by standard */
+ if (c < LEN(namedict) || c == 127) {
+ fprintf(f, "%3s ", (c == 127) ? "del" : namedict[c]);
+ } else {
+ fprintf(f, "%3c ", c);
+ }
+ break;
+ case 'c':
+ if (strchr("\a\b\t\n\b\f\r\0", c)) {
+ fprintf(f, "%3s ", escdict[c]);
+ } else {
+ fprintf(f, "%3c ", c);
}
- fprintf(f, "%3c ", c);
+ break;
+ default:
+ fprintf(f, fmtdict[type], c);
}
}
_AT_@ -73,9 +75,12 @@ od(FILE *in, char *in_name, FILE *out, char *out_name)
unsigned char buf[BUFSIZ];
for (addr = 0; (chunklen = fread(buf, 1, BUFSIZ, in)); ) {
- for (i = 0; i < chunklen; ++i, ++addr) {
- if ((addr % bytes_per_line) == 0) {
- if (addr)
+ for (i = 0; i < chunklen && (maxbytes == -1 ||
+ (addr - skip) < maxbytes); ++i, ++addr) {
+ if (addr - skip < 0)
+ continue;
+ if (((addr - skip) % bytes_per_line) == 0) {
+ if (addr - skip)
fputc('\n', out);
printaddress(out, addr);
}
_AT_@ -84,7 +89,8 @@ od(FILE *in, char *in_name, FILE *out, char *out_name)
if (feof(in) || ferror(in) || ferror(out))
break;
}
- fputc('\n', out);
+ if (addr)
+ fputc('\n', out);
if (radix != 'n') {
printaddress(out, addr);
fputc('\n', out);
_AT_@ -111,6 +117,12 @@ main(int argc, char *argv[])
usage();
radix = s[0];
break;
+ case 'j':
+ skip = parseoffset(EARGF(usage()));
+ break;
+ case 'N':
+ maxbytes = parseoffset(EARGF(usage()));
+ break;
case 't':
s = EARGF(usage());
if (strlen(s) != 1 || !strchr("acdoux", s[0]))
Received on Wed Sep 30 2015 - 20:44:18 CEST
This archive was generated by hypermail 2.3.0
: Wed Sep 30 2015 - 20:48:32 CEST