2013-06-18 10:19:47 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2016-03-07 00:42:01 +00:00
|
|
|
#include "text.h"
|
2013-06-18 10:19:47 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2013-06-19 19:09:50 +00:00
|
|
|
static int show = 0x07;
|
2013-06-18 10:19:47 +00:00
|
|
|
|
2015-01-17 23:07:29 +00:00
|
|
|
static void
|
2016-03-07 01:04:29 +00:00
|
|
|
printline(int pos, struct line *line)
|
2015-01-17 23:07:29 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!(show & (0x1 << pos)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < pos; i++) {
|
|
|
|
if (show & (0x1 << i))
|
|
|
|
putchar('\t');
|
|
|
|
}
|
2016-03-07 00:42:01 +00:00
|
|
|
fwrite(line->data, 1, line->len, stdout);
|
2015-01-17 23:07:29 +00:00
|
|
|
}
|
|
|
|
|
2013-06-18 10:19:47 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: %s [-123] file1 file2\n", argv0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2013-06-19 19:09:50 +00:00
|
|
|
FILE *fp[2];
|
2016-03-07 01:04:29 +00:00
|
|
|
static struct line line[2];
|
2016-03-07 00:42:01 +00:00
|
|
|
size_t linecap[2] = { 0, 0 };
|
|
|
|
ssize_t len;
|
2015-08-03 19:30:35 +00:00
|
|
|
int ret = 0, i, diff = 0, seenline = 0;
|
2013-06-18 10:19:47 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
2013-06-19 19:09:50 +00:00
|
|
|
show &= 0x07 ^ (1 << (ARGC() - '1'));
|
2013-06-18 10:19:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
2015-11-01 10:16:49 +00:00
|
|
|
} ARGEND
|
2013-06-18 10:19:47 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (argc != 2)
|
2013-06-18 10:19:47 +00:00
|
|
|
usage();
|
|
|
|
|
2015-03-11 12:39:08 +00:00
|
|
|
for (i = 0; i < 2; i++) {
|
2015-05-19 15:44:15 +00:00
|
|
|
if (!strcmp(argv[i], "-")) {
|
2015-03-11 12:39:08 +00:00
|
|
|
argv[i] = "<stdin>";
|
|
|
|
fp[i] = stdin;
|
|
|
|
} else if (!(fp[i] = fopen(argv[i], "r"))) {
|
2014-11-17 16:22:01 +00:00
|
|
|
eprintf("fopen %s:", argv[i]);
|
2015-03-11 12:39:08 +00:00
|
|
|
}
|
2013-06-19 19:09:50 +00:00
|
|
|
}
|
2013-06-18 10:19:47 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
for (;;) {
|
2015-03-11 12:39:08 +00:00
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
if (diff && i == (diff < 0))
|
|
|
|
continue;
|
2016-03-07 00:42:01 +00:00
|
|
|
if ((len = getline(&(line[i].data), &linecap[i],
|
|
|
|
fp[i])) > 0) {
|
|
|
|
line[i].len = len;
|
2015-08-03 19:30:35 +00:00
|
|
|
seenline = 1;
|
2015-03-11 12:39:08 +00:00
|
|
|
continue;
|
2015-08-03 19:30:35 +00:00
|
|
|
}
|
2015-03-11 12:39:08 +00:00
|
|
|
if (ferror(fp[i]))
|
|
|
|
eprintf("getline %s:", argv[i]);
|
2016-03-07 00:42:01 +00:00
|
|
|
if ((diff || seenline) && line[!i].data[0])
|
|
|
|
printline(!i, &line[!i]);
|
|
|
|
while ((len = getline(&(line[!i].data), &linecap[!i],
|
|
|
|
fp[!i])) > 0) {
|
|
|
|
line[!i].len = len;
|
|
|
|
printline(!i, &line[!i]);
|
|
|
|
}
|
2015-03-11 12:39:08 +00:00
|
|
|
if (ferror(fp[!i]))
|
|
|
|
eprintf("getline %s:", argv[!i]);
|
|
|
|
goto end;
|
2013-06-19 19:09:50 +00:00
|
|
|
}
|
2016-03-07 01:04:29 +00:00
|
|
|
diff = linecmp(&line[0], &line[1]);
|
2015-03-11 12:39:08 +00:00
|
|
|
LIMIT(diff, -1, 1);
|
2015-08-03 19:30:35 +00:00
|
|
|
seenline = 0;
|
2016-03-07 00:42:01 +00:00
|
|
|
printline((2 - diff) % 3, &line[MAX(0, diff)]);
|
2013-06-19 19:09:50 +00:00
|
|
|
}
|
2015-03-11 12:39:08 +00:00
|
|
|
end:
|
2015-05-24 23:33:19 +00:00
|
|
|
ret |= fshut(fp[0], argv[0]);
|
|
|
|
ret |= (fp[0] != fp[1]) && fshut(fp[1], argv[1]);
|
|
|
|
ret |= fshut(stdout, "<stdout>");
|
|
|
|
|
|
|
|
return ret;
|
2013-06-18 10:19:47 +00:00
|
|
|
}
|