Make cmp's TEST_HOST pass on current debin, adjusting command behavior to match.

This commit is contained in:
Rob Landley 2022-06-19 03:49:36 -05:00
parent 1d657cff59
commit 85c9503625
2 changed files with 16 additions and 11 deletions

View File

@ -22,7 +22,8 @@ c" > input2
testcmd "identical files, stdout" "input input2" "" "ab\nc\n" ""
testcmd "identical files, return code" "input input2 && echo yes" "yes\n" "ab\nc\n" ""
testcmd "EOF, stderr" "input input2 2>&1" "cmp: EOF on input2\n" "ab\nc\nx" ""
testcmd "EOF, stderr" "input input2 2>&1" \
"cmp: EOF on input2 after byte 5, line 2\n" "ab\nc\nx" ""
testcmd "EOF, return code" "input input2 2>/dev/null || echo yes" "yes\n" "ab\nc\nx" ""
# The gnu/dammit version fails this because posix says "char" and they don't.
testcmd "diff, stdout" "input input2 | sed s/byte/char/" \
@ -32,8 +33,10 @@ testcmd "diff, return code" "input input2 > /dev/null || echo yes" "yes\n" "ab\n
testcmd "-s EOF, return code" "-s input input2 2>&1 || echo yes" "yes\n" "ab\nc\nx" ""
testcmd "-s diff, return code" "-s input input2 2>&1 || echo yes" "yes\n" "ab\nx\nx" ""
testcmd "-l EOF, stderr" "-l input input2 2>&1" "cmp: EOF on input2\n" "ab\nc\nx" ""
testcmd "-l diff and EOF, stdout and stderr" "-l input input2 2>&1 | sort" "4 170 143\ncmp: EOF on input2\n" "ab\nx\nx" ""
testcmd "-l EOF, stderr" "-l input input2 2>&1" \
"cmp: EOF on input2 after byte 5\n" "ab\nc\nx" ""
testcmd "-l diff and EOF, stdout and stderr" "-l input input2 2>&1 | sort" \
"4 170 143\ncmp: EOF on input2 after byte 5\n" "ab\nx\nx" ""
testcmd "-s not exist" "-s input doesnotexist 2>&1 || echo yes" "yes\n" "x" ""

View File

@ -34,11 +34,10 @@ GLOBALS(
static void do_cmp(int fd, char *name)
{
int i, len1, len2, min_len, size = sizeof(toybuf)/2;
long byte_no = 1, line_no = 1;
long long byte_no = 1, line_no = 1;
char *buf2 = toybuf+size;
if (toys.optc>(i = 2+!!TT.fd) && lskip(fd, atolx(toys.optargs[i])))
error_exit("EOF on %s", name);
if (toys.optc>(i = 2+!!TT.fd)) lskip(fd, atolx(toys.optargs[i]));
// First time through, cache the data and return.
if (!TT.fd) {
@ -56,12 +55,12 @@ static void do_cmp(int fd, char *name)
len1 = readall(TT.fd, toybuf, size);
len2 = readall(fd, buf2, size);
min_len = minof(len1, len2);
for (i=0; i<min_len; i++) {
for (i = 0; i<min_len; i++) {
if (toybuf[i] != buf2[i]) {
toys.exitval = 1;
if (FLAG(l)) printf("%ld %o %o\n", byte_no, toybuf[i], buf2[i]);
if (FLAG(l)) printf("%lld %o %o\n", byte_no, toybuf[i], buf2[i]);
else {
if (!FLAG(s)) printf("%s %s differ: char %ld, line %ld\n",
if (!FLAG(s)) printf("%s %s differ: char %lld, line %lld\n",
TT.name, name, byte_no, line_no);
goto out;
}
@ -70,8 +69,11 @@ static void do_cmp(int fd, char *name)
if (toybuf[i] == '\n') line_no++;
}
if (len1 != len2) {
if (!FLAG(s)) error_msg("EOF on %s", len1 < len2 ? TT.name : name);
else toys.exitval = 1;
if (!FLAG(s)) {
strcpy(toybuf, "EOF on %s after byte %lld, line %lld");
if (FLAG(l)) *strchr(toybuf, ',') = 0;
error_msg(toybuf, len1 < len2 ? TT.name : name, byte_no-1, line_no-1);
} else toys.exitval = 1;
break;
}
if (len1 < 1) break;