Commit Graph

27 Commits

Author SHA1 Message Date
Michael Forney
d098ac4abc od: For the 'c' type, format non-printable charecters as octal
This is the behavior specified by POSIX.
2018-02-25 23:01:57 -08:00
Michael Forney
f83c468b55 od: Don't modify bytes in the line for the 'a' type
Subsequent types should still print the full 8-bit value.
2018-02-25 23:01:00 -08:00
Michael Forney
9a3b12525b Don't use buffered IO (fread) when not appropriate
fread reads the entire requested size (BUFSIZ), which causes tools to
block if only small amounts of data are available at a time. At best,
this causes unnecessary copies and inefficiency, at worst, tools like
tee and cat are almost unusable in some cases since they only display
large chunks of data at a time.
2017-07-03 21:04:14 +02:00
Michael Forney
5e4e6aeb3e od: Fix buffer overflow if -N flag is larger than BUFSIZ
Previously, if max was specified, od will call read with that size,
potentially overflowing buf with data read from the file.
2016-12-27 14:32:04 +01:00
Michael Forney
49e1854600 od: Don't advance past end of type string
Currently, if you specify -t x, then s is advanced once in the switch statement
to determine the length, and then once again in the for loop, resulting in a
read past the end of the argument.

Also, use sizeof(int) when no length is specified, as specified by POSIX.
2016-07-09 10:17:16 +01:00
Michael Forney
5ae2793da6 od: Add some missing type flags 2016-07-09 10:17:16 +01:00
Greg Reagle
c0a63db7f2 od: document skip (-j) option 2015-11-06 10:18:56 +00:00
sin
2366164de7 No need for semicolon after ARGEND
This is also the style used in Plan 9.
2015-11-01 10:18:55 +00:00
FRIGN
91ab4e80b9 Fix multiple file handling
1) Fix skip overflow for negative numbers
2) Print proper trailing lines and addresses according to the last
   element.
2015-10-26 16:53:28 +00:00
sin
1e4ab5ec9a Minor style fix 2015-10-26 11:57:04 +00:00
FRIGN
ecb351f1d5 Properly parse numbers in od(1)
Previously, it'd drop right through in the number case and return
crazy-long numbers (like 28 for L), resulting in unexpected behaviour.
2015-10-26 11:55:41 +00:00
FRIGN
1eff1e8214 Properly handle partial chunks in od(1)
Grab the remaining bytes and fill them up with zeroes in a
temporary buffer.
2015-10-26 11:55:41 +00:00
FRIGN
914991f5f6 Fix remaining endian-issue in od(1)
After setting up qemu and testing od(1) in a Big Endian environment,
I found out that the conditional in the printing function was not
right.
Instead, it's supposed to be way simpler. While at it, we don't need
HOST_BIG_ENDIAN any more. Just set big_endian properly in main()
and be done with it.
2015-10-26 11:55:41 +00:00
FRIGN
eeccb0a5ea Fix od(1) output
Of course, for right-justification we want no "+"'s in the output.
2015-10-26 11:55:41 +00:00
FRIGN
d03baf1697 Add -e and -E flags to od(1) and properly handle endianness
The -e and -E flags allow the user to override the host endianness
and force od(1) to handle input according to a little (-e) or big (-E)
endian environment.
The previous handling was broken as bitshifts alone are already
endian-independent.
2015-10-26 11:55:41 +00:00
FRIGN
c619e168c9 Update od(1)-documentation 2015-10-26 11:55:41 +00:00
FRIGN
092c95b66c 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.
2015-10-26 11:55:41 +00:00
FRIGN
ea986ea504 Match \v as escaped character
Thanks Greg for noticing this!
2015-10-02 14:48:21 +01:00
FRIGN
98e7e75ec6 Fix last offset output using skip in od(1)
Yeah, if the skipping is longer than the file itself, we need
to take the skip value, not the address.
Also, only print the last newline when we've actually printed
at least 1 address.
2015-09-30 19:44:11 +01:00
FRIGN
8163451249 Check parseoffset return value in od(1)
Yeah, kinda embarassing. Forgot about that.
2015-09-30 19:44:10 +01:00
FRIGN
fc886aa144 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 19:44:10 +01:00
FRIGN
f8f2a56852 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!
2015-09-30 19:44:10 +01:00
FRIGN
7132473947 Some small changes for od(1)
1) Move usage() down above main().
2) Consistently use printaddress() across the code.
3) Use off_t instead of size_t for file offsets.
2015-09-30 19:44:10 +01:00
FRIGN
365f392d3c Properly handle namedict in od(1)
I added the [127]-index but forgot that this increases the length,
breaking a later check.
2015-09-30 19:44:10 +01:00
FRIGN
0d6d1dc02f Check explicit length in od(1)
If you pass an empty string, the null-char will always match
with strchr()!
2015-09-30 19:44:10 +01:00
FRIGN
d49bce7fc8 Refactor parts of od(1)
It was possible to make some sections of the code shorter.
Also fix a bug where the last printed address was always in hex
rather than depending on the radix chosen.
2015-09-30 19:44:10 +01:00
Greg Reagle
fd0d1e4567 Created od, with improvements suggested by FRIGN 2015-09-30 19:44:10 +01:00