[hackers] [sbase] comm: Print first trailing unpaired line in file 1 || Wolfgang Corcoran-Mathe
commit ac62259d242d7e54a2006140e863e9851fedcccb
Author: Wolfgang Corcoran-Mathe <first.lord.of.teal_AT_gmail.com>
AuthorDate: Mon Aug 3 15:30:35 2015 -0400
Commit: sin <sin_AT_2f30.org>
CommitDate: Tue Aug 4 12:35:44 2015 +0100
comm: Print first trailing unpaired line in file 1
Previously, a line read from file 1 before a strcmp was
performed would be overwritten and lost. Something like
this:
comm one_line_file empty_file
produced no output.
This patch is a bit inelegant, but quite simple.
diff --git a/comm.c b/comm.c
index e0ec7b7..138b5ea 100644
--- a/comm.c
+++ b/comm.c
_AT_@ -33,7 +33,7 @@ main(int argc, char *argv[])
{
FILE *fp[2];
size_t linelen[2] = { 0, 0 };
- int ret = 0, i, diff = 0;
+ int ret = 0, i, diff = 0, seenline = 0;
char *line[2] = { NULL, NULL };
ARGBEGIN {
_AT_@ -62,11 +62,13 @@ main(int argc, char *argv[])
for (i = 0; i < 2; i++) {
if (diff && i == (diff < 0))
continue;
- if (getline(&line[i], &linelen[i], fp[i]) > 0)
+ if (getline(&line[i], &linelen[i], fp[i]) > 0) {
+ seenline = 1;
continue;
+ }
if (ferror(fp[i]))
eprintf("getline %s:", argv[i]);
- if (diff && line[!i][0])
+ if ((diff || seenline) && line[!i][0])
printline(!i, line[!i]);
while (getline(&line[!i], &linelen[!i], fp[!i]) > 0)
printline(!i, line[!i]);
_AT_@ -76,6 +78,7 @@ main(int argc, char *argv[])
}
diff = strcmp(line[0], line[1]);
LIMIT(diff, -1, 1);
+ seenline = 0;
printline((2 - diff) % 3, line[MAX(0, diff)]);
}
end:
Received on Tue Aug 04 2015 - 13:35:48 CEST
This archive was generated by hypermail 2.3.0
: Tue Aug 04 2015 - 13:36:14 CEST