2013-06-18 10:19:47 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-11-13 18:54:28 +00:00
|
|
|
#include <limits.h>
|
2013-06-18 10:19:47 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2013-06-18 10:19:47 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2013-06-19 19:09:50 +00:00
|
|
|
#define CLAMP(x, l, h) MIN(h, MAX(l, x))
|
2013-06-18 10:19:47 +00:00
|
|
|
|
2013-06-19 19:09:50 +00:00
|
|
|
static void printline(int, char *);
|
|
|
|
static char *nextline(char *, int, FILE *, char *);
|
|
|
|
static void finish(int, FILE *, char *);
|
|
|
|
|
|
|
|
static int show = 0x07;
|
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[])
|
|
|
|
{
|
2014-04-04 10:05:47 +00:00
|
|
|
int i, diff = 0;
|
2013-06-19 19:09:50 +00:00
|
|
|
FILE *fp[2];
|
|
|
|
char lines[2][LINE_MAX+1];
|
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();
|
|
|
|
} ARGEND;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (argc != 2)
|
2013-06-18 10:19:47 +00:00
|
|
|
usage();
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
for (i = 0; i < LEN(fp); i++) {
|
2014-11-13 15:38:39 +00:00
|
|
|
if (argv[i][0] == '-')
|
|
|
|
argv[i] = "/dev/fd/0";
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!(fp[i] = fopen(argv[i], "r")))
|
2014-11-17 16:22:01 +00:00
|
|
|
eprintf("fopen %s:", argv[i]);
|
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 (;;) {
|
|
|
|
if (diff <= 0) {
|
2014-11-13 15:55:02 +00:00
|
|
|
lines[0][0] = '\0';
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!nextline(lines[0], sizeof(lines[0]),
|
2014-11-13 15:38:39 +00:00
|
|
|
fp[0], argv[0])) {
|
2014-11-13 15:55:02 +00:00
|
|
|
if (lines[1][0] != '\0')
|
|
|
|
printline(1, lines[1]);
|
2013-06-19 19:09:50 +00:00
|
|
|
finish(1, fp[1], argv[1]);
|
|
|
|
}
|
|
|
|
}
|
2014-11-13 17:29:30 +00:00
|
|
|
if (diff >= 0) {
|
2014-11-13 15:55:02 +00:00
|
|
|
lines[1][0] = '\0';
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!nextline(lines[1], sizeof(lines[1]),
|
2014-11-13 15:38:39 +00:00
|
|
|
fp[1], argv[1])) {
|
2014-11-13 15:55:02 +00:00
|
|
|
if (lines[0][0] != '\0')
|
|
|
|
printline(0, lines[0]);
|
2013-06-19 19:09:50 +00:00
|
|
|
finish(0, fp[0], argv[0]);
|
|
|
|
}
|
|
|
|
}
|
2014-04-04 10:05:47 +00:00
|
|
|
diff = strcmp(lines[0], lines[1]);
|
|
|
|
diff = CLAMP(diff, -1, 1);
|
|
|
|
printline((2-diff) % 3, lines[MAX(0, diff)]);
|
2013-06-19 19:09:50 +00:00
|
|
|
}
|
2013-06-18 10:19:47 +00:00
|
|
|
|
2014-10-02 22:46:04 +00:00
|
|
|
return 0;
|
2013-06-18 10:19:47 +00:00
|
|
|
}
|
|
|
|
|
2014-06-01 12:59:47 +00:00
|
|
|
static void
|
2013-06-19 19:09:50 +00:00
|
|
|
printline(int pos, char *line)
|
2013-06-18 10:19:47 +00:00
|
|
|
{
|
2013-06-19 19:09:50 +00:00
|
|
|
int i;
|
2013-06-18 10:19:47 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!(show & (0x1 << pos)))
|
2013-06-18 10:19:47 +00:00
|
|
|
return;
|
2013-06-19 19:09:50 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
for (i = 0; i < pos; i++) {
|
|
|
|
if (show & (0x1 << i))
|
2013-06-19 19:09:50 +00:00
|
|
|
putchar('\t');
|
|
|
|
}
|
|
|
|
printf("%s", line);
|
2013-06-18 10:19:47 +00:00
|
|
|
}
|
|
|
|
|
2014-06-01 12:59:47 +00:00
|
|
|
static char *
|
2013-06-19 19:09:50 +00:00
|
|
|
nextline(char *buf, int n, FILE *f, char *name)
|
2013-06-18 10:19:47 +00:00
|
|
|
{
|
2013-06-19 19:09:50 +00:00
|
|
|
buf = fgets(buf, n, f);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!buf && !feof(f))
|
2014-11-17 16:22:01 +00:00
|
|
|
eprintf("%s: read error:", name);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (buf && !strchr(buf, '\n'))
|
2014-11-17 16:22:01 +00:00
|
|
|
eprintf("%s: line too long\n", name);
|
2013-06-19 19:09:50 +00:00
|
|
|
return buf;
|
2013-06-18 10:19:47 +00:00
|
|
|
}
|
|
|
|
|
2014-06-01 12:59:47 +00:00
|
|
|
static void
|
2013-06-19 19:09:50 +00:00
|
|
|
finish(int pos, FILE *f, char *name)
|
2013-06-18 10:19:47 +00:00
|
|
|
{
|
2013-06-19 19:09:50 +00:00
|
|
|
char buf[LINE_MAX+1];
|
2013-06-18 10:19:47 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
while (nextline(buf, sizeof(buf), f, name))
|
2013-06-19 19:09:50 +00:00
|
|
|
printline(pos, buf);
|
2014-10-02 22:46:04 +00:00
|
|
|
exit(1);
|
2013-06-18 10:19:47 +00:00
|
|
|
}
|