Fix comm - and make comm -123 detect missing files.

This commit is contained in:
Rob Landley 2022-10-04 23:22:27 -05:00
parent 891c5520f4
commit b5baa04012
2 changed files with 13 additions and 10 deletions

View File

@ -4,6 +4,9 @@
#testing "name" "command" "result" "infile" "stdin"
for i in a b c ; do echo $i >> lhs ; done
echo -e 'a\nb\nc'> lhs
for i in c d e ; do echo $i >> rhs ; done
testing "comm" "comm lhs rhs" "a\nb\n\t\tc\n\td\n\te\n" "" ""
testing "comm" "comm lhs input" "a\nb\n\t\tc\n\td\n\te\n" "c\nd\ne\n" ""
testing "comm -" "comm - input" "a\nb\n\t\tc\n\td\n\te\n" "c\nd\ne\n" "a\nb\nc\n"
testing "comm -123 detects missing" "comm - missing 2>/dev/null || echo here" \
"here\n" "" ""

View File

@ -43,26 +43,26 @@ void comm_main(void)
{
FILE *file[2];
char *line[2];
int i;
int i = 0;
if (toys.optflags == 7) return;
for (i = 0; i < 2; i++) {
file[i] = xfopen(toys.optargs[i], "r");
for (i = 0; i<2; i++) {
file[i] = strcmp(toys.optargs[i], "-")?xfopen(toys.optargs[i], "r"):stdin;
line[i] = xgetline(file[i]);
}
if (toys.optflags == 7) return;
while (line[0] && line[1]) {
int order = strcmp(line[0], line[1]);
if (order == 0) {
if (!order) {
writeline(line[0], 2);
for (i = 0; i < 2; i++) {
free(line[i]);
line[i] = xgetline(file[i]);
}
} else {
i = order < 0 ? 0 : 1;
i = order>0;
writeline(line[i], i);
free(line[i]);
line[i] = xgetline(file[i]);
@ -76,5 +76,5 @@ void comm_main(void)
line[i] = xgetline(file[i]);
}
if (CFG_TOYBOX_FREE) for (i = 0; i < 2; i++) fclose(file[i]);
if (CFG_TOYBOX_FREE) fclose(file[0]), fclose(file[1]);
}