#include <sys/param.h>
#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <paths.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "extern.h"
static char diff_path[] = "/usr/bin/diff";
#define WIDTH 126
#define WIDTH_MIN 5
#define MAX_CHECK 768
struct diffline {
STAILQ_ENTRY(diffline) diffentries;
char *left;
char div;
char *right;
};
static void astrcat(char **, const char *);
static void enqueue(char *, char, char *);
static char *mktmpcpy(const char *);
static int istextfile(FILE *);
static int bindiff(FILE *, char *, FILE *, char *);
static void freediff(struct diffline *);
static void int_usage(void);
static int parsecmd(FILE *, FILE *, FILE *);
static void printa(FILE *, size_t);
static void printc(FILE *, size_t, FILE *, size_t);
static void printcol(const char *, size_t *, const size_t);
static void printd(FILE *, size_t);
static void println(const char *, const char, const char *);
static void processq(void);
static void prompt(const char *, const char *);
static void usage(void) __dead2;
static char *xfgets(FILE *);
static STAILQ_HEAD(, diffline) diffhead = STAILQ_HEAD_INITIALIZER(diffhead);
static size_t line_width;
static size_t width;
static size_t file1ln, file2ln;
static bool Iflag;
static bool lflag;
static bool sflag;
static bool tflag;
static int tabsize = 8;
FILE *outfp;
const char *tmpdir;
enum {
HELP_OPT = CHAR_MAX + 1,
NORMAL_OPT,
FCASE_SENSITIVE_OPT,
FCASE_IGNORE_OPT,
STRIPCR_OPT,
TSIZE_OPT,
DIFFPROG_OPT,
};
static struct option longopts[] = {
{ "suppress-common-lines", no_argument, NULL, 's' },
{ "width", required_argument, NULL, 'w' },
{ "output", required_argument, NULL, 'o' },
{ "diff-program", required_argument, NULL, DIFFPROG_OPT },
{ "ignore-file-name-case", no_argument, NULL, FCASE_IGNORE_OPT },
{ "no-ignore-file-name-case", no_argument, NULL, FCASE_SENSITIVE_OPT },
{ "strip-trailing-cr", no_argument, NULL, STRIPCR_OPT },
{ "tabsize", required_argument, NULL, TSIZE_OPT },
{ "help", no_argument, NULL, HELP_OPT },
{ "text", no_argument, NULL, 'a' },
{ "ignore-blank-lines", no_argument, NULL, 'B' },
{ "ignore-space-change", no_argument, NULL, 'b' },
{ "minimal", no_argument, NULL, 'd' },
{ "ignore-tab-expansion", no_argument, NULL, 'E' },
{ "ignore-matching-lines", required_argument, NULL, 'I' },
{ "ignore-case", no_argument, NULL, 'i' },
{ "left-column", no_argument, NULL, 'l' },
{ "expand-tabs", no_argument, NULL, 't' },
{ "speed-large-files", no_argument, NULL, 'H' },
{ "ignore-all-space", no_argument, NULL, 'W' },
{ NULL, 0, NULL, '\0'}
};
static const char *help_msg[] = {
"usage: sdiff [-abdilstW] [-I regexp] [-o outfile] [-w width] file1 file2\n",
"-l, --left-column: only print the left column for identical lines.",
"-o OUTFILE, --output=OUTFILE: interactively merge file1 and file2 into outfile.",
"-s, --suppress-common-lines: skip identical lines.",
"-w WIDTH, --width=WIDTH: print a maximum of WIDTH characters on each line.",
"",
"Options passed to diff(1) are:",
"\t-a, --text: treat file1 and file2 as text files.",
"\t-b, --ignore-trailing-cr: ignore trailing blank spaces.",
"\t-d, --minimal: minimize diff size.",
"\t-I RE, --ignore-matching-lines=RE: ignore changes whose line matches RE.",
"\t-i, --ignore-case: do a case-insensitive comparison.",
"\t-t, --expand-tabs: expand tabs to spaces.",
"\t-W, --ignore-all-space: ignore all whitespace.",
"\t--speed-large-files: assume large file with scattered changes.",
"\t--strip-trailing-cr: strip trailing carriage return.",
"\t--ignore-file-name-case: ignore case of file names.",
"\t--no-ignore-file-name-case: do not ignore file name case",
"\t--tabsize NUM: change size of tabs (default 8.)",
NULL,
};
static char *
mktmpcpy(const char *source_file)
{
struct stat sb;
ssize_t rcount;
int ifd, ofd;
u_char buf[BUFSIZ];
char *target_file;
ifd = open(source_file, O_RDONLY, 0);
if (ifd != -1) {
if (fstat(ifd, &sb) == -1)
err(2, "error getting file status from %s", source_file);
if (S_ISREG(sb.st_mode)) {
close(ifd);
return (NULL);
}
} else {
if (errno == ENOENT && strcmp(source_file, "-") == 0)
ifd = STDIN_FILENO;
else
err(2, "error opening %s", source_file);
}
if (asprintf(&target_file, "%s/sdiff.XXXXXXXXXX", tmpdir) == -1)
err(2, "asprintf");
if ((ofd = mkstemp(target_file)) == -1) {
warn("error opening %s", target_file);
goto FAIL;
}
while ((rcount = read(ifd, buf, sizeof(buf))) != -1 &&
rcount != 0) {
ssize_t wcount;
wcount = write(ofd, buf, (size_t)rcount);
if (-1 == wcount || rcount != wcount) {
warn("error writing to %s", target_file);
goto FAIL;
}
}
if (rcount == -1) {
warn("error reading from %s", source_file);
goto FAIL;
}
close(ifd);
close(ofd);
return (target_file);
FAIL:
unlink(target_file);
exit(2);
}
int
main(int argc, char **argv)
{
FILE *diffpipe, *file1, *file2;
size_t diffargc = 0, flagc = 0, wval = WIDTH;
int ch, fd[2], i, ret, status;
pid_t pid;
const char *errstr, *outfile = NULL;
char **diffargv, *diffprog = diff_path, *flagv;
char *filename1, *filename2, *tmp1, *tmp2, *s1, *s2;
char I_arg[] = "-I";
char speed_lf[] = "--speed-large-files";
if ((diffargv = calloc(argc, sizeof(char *) * 2)) == NULL)
err(2, NULL);
diffargv[diffargc++] = diffprog;
if ((flagv = malloc(flagc + 2)) == NULL)
err(2, NULL);
flagv[flagc] = '-';
flagv[flagc + 1] = '\0';
diffargv[diffargc++] = flagv;
while ((ch = getopt_long(argc, argv, "aBbdEHI:ilo:stWw:",
longopts, NULL)) != -1) {
switch (ch) {
case FCASE_IGNORE_OPT:
case FCASE_SENSITIVE_OPT:
case STRIPCR_OPT:
case 'S':
break;
case 'a':
case 'B':
case 'b':
case 'd':
case 'E':
case 'i':
case 'W':
flagc++;
flagv = realloc(flagv, flagc + 2);
flagv[flagc] = ch == 'W' ? 'w' : ch;
flagv[flagc + 1] = '\0';
break;
case 'H':
diffargv[diffargc++] = speed_lf;
break;
case DIFFPROG_OPT:
diffargv[0] = diffprog = optarg;
break;
case 'I':
Iflag = true;
diffargv[diffargc++] = I_arg;
diffargv[diffargc++] = optarg;
break;
case 'l':
lflag = true;
break;
case 'o':
outfile = optarg;
break;
case 's':
sflag = true;
break;
case 't':
tflag = true;
break;
case 'w':
wval = strtonum(optarg, WIDTH_MIN,
INT_MAX, &errstr);
if (errstr)
errx(2, "width is %s: %s", errstr, optarg);
break;
case HELP_OPT:
for (i = 0; help_msg[i] != NULL; i++)
printf("%s\n", help_msg[i]);
exit(0);
break;
case TSIZE_OPT:
tabsize = strtonum(optarg, 1, INT_MAX, &errstr);
if (errstr)
errx(2, "tabsize is %s: %s", errstr, optarg);
break;
default:
usage();
break;
}
}
if (flagc == 0) {
memmove(diffargv + 1, diffargv + 2,
sizeof(char *) * (diffargc - 2));
diffargc--;
free(flagv);
}
argc -= optind;
argv += optind;
if (argc != 2)
usage();
if (outfile && (outfp = fopen(outfile, "w")) == NULL)
err(2, "could not open: %s", optarg);
if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0')
tmpdir = _PATH_TMP;
filename1 = argv[0];
filename2 = argv[1];
tmp1 = tmp2 = NULL;
if (strcmp(filename1, filename2) == 0) {
if ((tmp1 = mktmpcpy(filename1)))
filename1 = filename2 = tmp1;
} else {
if ((tmp1 = mktmpcpy(filename1)))
filename1 = tmp1;
if ((tmp2 = mktmpcpy(filename2)))
filename2 = tmp2;
}
if ((file1 = fopen(filename1, "r")) == NULL)
err(2, "could not open %s", filename1);
if ((file2 = fopen(filename2, "r")) == NULL)
err(2, "could not open %s", filename2);
if (!istextfile(file1) || !istextfile(file2)) {
ret = bindiff(file1, filename1, file2, filename2);
goto done;
}
diffargv[diffargc++] = filename1;
diffargv[diffargc++] = filename2;
diffargv[diffargc++] = NULL;
width = (wval - 3) / 2;
if (width > (SIZE_MAX - 3) / 2)
errx(2, "width is too large: %zu", width);
line_width = width * 2 + 3;
if (pipe(fd))
err(2, "pipe");
if ((pid = fork()) < 0)
err(1, "fork()");
if (pid == 0) {
close(fd[0]);
if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO)
_exit(2);
close(fd[1]);
execvp(diffprog, diffargv);
_exit(2);
}
close(fd[1]);
if ((diffpipe = fdopen(fd[0], "r")) == NULL)
err(2, "could not open diff pipe");
file1ln = file2ln = 1;
while (parsecmd(diffpipe, file1, file2) != EOF)
;
fclose(diffpipe);
if (waitpid(pid, &status, 0) == -1 || !WIFEXITED(status) ||
WEXITSTATUS(status) >= 2)
errx(2, "diff exited abnormally");
ret = WEXITSTATUS(status);
if (lflag)
while ((s1 = xfgets(file1)))
enqueue(s1, ' ', NULL);
else
for (;;) {
s1 = xfgets(file1);
s2 = xfgets(file2);
if (s1 || s2)
enqueue(s1, ' ', s2);
else
break;
}
fclose(file1);
fclose(file2);
processq();
done:
if (tmp1 != NULL) {
if (unlink(tmp1) != 0)
warn("failed to delete %s", tmp1);
free(tmp1);
}
if (tmp2 != NULL) {
if (unlink(tmp2) != 0)
warn("failed to delete %s", tmp2);
free(tmp2);
}
free(diffargv);
if (flagc > 0)
free(flagv);
return (ret);
}
static int
bindiff(FILE *f1, char *fn1, FILE *f2, char *fn2)
{
int ch1, ch2;
flockfile(f1);
flockfile(f2);
do {
ch1 = getc_unlocked(f1);
ch2 = getc_unlocked(f2);
} while (ch1 != EOF && ch2 != EOF && ch1 == ch2);
funlockfile(f2);
funlockfile(f1);
if (ferror(f1)) {
warn("%s", fn1);
return (2);
}
if (ferror(f2)) {
warn("%s", fn2);
return (2);
}
if (ch1 != EOF || ch2 != EOF) {
printf("Binary files %s and %s differ\n", fn1, fn2);
return (1);
}
return (0);
}
static int
istextfile(FILE *f)
{
int ch, i;
if (f == NULL)
return (1);
rewind(f);
for (i = 0; i <= MAX_CHECK; i++) {
ch = fgetc(f);
if (ch == '\0') {
rewind(f);
return (0);
}
if (ch == EOF)
break;
}
rewind(f);
return (1);
}
static void
printcol(const char *s, size_t *col, const size_t col_max)
{
for (; *s && *col < col_max; ++s) {
size_t new_col;
switch (*s) {
case '\t':
if (*col > SIZE_MAX - tabsize)
return;
new_col = (*col / tabsize + 1) * tabsize;
if (new_col > col_max)
return;
if (tflag) {
do {
putchar(' ');
} while (++*col < new_col);
} else {
putchar(*s);
*col = new_col;
}
break;
default:
++*col;
putchar(*s);
}
}
}
static void
prompt(const char *s1, const char *s2)
{
char *cmd;
putchar('%');
for (; (cmd = xfgets(stdin)); free(cmd)) {
const char *p;
for (p = cmd; isspace((unsigned char)*p); ++p)
;
switch (*p) {
case 'e':
++p;
if (eparse(p, s1, s2) == -1)
goto USAGE;
break;
case 'l':
case '1':
if (s1 != NULL)
fprintf(outfp, "%s\n", s1);
break;
case 'q':
goto QUIT;
case 'r':
case '2':
if (s2 != NULL)
fprintf(outfp, "%s\n", s2);
break;
case 's':
sflag = true;
goto PROMPT;
case 'v':
sflag = false;
default:
USAGE:
int_usage();
PROMPT:
putchar('%');
continue;
}
free(cmd);
return;
}
QUIT:
fclose(outfp);
exit(0);
}
static void
println(const char *s1, const char divider, const char *s2)
{
size_t col;
col = 0;
if (s1) {
printcol(s1, &col, width);
}
for (; col < width; ++col)
putchar(' ');
if (divider == ' ' && !s2) {
printf(" (\n");
return;
}
if (!s2) {
printf(" %c\n", divider);
return;
}
printf(" %c ", divider);
col += 3;
printcol(s2, &col, line_width);
putchar('\n');
}
static char *
xfgets(FILE *file)
{
size_t linecap;
ssize_t l;
char *s;
clearerr(file);
linecap = 0;
s = NULL;
if ((l = getline(&s, &linecap, file)) == -1) {
if (ferror(file))
err(2, "error reading file");
return (NULL);
}
if (s[l-1] == '\n')
s[l-1] = '\0';
return (s);
}
static int
parsecmd(FILE *diffpipe, FILE *file1, FILE *file2)
{
size_t file1start, file1end, file2start, file2end, n;
char *line, *p, *q;
const char *errstr;
char c, cmd;
if (!(line = xfgets(diffpipe)))
return (EOF);
p = line;
while (isdigit((unsigned char)*p))
++p;
c = *p;
*p++ = 0;
file1start = strtonum(line, 0, INT_MAX, &errstr);
if (errstr)
errx(2, "file1 start is %s: %s", errstr, line);
if (c == ',') {
q = p;
while (isdigit((unsigned char)*p))
++p;
c = *p;
*p++ = 0;
file1end = strtonum(q, 0, INT_MAX, &errstr);
if (errstr)
errx(2, "file1 end is %s: %s", errstr, line);
if (file1start > file1end)
errx(2, "invalid line range in file1: %s", line);
} else
file1end = file1start;
cmd = c;
if (!(cmd == 'a' || cmd == 'c' || cmd == 'd'))
errx(2, "ed command not recognized: %c: %s", cmd, line);
q = p;
while (isdigit((unsigned char)*p))
++p;
c = *p;
*p++ = 0;
file2start = strtonum(q, 0, INT_MAX, &errstr);
if (errstr)
errx(2, "file2 start is %s: %s", errstr, line);
if (c != ',' && c != '\0')
errx(2, "invalid line range in file2: %c: %s", c, line);
if (c == ',') {
file2end = strtonum(p, 0, INT_MAX, &errstr);
if (errstr)
errx(2, "file2 end is %s: %s", errstr, line);
if (file2start >= file2end)
errx(2, "invalid line range in file2: %s", line);
} else
file2end = file2start;
if (cmd == 'a') {
if (file1start != file1end)
errx(2, "append cannot have a file1 range: %s",
line);
if (file1start == SIZE_MAX)
errx(2, "file1 line range too high: %s", line);
file1start = ++file1end;
}
else if (cmd == 'd') {
if (file2start != file2end)
errx(2, "delete cannot have a file2 range: %s",
line);
if (file2start == SIZE_MAX)
errx(2, "file2 line range too high: %s", line);
file2start = ++file2end;
}
for (; file1ln < file1start && file2ln < file2start;
++file1ln, ++file2ln) {
char *s1, *s2;
if (!(s1 = xfgets(file1)))
errx(2, "file1 shorter than expected");
if (!(s2 = xfgets(file2)))
errx(2, "file2 shorter than expected");
if (lflag) {
free(s2);
if (Iflag)
enqueue(s1, '(', NULL);
else
enqueue(s1, ' ', NULL);
} else
enqueue(s1, ' ', s2);
}
for (; file1ln < file1start; ++file1ln) {
char *s;
if (!(s = xfgets(file1)))
errx(2, "file1 shorter than expected");
enqueue(s, '(', NULL);
}
for (; file2ln < file2start; ++file2ln) {
char *s;
if (!(s = xfgets(file2)))
errx(2, "file2 shorter than expected");
if (lflag)
free(s);
else
enqueue(NULL, ')', s);
}
processq();
switch (cmd) {
case 'a':
printa(file2, file2end);
n = file2end - file2start + 1;
break;
case 'c':
printc(file1, file1end, file2, file2end);
n = file1end - file1start + 1 + 1 + file2end - file2start + 1;
break;
case 'd':
printd(file1, file1end);
n = file1end - file1start + 1;
break;
default:
errx(2, "invalid diff command: %c: %s", cmd, line);
}
free(line);
while (n--) {
if (!(line = xfgets(diffpipe)))
errx(2, "diff ended early");
free(line);
}
return (0);
}
static void
enqueue(char *left, char divider, char *right)
{
struct diffline *diffp;
if (!(diffp = malloc(sizeof(struct diffline))))
err(2, "enqueue");
diffp->left = left;
diffp->div = divider;
diffp->right = right;
STAILQ_INSERT_TAIL(&diffhead, diffp, diffentries);
}
static void
freediff(struct diffline *diffp)
{
free(diffp->left);
free(diffp->right);
free(diffp);
}
static void
astrcat(char **s, const char *append)
{
static size_t offset = 0;
size_t newsiz;
static const char *oldstr = NULL;
char *newstr;
if (!*s) {
if (!(*s = strdup(append)))
err(2, "astrcat");
offset = strlen(*s);
oldstr = *s;
return;
}
if (oldstr != *s) {
offset = strlen(*s);
oldstr = *s;
}
newsiz = offset + 1 + strlen(append) + 1;
newstr = realloc(*s, newsiz);
if (newstr == NULL)
err(2, "astrcat");
*s = newstr;
strlcpy(*s + offset, "\n", newsiz - offset);
strlcat(*s + offset, append, newsiz - offset);
offset = newsiz - 1;
oldstr = *s;
}
static void
processq(void)
{
struct diffline *diffp;
char divc, *left, *right;
if (STAILQ_EMPTY(&diffhead))
return;
divc = STAILQ_FIRST(&diffhead)->div;
left = NULL;
right = NULL;
STAILQ_FOREACH(diffp, &diffhead, diffentries) {
if (!sflag || diffp->div == '|' || diffp->div == '<' ||
diffp->div == '>')
println(diffp->left, diffp->div, diffp->right);
if (diffp->left)
astrcat(&left, diffp->left);
if (diffp->right)
astrcat(&right, diffp->right);
}
while (!STAILQ_EMPTY(&diffhead)) {
diffp = STAILQ_FIRST(&diffhead);
STAILQ_REMOVE_HEAD(&diffhead, diffentries);
freediff(diffp);
}
if (outfp)
switch (divc) {
case ' ': case '(': case ')':
fprintf(outfp, "%s\n", left);
break;
case '|': case '<': case '>':
prompt(left, right);
break;
default:
errx(2, "invalid divider: %c", divc);
}
free(left);
free(right);
}
static void
printa(FILE *file, size_t line2)
{
char *line;
for (; file2ln <= line2; ++file2ln) {
if (!(line = xfgets(file)))
errx(2, "append ended early");
enqueue(NULL, '>', line);
}
processq();
}
static void
printc(FILE *file1, size_t file1end, FILE *file2, size_t file2end)
{
struct fileline {
STAILQ_ENTRY(fileline) fileentries;
char *line;
};
STAILQ_HEAD(, fileline) delqhead = STAILQ_HEAD_INITIALIZER(delqhead);
for (; file1ln <= file1end; ++file1ln) {
struct fileline *linep;
char *line1;
if (!(line1 = xfgets(file1)))
errx(2, "error reading file1 in delete in change");
if (!(linep = malloc(sizeof(struct fileline))))
err(2, "printc");
linep->line = line1;
STAILQ_INSERT_TAIL(&delqhead, linep, fileentries);
}
for (; !STAILQ_EMPTY(&delqhead) && file2ln <= file2end;
++file2ln) {
struct fileline *del;
char *add;
if (!(add = xfgets(file2)))
errx(2, "error reading add in change");
del = STAILQ_FIRST(&delqhead);
enqueue(del->line, '|', add);
STAILQ_REMOVE_HEAD(&delqhead, fileentries);
free(del);
}
processq();
for (; file2ln <= file2end; ++file2ln) {
char *add;
if (!(add = xfgets(file2)))
errx(2, "error reading add in change");
enqueue(NULL, '>', add);
}
processq();
while (!STAILQ_EMPTY(&delqhead)) {
struct fileline *filep;
filep = STAILQ_FIRST(&delqhead);
enqueue(filep->line, '<', NULL);
STAILQ_REMOVE_HEAD(&delqhead, fileentries);
free(filep);
}
processq();
}
static void
printd(FILE *file1, size_t file1end)
{
char *line1;
for (; file1ln <= file1end; ++file1ln) {
if (!(line1 = xfgets(file1)))
errx(2, "file1 ended early in delete");
enqueue(line1, '<', NULL);
}
processq();
}
static void
int_usage(void)
{
puts("e:\tedit blank diff\n"
"eb:\tedit both diffs concatenated\n"
"el:\tedit left diff\n"
"er:\tedit right diff\n"
"l | 1:\tchoose left diff\n"
"r | 2:\tchoose right diff\n"
"s:\tsilent mode--don't print identical lines\n"
"v:\tverbose mode--print identical lines\n"
"q:\tquit");
}
static void
usage(void)
{
fprintf(stderr,
"usage: sdiff [-abdilstHW] [-I regexp] [-o outfile] [-w width] file1"
" file2\n");
exit(2);
}