1 /* $OpenBSD: sdiff.c,v 1.36 2015/12/29 19:04:46 gsoares Exp $ */
4 * Written by Raymond Lai <ray@cyth.net>.
11 #include <sys/param.h>
12 #include <sys/queue.h>
14 #include <sys/types.h>
33 #define DIFF_PATH "/usr/bin/diff"
37 * Each column must be at least one character wide, plus three
38 * characters between the columns (space, [<|>], space).
42 /* 3 kilobytes of chars */
45 /* A single diff line. */
47 STAILQ_ENTRY(diffline) diffentries;
53 static void astrcat(char **, const char *);
54 static void enqueue(char *, char, char *);
55 static char *mktmpcpy(const char *);
56 static int istextfile(FILE *);
57 static void binexec(char *, char *, char *) __dead2;
58 static void freediff(struct diffline *);
59 static void int_usage(void);
60 static int parsecmd(FILE *, FILE *, FILE *);
61 static void printa(FILE *, size_t);
62 static void printc(FILE *, size_t, FILE *, size_t);
63 static void printcol(const char *, size_t *, const size_t);
64 static void printd(FILE *, size_t);
65 static void println(const char *, const char, const char *);
66 static void processq(void);
67 static void prompt(const char *, const char *);
68 static void usage(void) __dead2;
69 static char *xfgets(FILE *);
71 static STAILQ_HEAD(, diffline) diffhead = STAILQ_HEAD_INITIALIZER(diffhead);
72 static size_t line_width; /* width of a line (two columns and divider) */
73 static size_t width; /* width of each column */
74 static size_t file1ln, file2ln; /* line number of file1 and file2 */
75 static int Iflag = 0; /* ignore sets matching regexp */
76 static int lflag; /* print only left column for identical lines */
77 static int sflag; /* skip identical lines */
78 FILE *outfp; /* file to save changes to */
79 const char *tmpdir; /* TMPDIR or /tmp */
82 HELP_OPT = CHAR_MAX + 1,
91 static struct option longopts[] = {
92 /* options only processed in sdiff */
93 { "suppress-common-lines", no_argument, NULL, 's' },
94 { "width", required_argument, NULL, 'w' },
96 { "output", required_argument, NULL, 'o' },
97 { "diff-program", required_argument, NULL, DIFFPROG_OPT },
99 /* Options processed by diff. */
100 { "ignore-file-name-case", no_argument, NULL, FCASE_IGNORE_OPT },
101 { "no-ignore-file-name-case", no_argument, NULL, FCASE_SENSITIVE_OPT },
102 { "strip-trailing-cr", no_argument, NULL, STRIPCR_OPT },
103 { "tabsize", required_argument, NULL, TSIZE_OPT },
104 { "help", no_argument, NULL, HELP_OPT },
105 { "text", no_argument, NULL, 'a' },
106 { "ignore-blank-lines", no_argument, NULL, 'B' },
107 { "ignore-space-change", no_argument, NULL, 'b' },
108 { "minimal", no_argument, NULL, 'd' },
109 { "ignore-tab-expansion", no_argument, NULL, 'E' },
110 { "ignore-matching-lines", required_argument, NULL, 'I' },
111 { "ignore-case", no_argument, NULL, 'i' },
112 { "left-column", no_argument, NULL, 'l' },
113 { "expand-tabs", no_argument, NULL, 't' },
114 { "speed-large-files", no_argument, NULL, 'H' },
115 { "ignore-all-space", no_argument, NULL, 'W' },
117 { NULL, 0, NULL, '\0'}
120 static const char *help_msg[] = {
121 "usage: sdiff [-abdilstW] [-I regexp] [-o outfile] [-w width] file1 file2\n",
122 "-l, --left-column: only print the left column for identical lines.",
123 "-o OUTFILE, --output=OUTFILE: interactively merge file1 and file2 into outfile.",
124 "-s, --suppress-common-lines: skip identical lines.",
125 "-w WIDTH, --width=WIDTH: print a maximum of WIDTH characters on each line.",
127 "Options passed to diff(1) are:",
128 "\t-a, --text: treat file1 and file2 as text files.",
129 "\t-b, --ignore-trailing-cr: ignore trailing blank spaces.",
130 "\t-d, --minimal: minimize diff size.",
131 "\t-I RE, --ignore-matching-lines=RE: ignore changes whose line matches RE.",
132 "\t-i, --ignore-case: do a case-insensitive comparison.",
133 "\t-t, --expand-tabs: sxpand tabs to spaces.",
134 "\t-W, --ignore-all-spaces: ignore all spaces.",
135 "\t--speed-large-files: assume large file with scattered changes.",
136 "\t--strip-trailing-cr: strip trailing carriage return.",
137 "\t--ignore-file-name-case: ignore case of file names.",
138 "\t--no-ignore-file-name-case: do not ignore file name case",
139 "\t--tabsize NUM: change size of tabs (default 8.)",
145 * Create temporary file if source_file is not a regular file.
146 * Returns temporary file name if one was malloced, NULL if unnecessary.
149 mktmpcpy(const char *source_file)
157 /* Open input and output. */
158 ifd = open(source_file, O_RDONLY, 0);
159 /* File was opened successfully. */
161 if (fstat(ifd, &sb) == -1)
162 err(2, "error getting file status from %s", source_file);
165 if (S_ISREG(sb.st_mode)) {
170 /* If ``-'' does not exist the user meant stdin. */
171 if (errno == ENOENT && strcmp(source_file, "-") == 0)
174 err(2, "error opening %s", source_file);
177 /* Not a regular file, so copy input into temporary file. */
178 if (asprintf(&target_file, "%s/sdiff.XXXXXXXXXX", tmpdir) == -1)
180 if ((ofd = mkstemp(target_file)) == -1) {
181 warn("error opening %s", target_file);
184 while ((rcount = read(ifd, buf, sizeof(buf))) != -1 &&
188 wcount = write(ofd, buf, (size_t)rcount);
189 if (-1 == wcount || rcount != wcount) {
190 warn("error writing to %s", target_file);
195 warn("error reading from %s", source_file);
202 return (target_file);
210 main(int argc, char **argv)
212 FILE *diffpipe=NULL, *file1, *file2;
213 size_t diffargc = 0, wflag = WIDTH;
214 int ch, fd[2] = {-1}, status;
216 const char *outfile = NULL;
217 char **diffargv, *diffprog = DIFF_PATH, *filename1, *filename2,
218 *tmp1, *tmp2, *s1, *s2;
222 * Process diff flags.
225 * Allocate memory for diff arguments and NULL.
226 * Each flag has at most one argument, so doubling argc gives an
227 * upper limit of how many diff args can be passed. argv[0],
228 * file1, and file2 won't have arguments so doubling them will
229 * waste some memory; however we need an extra space for the
230 * NULL at the end, so it sort of works out.
232 if (!(diffargv = calloc(argc, sizeof(char **) * 2)))
235 /* Add first argument, the program name. */
236 diffargv[diffargc++] = diffprog;
238 /* create a dynamic string for merging single-switch options */
239 if ( asprintf(&diffargv[diffargc++], "-") < 0 )
242 while ((ch = getopt_long(argc, argv, "aBbdEHI:ilo:stWw:",
243 longopts, NULL)) != -1) {
247 /* only compatible --long-name-form with diff */
248 case FCASE_IGNORE_OPT:
249 case FCASE_SENSITIVE_OPT:
254 /* combine no-arg single switches */
263 diffargv[1] = realloc(diffargv[1], sizeof(char) * strlen(diffargv[1]) + 2);
265 * In diff, the 'W' option is 'w' and the 'w' is 'W'.
268 sprintf(diffargv[1], "%sw", diffargv[1]);
270 sprintf(diffargv[1], "%s%c", diffargv[1], ch);
273 diffargv[diffargc++] = "--speed-large-files";
276 diffargv[0] = diffprog = optarg;
280 diffargv[diffargc++] = "-I";
281 diffargv[diffargc++] = optarg;
293 wflag = strtonum(optarg, WIDTH_MIN,
296 errx(2, "width is %s: %s", errstr, optarg);
299 for (i = 0; help_msg[i] != NULL; i++)
300 printf("%s\n", help_msg[i]);
309 /* no single switches were used */
310 if (strcmp(diffargv[1], "-") == 0 ) {
311 for ( i = 1; i < argc-1; i++) {
312 diffargv[i] = diffargv[i+1];
314 diffargv[diffargc-1] = NULL;
324 if (outfile && (outfp = fopen(outfile, "w")) == NULL)
325 err(2, "could not open: %s", optarg);
327 if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0')
334 * Create temporary files for diff and sdiff to share if file1
335 * or file2 are not regular files. This allows sdiff and diff
336 * to read the same inputs if one or both inputs are stdin.
338 * If any temporary files were created, their names would be
339 * saved in tmp1 or tmp2. tmp1 should never equal tmp2.
342 /* file1 and file2 are the same, so copy to same temp file. */
343 if (strcmp(filename1, filename2) == 0) {
344 if ((tmp1 = mktmpcpy(filename1)))
345 filename1 = filename2 = tmp1;
346 /* Copy file1 and file2 into separate temp files. */
348 if ((tmp1 = mktmpcpy(filename1)))
350 if ((tmp2 = mktmpcpy(filename2)))
354 diffargv[diffargc++] = filename1;
355 diffargv[diffargc++] = filename2;
356 /* Add NULL to end of array to indicate end of array. */
357 diffargv[diffargc++] = NULL;
359 /* Subtract column divider and divide by two. */
360 width = (wflag - 3) / 2;
361 /* Make sure line_width can fit in size_t. */
362 if (width > (SIZE_MAX - 3) / 2)
363 errx(2, "width is too large: %zu", width);
364 line_width = width * 2 + 3;
369 switch (pid = fork()) {
372 /* We don't read from the pipe. */
374 if (dup2(fd[1], STDOUT_FILENO) == -1)
375 err(2, "child could not duplicate descriptor");
376 /* Free unused descriptor. */
378 execvp(diffprog, diffargv);
379 err(2, "could not execute diff: %s", diffprog);
382 err(2, "could not fork");
387 /* We don't write to the pipe. */
390 /* Open pipe to diff command. */
391 if ((diffpipe = fdopen(fd[0], "r")) == NULL)
392 err(2, "could not open diff pipe");
394 if ((file1 = fopen(filename1, "r")) == NULL)
395 err(2, "could not open %s", filename1);
396 if ((file2 = fopen(filename2, "r")) == NULL)
397 err(2, "could not open %s", filename2);
398 if (!istextfile(file1) || !istextfile(file2)) {
399 /* Close open files and pipe, delete temps */
402 if (diffpipe != NULL)
406 warn("Error deleting %s.", tmp1);
409 warn("Error deleting %s.", tmp2);
412 binexec(diffprog, filename1, filename2);
414 /* Line numbers start at one. */
415 file1ln = file2ln = 1;
417 /* Read and parse diff output. */
418 while (parsecmd(diffpipe, file1, file2) != EOF)
422 /* Wait for diff to exit. */
423 if (waitpid(pid, &status, 0) == -1 || !WIFEXITED(status) ||
424 WEXITSTATUS(status) >= 2)
425 err(2, "diff exited abnormally.");
427 /* Delete and free unneeded temporary files. */
430 warn("Error deleting %s.", tmp1);
433 warn("Error deleting %s.", tmp2);
436 filename1 = filename2 = tmp1 = tmp2 = NULL;
438 /* No more diffs, so print common lines. */
440 while ((s1 = xfgets(file1)))
441 enqueue(s1, ' ', NULL);
447 enqueue(s1, ' ', s2);
453 /* Process unmodified lines. */
456 /* Return diff exit status. */
457 return (WEXITSTATUS(status));
461 * When sdiff detects a binary file as input, executes them with
462 * diff to maintain the same behavior as GNU sdiff with binary input.
465 binexec(char *diffprog, char *f1, char *f2)
468 char *args[] = {diffprog, f1, f2, (char *) 0};
469 execv(diffprog, args);
471 /* If execv() fails, sdiff's execution will continue below. */
472 errx(1, "Could not execute diff process.\n");
476 * Checks whether a file appears to be a text file.
486 for (i = 0; i <= MAX_CHECK; i++) {
500 * Prints an individual column (left or right), taking into account
501 * that tabs are variable-width. Takes a string, the current column
502 * the cursor is on the screen, and the maximum value of the column.
503 * The column value is updated as we go along.
506 printcol(const char *s, size_t *col, const size_t col_max)
509 for (; *s && *col < col_max; ++s) {
515 * If rounding to next multiple of eight causes
516 * an integer overflow, just return.
518 if (*col > SIZE_MAX - 8)
521 /* Round to next multiple of eight. */
522 new_col = (*col / 8 + 1) * 8;
525 * If printing the tab goes past the column
526 * width, don't print it and just quit.
528 if (new_col > col_max)
540 * Prompts user to either choose between two strings or edit one, both,
544 prompt(const char *s1, const char *s2)
548 /* Print command prompt. */
551 /* Get user input. */
552 for (; (cmd = xfgets(stdin)); free(cmd)) {
555 /* Skip leading whitespace. */
556 for (p = cmd; isspace(*p); ++p)
562 if (eparse(p, s1, s2) == -1)
567 /* Choose left column as-is. */
569 fprintf(outfp, "%s\n", s1);
570 /* End of command parsing. */
576 /* Choose right column as-is. */
578 fprintf(outfp, "%s\n", s2);
579 /* End of command parsing. */
588 /* Interactive usage help. */
594 /* Prompt user again. */
602 * If there was no error, we received an EOF from stdin, so we
611 * Takes two strings, separated by a column divider. NULL strings are
612 * treated as empty columns. If the divider is the ` ' character, the
613 * second column is not printed (-l flag). In this case, the second
614 * string must be NULL. When the second column is NULL, the divider
615 * does not print the trailing space following the divider character.
617 * Takes into account that tabs can take multiple columns.
620 println(const char *s1, const char div, const char *s2)
624 /* Print first column. Skips if s1 == NULL. */
627 /* Skip angle bracket and space. */
628 printcol(s1, &col, width);
632 /* Otherwise, we pad this column up to width. */
633 for (; col < width; ++col)
636 /* Only print left column. */
637 if (div == ' ' && !s2) {
643 * Print column divider. If there is no second column, we don't
644 * need to add the space for padding.
647 printf(" %c\n", div);
653 /* Skip angle bracket and space. */
654 printcol(s2, &col, line_width);
660 * Reads a line from file and returns as a string. If EOF is reached,
661 * NULL is returned. The returned string must be freed afterwards.
674 if ((l = getline(&s, &linecap, file)) == -1) {
676 err(2, "error reading file");
687 * Parse ed commands from diffpipe and print lines from file1 (lines
688 * to change or delete) or file2 (lines to add or change).
692 parsecmd(FILE *diffpipe, FILE *file1, FILE *file2)
694 size_t file1start, file1end, file2start, file2end, n;
695 /* ed command line and pointer to characters in line */
700 /* Read ed command. */
701 if (!(line = xfgets(diffpipe)))
705 /* Go to character after line number. */
710 file1start = strtonum(line, 0, INT_MAX, &errstr);
712 errx(2, "file1 start is %s: %s", errstr, line);
714 /* A range is specified for file1. */
717 /* Go to character after file2end. */
722 file1end = strtonum(q, 0, INT_MAX, &errstr);
724 errx(2, "file1 end is %s: %s", errstr, line);
725 if (file1start > file1end)
726 errx(2, "invalid line range in file1: %s", line);
728 file1end = file1start;
731 /* Check that cmd is valid. */
732 if (!(cmd == 'a' || cmd == 'c' || cmd == 'd'))
733 errx(2, "ed command not recognized: %c: %s", cmd, line);
736 /* Go to character after line number. */
741 file2start = strtonum(q, 0, INT_MAX, &errstr);
743 errx(2, "file2 start is %s: %s", errstr, line);
746 * There should either be a comma signifying a second line
747 * number or the line should just end here.
749 if (c != ',' && c != '\0')
750 errx(2, "invalid line range in file2: %c: %s", c, line);
754 file2end = strtonum(p, 0, INT_MAX, &errstr);
756 errx(2, "file2 end is %s: %s", errstr, line);
757 if (file2start >= file2end)
758 errx(2, "invalid line range in file2: %s", line);
760 file2end = file2start;
762 /* Appends happen _after_ stated line. */
764 if (file1start != file1end)
765 errx(2, "append cannot have a file1 range: %s",
767 if (file1start == SIZE_MAX)
768 errx(2, "file1 line range too high: %s", line);
769 file1start = ++file1end;
772 * I'm not sure what the deal is with the line numbers for
775 else if (cmd == 'd') {
776 if (file2start != file2end)
777 errx(2, "delete cannot have a file2 range: %s",
779 if (file2start == SIZE_MAX)
780 errx(2, "file2 line range too high: %s", line);
781 file2start = ++file2end;
785 * Continue reading file1 and file2 until we reach line numbers
786 * specified by diff. Should only happen with -I flag.
788 for (; file1ln < file1start && file2ln < file2start;
789 ++file1ln, ++file2ln) {
792 if (!(s1 = xfgets(file1)))
793 errx(2, "file1 shorter than expected");
794 if (!(s2 = xfgets(file2)))
795 errx(2, "file2 shorter than expected");
797 /* If the -l flag was specified, print only left column. */
801 * XXX - If -l and -I are both specified, all
802 * unchanged or ignored lines are shown with a
803 * `(' divider. This matches GNU sdiff, but I
804 * believe it is a bug. Just check out:
805 * gsdiff -l -I '^$' samefile samefile.
808 enqueue(s1, '(', NULL);
810 enqueue(s1, ' ', NULL);
812 enqueue(s1, ' ', s2);
814 /* Ignore deleted lines. */
815 for (; file1ln < file1start; ++file1ln) {
818 if (!(s = xfgets(file1)))
819 errx(2, "file1 shorter than expected");
821 enqueue(s, '(', NULL);
823 /* Ignore added lines. */
824 for (; file2ln < file2start; ++file2ln) {
827 if (!(s = xfgets(file2)))
828 errx(2, "file2 shorter than expected");
830 /* If -l flag was given, don't print right column. */
834 enqueue(NULL, ')', s);
837 /* Process unmodified or skipped lines. */
842 printa(file2, file2end);
843 n = file2end - file2start + 1;
846 printc(file1, file1end, file2, file2end);
847 n = file1end - file1start + 1 + 1 + file2end - file2start + 1;
850 printd(file1, file1end);
851 n = file1end - file1start + 1;
854 errx(2, "invalid diff command: %c: %s", cmd, line);
858 /* Skip to next ed line. */
860 if (!(line = xfgets(diffpipe)))
861 errx(2, "diff ended early");
869 * Queues up a diff line.
872 enqueue(char *left, char div, char *right)
874 struct diffline *diffp;
876 if (!(diffp = malloc(sizeof(struct diffline))))
880 diffp->right = right;
881 STAILQ_INSERT_TAIL(&diffhead, diffp, diffentries);
885 * Free a diffline structure and its elements.
888 freediff(struct diffline *diffp)
897 * Append second string into first. Repeated appends to the same string
898 * are cached, making this an O(n) function, where n = strlen(append).
901 astrcat(char **s, const char *append)
903 /* Length of string in previous run. */
904 static size_t offset = 0;
907 * String from previous run. Compared to *s to see if we are
908 * dealing with the same string. If so, we can use offset.
910 static const char *oldstr = NULL;
914 * First string is NULL, so just copy append.
917 if (!(*s = strdup(append)))
920 /* Keep track of string. */
928 * *s is a string so concatenate.
931 /* Did we process the same string in the last run? */
933 * If this is a different string from the one we just processed
941 /* Size = strlen(*s) + \n + strlen(append) + '\0'. */
942 newsiz = offset + 1 + strlen(append) + 1;
944 /* Resize *s to fit new string. */
945 newstr = realloc(*s, newsiz);
950 /* *s + offset should be end of string. */
952 strlcpy(*s + offset, "\n", newsiz - offset);
953 strlcat(*s + offset, append, newsiz - offset);
955 /* New string length should be exactly newsiz - 1 characters. */
956 /* Store generated string's values. */
962 * Process diff set queue, printing, prompting, and saving each diff
963 * line stored in queue.
968 struct diffline *diffp;
969 char divc, *left, *right;
971 /* Don't process empty queue. */
972 if (STAILQ_EMPTY(&diffhead))
975 /* Remember the divider. */
976 divc = STAILQ_FIRST(&diffhead)->div;
981 * Go through set of diffs, concatenating each line in left or
982 * right column into two long strings, `left' and `right'.
984 STAILQ_FOREACH(diffp, &diffhead, diffentries) {
986 * Print changed lines if -s was given,
987 * print all lines if -s was not given.
989 if (!sflag || diffp->div == '|' || diffp->div == '<' ||
991 println(diffp->left, diffp->div, diffp->right);
993 /* Append new lines to diff set. */
995 astrcat(&left, diffp->left);
997 astrcat(&right, diffp->right);
1000 /* Empty queue and free each diff line and its elements. */
1001 while (!STAILQ_EMPTY(&diffhead)) {
1002 diffp = STAILQ_FIRST(&diffhead);
1003 STAILQ_REMOVE_HEAD(&diffhead, diffentries);
1007 /* Write to outfp, prompting user if lines are different. */
1010 case ' ': case '(': case ')':
1011 fprintf(outfp, "%s\n", left);
1013 case '|': case '<': case '>':
1014 prompt(left, right);
1017 errx(2, "invalid divider: %c", divc);
1020 /* Free left and right. */
1026 * Print lines following an (a)ppend command.
1029 printa(FILE *file, size_t line2)
1033 for (; file2ln <= line2; ++file2ln) {
1034 if (!(line = xfgets(file)))
1035 errx(2, "append ended early");
1036 enqueue(NULL, '>', line);
1042 * Print lines following a (c)hange command, from file1ln to file1end
1043 * and from file2ln to file2end.
1046 printc(FILE *file1, size_t file1end, FILE *file2, size_t file2end)
1049 STAILQ_ENTRY(fileline) fileentries;
1052 STAILQ_HEAD(, fileline) delqhead = STAILQ_HEAD_INITIALIZER(delqhead);
1054 /* Read lines to be deleted. */
1055 for (; file1ln <= file1end; ++file1ln) {
1056 struct fileline *linep;
1059 /* Read lines from both. */
1060 if (!(line1 = xfgets(file1)))
1061 errx(2, "error reading file1 in delete in change");
1063 /* Add to delete queue. */
1064 if (!(linep = malloc(sizeof(struct fileline))))
1066 linep->line = line1;
1067 STAILQ_INSERT_TAIL(&delqhead, linep, fileentries);
1070 /* Process changed lines.. */
1071 for (; !STAILQ_EMPTY(&delqhead) && file2ln <= file2end;
1073 struct fileline *del;
1077 if (!(add = xfgets(file2)))
1078 errx(2, "error reading add in change");
1080 del = STAILQ_FIRST(&delqhead);
1081 enqueue(del->line, '|', add);
1082 STAILQ_REMOVE_HEAD(&delqhead, fileentries);
1084 * Free fileline structure but not its elements since
1085 * they are queued up.
1091 /* Process remaining lines to add. */
1092 for (; file2ln <= file2end; ++file2ln) {
1096 if (!(add = xfgets(file2)))
1097 errx(2, "error reading add in change");
1099 enqueue(NULL, '>', add);
1103 /* Process remaining lines to delete. */
1104 while (!STAILQ_EMPTY(&delqhead)) {
1105 struct fileline *filep;
1107 filep = STAILQ_FIRST(&delqhead);
1108 enqueue(filep->line, '<', NULL);
1109 STAILQ_REMOVE_HEAD(&delqhead, fileentries);
1116 * Print deleted lines from file, from file1ln to file1end.
1119 printd(FILE *file1, size_t file1end)
1123 /* Print out lines file1ln to line2. */
1124 for (; file1ln <= file1end; ++file1ln) {
1125 if (!(line1 = xfgets(file1)))
1126 errx(2, "file1 ended early in delete");
1127 enqueue(line1, '<', NULL);
1133 * Interactive mode usage.
1139 puts("e:\tedit blank diff\n"
1140 "eb:\tedit both diffs concatenated\n"
1141 "el:\tedit left diff\n"
1142 "er:\tedit right diff\n"
1143 "l | 1:\tchoose left diff\n"
1144 "r | 2:\tchoose right diff\n"
1145 "s:\tsilent mode--don't print identical lines\n"
1146 "v:\tverbose mode--print identical lines\n"
1155 "usage: sdiff [-abdilstHW] [-I regexp] [-o outfile] [-w width] file1"