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.
This commit is contained in:
parent
5dc6f3c1e6
commit
ac62259d24
9
comm.c
9
comm.c
@ -33,7 +33,7 @@ main(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
FILE *fp[2];
|
FILE *fp[2];
|
||||||
size_t linelen[2] = { 0, 0 };
|
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 };
|
char *line[2] = { NULL, NULL };
|
||||||
|
|
||||||
ARGBEGIN {
|
ARGBEGIN {
|
||||||
@ -62,11 +62,13 @@ main(int argc, char *argv[])
|
|||||||
for (i = 0; i < 2; i++) {
|
for (i = 0; i < 2; i++) {
|
||||||
if (diff && i == (diff < 0))
|
if (diff && i == (diff < 0))
|
||||||
continue;
|
continue;
|
||||||
if (getline(&line[i], &linelen[i], fp[i]) > 0)
|
if (getline(&line[i], &linelen[i], fp[i]) > 0) {
|
||||||
|
seenline = 1;
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
if (ferror(fp[i]))
|
if (ferror(fp[i]))
|
||||||
eprintf("getline %s:", argv[i]);
|
eprintf("getline %s:", argv[i]);
|
||||||
if (diff && line[!i][0])
|
if ((diff || seenline) && line[!i][0])
|
||||||
printline(!i, line[!i]);
|
printline(!i, line[!i]);
|
||||||
while (getline(&line[!i], &linelen[!i], fp[!i]) > 0)
|
while (getline(&line[!i], &linelen[!i], fp[!i]) > 0)
|
||||||
printline(!i, line[!i]);
|
printline(!i, line[!i]);
|
||||||
@ -76,6 +78,7 @@ main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
diff = strcmp(line[0], line[1]);
|
diff = strcmp(line[0], line[1]);
|
||||||
LIMIT(diff, -1, 1);
|
LIMIT(diff, -1, 1);
|
||||||
|
seenline = 0;
|
||||||
printline((2 - diff) % 3, line[MAX(0, diff)]);
|
printline((2 - diff) % 3, line[MAX(0, diff)]);
|
||||||
}
|
}
|
||||||
end:
|
end:
|
||||||
|
Loading…
Reference in New Issue
Block a user