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