hexdump: fix short file of zero butes treated as dup

function                                             old     new   delta
bb_dump_dump                                        1466    1491     +25

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2018-07-03 16:27:54 +02:00
parent 9634e8a7d5
commit e5d5f5b9a7
2 changed files with 27 additions and 5 deletions

View File

@ -387,7 +387,10 @@ static unsigned char *get(priv_dumper_t *dumper)
if (need == blocksize) { if (need == blocksize) {
return NULL; return NULL;
} }
if (dumper->pub.dump_vflag != ALL && !memcmp(dumper->get__curp, dumper->get__savp, nread)) { if (dumper->pub.dump_vflag != ALL /* not "show all"? */
&& dumper->pub.dump_vflag != FIRST /* not first line? */
&& memcmp(dumper->get__curp, dumper->get__savp, nread) == 0 /* same data? */
) {
if (dumper->pub.dump_vflag != DUP) { if (dumper->pub.dump_vflag != DUP) {
puts("*"); puts("*");
} }
@ -399,7 +402,7 @@ static unsigned char *get(priv_dumper_t *dumper)
} }
n = fread(dumper->get__curp + nread, sizeof(unsigned char), n = fread(dumper->get__curp + nread, sizeof(unsigned char),
dumper->pub.dump_length == -1 ? need : MIN(dumper->pub.dump_length, need), stdin); dumper->pub.dump_length == -1 ? need : MIN(dumper->pub.dump_length, need), stdin);
if (!n) { if (n == 0) {
if (ferror(stdin)) { if (ferror(stdin)) {
bb_simple_perror_msg(dumper->argv[-1]); bb_simple_perror_msg(dumper->argv[-1]);
} }
@ -411,9 +414,10 @@ static unsigned char *get(priv_dumper_t *dumper)
dumper->pub.dump_length -= n; dumper->pub.dump_length -= n;
} }
need -= n; need -= n;
if (!need) { if (need == 0) {
if (dumper->pub.dump_vflag == ALL || dumper->pub.dump_vflag == FIRST if (dumper->pub.dump_vflag == ALL /* "show all"? */
|| memcmp(dumper->get__curp, dumper->get__savp, blocksize) || dumper->pub.dump_vflag == FIRST /* first line? */
|| memcmp(dumper->get__curp, dumper->get__savp, blocksize) != 0 /* not same data? */
) { ) {
if (dumper->pub.dump_vflag == DUP || dumper->pub.dump_vflag == FIRST) { if (dumper->pub.dump_vflag == DUP || dumper->pub.dump_vflag == FIRST) {
dumper->pub.dump_vflag = WAIT; dumper->pub.dump_vflag = WAIT;

18
testsuite/hexdump.tests Executable file
View File

@ -0,0 +1,18 @@
#!/bin/sh
# Copyright 2018 by Denys Vlasenko <vda.linux@googlemail.com>
# Licensed under GPLv2, see file LICENSE in this source tree.
. ./testing.sh
# testing "description" "command" "result" "infile" "stdin"
testing 'hexdump -C with four NULs' \
'hexdump -C' \
"\
00000000 00 00 00 00 |....|
00000004
" \
'' \
'\0\0\0\0'
exit $FAILCOUNT