aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2022-10-04 23:22:27 -0500
committerRob Landley <rob@landley.net>2022-10-04 23:22:27 -0500
commitb5baa040128e8cc4cda5b5e982f03a0ed92acce3 (patch)
treed16040a400949e9c82399f0475e4d0db2c0fcf48
parent891c5520f493802a78c80ec20429eb107ce72241 (diff)
downloadtoybox-b5baa040128e8cc4cda5b5e982f03a0ed92acce3.tar.gz
Fix comm - and make comm -123 detect missing files.
-rwxr-xr-xtests/comm.test7
-rw-r--r--toys/posix/comm.c16
2 files changed, 13 insertions, 10 deletions
diff --git a/tests/comm.test b/tests/comm.test
index 63d9c847..2891c6b2 100755
--- a/tests/comm.test
+++ b/tests/comm.test
@@ -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" "" ""
diff --git a/toys/posix/comm.c b/toys/posix/comm.c
index 2d118b7b..91359246 100644
--- a/toys/posix/comm.c
+++ b/toys/posix/comm.c
@@ -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]);
}