[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] "od -N" reads too much input data

From: Ian Bruce
Subject: [PATCH] "od -N" reads too much input data
Date: Thu, 06 Sep 2001 08:30:29 -0700

"od -N" exhibits the same problem I reported for head(1): it reads
more input than it needs to. This causes problems when reading from
pipes, character devices, and open file descriptors inherited from a
parent process.

The following commands should produce the same output (modulo
whitespace), but they don't:

$ echo abcdefgh | od -An -tx1 -N6
 61 62 63 64 65 66

$ echo abcdefgh | (od -An -tx1 -N3 ; od -An -tx1 -N3)
 61 62 63

The attached patch fixes this problem:

$ echo abcdefgh | (od -An -tx1 -N3 ; od -An -tx1 -N3)
 61 62 63
 64 65 66

The issue is that od(1) uses the standard I/O library, which by
default does buffered reads. This means that excess data will be read
unless the requested byte count is a multiple of the buffer size,
which is unlikely. The solution is to turn off buffering when the "-N"
option is specified.

The patch also contains a documentation fix for the discrepancy with
the real behaviour of the "-j" and "-N" options which I previously
reported. I would have patched the manual page as well, but it appears
to be autogenerated.

The patch is larger than I had expected; it turned out that the
relevant section of code had been duplicated three ways. I took the
opportunity to make it into a function. The modified version is
marginally shorter than the original and I believe it is easier to
read. I was careful to preserve the original program logic.

-- Ian Bruce  <ian dot bruce at myrealbox dot com>

Attachment: od.patch.gz
Description: application/gzip

reply via email to

[Prev in Thread] Current Thread [Next in Thread]