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>
32 #define DIFF_PATH "/usr/bin/diff"
36 * Each column must be at least one character wide, plus three
37 * characters between the columns (space, [<|>], space).
41 /* 3 kilobytes of chars */
44 /* A single diff line. */
46 STAILQ_ENTRY(diffline) diffentries;
52 static void astrcat(char **, const char *);
53 static void enqueue(char *, char, char *);
54 static char *mktmpcpy(const char *);
55 static int istextfile(FILE *);
56 static void binexec(char *, char *, char *) __dead2;
57 static void freediff(struct diffline *);
58 static void int_usage(void);
59 static int parsecmd(FILE *, FILE *, FILE *);
60 static void printa(FILE *, size_t);
61 static void printc(FILE *, size_t, FILE *, size_t);
62 static void printcol(const char *, size_t *, const size_t);
63 static void printd(FILE *, size_t);
64 static void println(const char *, const char, const char *);
65 static void processq(void);
66 static void prompt(const char *, const char *);
67 static void usage(void) __dead2;
68 static char *xfgets(FILE *);
70 static STAILQ_HEAD(, diffline) diffhead = STAILQ_HEAD_INITIALIZER(diffhead);
71 static size_t line_width; /* width of a line (two columns and divider) */
72 static size_t width; /* width of each column */
73 static size_t file1ln, file2ln; /* line number of file1 and file2 */
74 static int Iflag = 0; /* ignore sets matching regexp */
75 static int lflag; /* print only left column for identical lines */
76 static int sflag; /* skip identical lines */
77 FILE *outfp; /* file to save changes to */
78 const char *tmpdir; /* TMPDIR or /tmp */
81 HELP_OPT = CHAR_MAX + 1,
90 static struct option longopts[] = {
91 /* options only processed in sdiff */
92 { "suppress-common-lines", no_argument, NULL, 's' },
93 { "width", required_argument, NULL, 'w' },
95 { "output", required_argument, NULL, 'o' },
96 { "diff-program", required_argument, NULL, DIFFPROG_OPT },
98 /* Options processed by diff. */
99 { "ignore-file-name-case", no_argument, NULL, FCASE_IGNORE_OPT },
100 { "no-ignore-file-name-case", no_argument, NULL, FCASE_SENSITIVE_OPT },
101 { "strip-trailing-cr", no_argument, NULL, STRIPCR_OPT },
102 { "tabsize", required_argument, NULL, TSIZE_OPT },
103 { "help", no_argument, NULL, HELP_OPT },
104 { "text", no_argument, NULL, 'a' },
105 { "ignore-blank-lines", no_argument, NULL, 'B' },
106 { "ignore-space-change", no_argument, NULL, 'b' },
107 { "minimal", no_argument, NULL, 'd' },
108 { "ignore-tab-expansion", no_argument, NULL, 'E' },
109 { "ignore-matching-lines", required_argument, NULL, 'I' },
110 { "ignore-case", no_argument, NULL, 'i' },
111 { "left-column", no_argument, NULL, 'l' },
112 { "expand-tabs", no_argument, NULL, 't' },
113 { "speed-large-files", no_argument, NULL, 'H' },
114 { "ignore-all-space", no_argument, NULL, 'W' },
116 { NULL, 0, NULL, '\0'}
119 static const char *help_msg[] = {
120 "usage: sdiff [-abdilstW] [-I regexp] [-o outfile] [-w width] file1 file2\n",
121 "-l, --left-column: only print the left column for identical lines.",
122 "-o OUTFILE, --output=OUTFILE: interactively merge file1 and file2 into outfile.",
123 "-s, --suppress-common-lines: skip identical lines.",
124 "-w WIDTH, --width=WIDTH: print a maximum of WIDTH characters on each line.",
126 "Options passed to diff(1) are:",
127 "\t-a, --text: treat file1 and file2 as text files.",
128 "\t-b, --ignore-trailing-cr: ignore trailing blank spaces.",
129 "\t-d, --minimal: minimize diff size.",
130 "\t-I RE, --ignore-matching-lines=RE: ignore changes whose line matches RE.",
131 "\t-i, --ignore-case: do a case-insensitive comparison.",
132 "\t-t, --expand-tabs: sxpand tabs to spaces.",
133 "\t-W, --ignore-all-spaces: ignore all spaces.",
134 "\t--speed-large-files: assume large file with scattered changes.",
135 "\t--strip-trailing-cr: strip trailing carriage return.",
136 "\t--ignore-file-name-case: ignore case of file names.",
137 "\t--no-ignore-file-name-case: do not ignore file name case",
138 "\t--tabsize NUM: change size of tabs (default 8.)",
144 * Create temporary file if source_file is not a regular file.
145 * Returns temporary file name if one was malloced, NULL if unnecessary.
148 mktmpcpy(const char *source_file)
156 /* Open input and output. */
157 ifd = open(source_file, O_RDONLY, 0);
158 /* File was opened successfully. */
160 if (fstat(ifd, &sb) == -1)
161 err(2, "error getting file status from %s", source_file);
164 if (S_ISREG(sb.st_mode)) {
169 /* If ``-'' does not exist the user meant stdin. */
170 if (errno == ENOENT && strcmp(source_file, "-") == 0)
173 err(2, "error opening %s", source_file);
176 /* Not a regular file, so copy input into temporary file. */
177 if (asprintf(&target_file, "%s/sdiff.XXXXXXXXXX", tmpdir) == -1)
179 if ((ofd = mkstemp(target_file)) == -1) {
180 warn("error opening %s", target_file);
183 while ((rcount = read(ifd, buf, sizeof(buf))) != -1 &&
187 wcount = write(ofd, buf, (size_t)rcount);
188 if (-1 == wcount || rcount != wcount) {
189 warn("error writing to %s", target_file);
194 warn("error reading from %s", source_file);
201 return (target_file);
209 main(int argc, char **argv)
211 FILE *diffpipe=NULL, *file1, *file2;
212 size_t diffargc = 0, wflag = WIDTH;
213 int ch, fd[2] = {-1}, status;
215 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 */
264 for(popt = longopts; ch != popt->val && popt->name != NULL; popt++);
265 diffargv[1] = realloc(diffargv[1], sizeof(char) * strlen(diffargv[1]) + 2);
267 * In diff, the 'W' option is 'w' and the 'w' is 'W'.
270 sprintf(diffargv[1], "%sw", diffargv[1]);
272 sprintf(diffargv[1], "%s%c", diffargv[1], ch);
275 diffargv[0] = diffprog = optarg;
279 diffargv[diffargc++] = "-I";
280 diffargv[diffargc++] = optarg;
292 wflag = strtonum(optarg, WIDTH_MIN,
295 errx(2, "width is %s: %s", errstr, optarg);
298 for (i = 0; help_msg[i] != NULL; i++)
299 printf("%s\n", help_msg[i]);
308 /* no single switches were used */
309 if (strcmp(diffargv[1], "-") == 0 ) {
310 for ( i = 1; i < argc-1; i++) {
311 diffargv[i] = diffargv[i+1];
313 diffargv[diffargc-1] = NULL;
323 if (outfile && (outfp = fopen(outfile, "w")) == NULL)
324 err(2, "could not open: %s", optarg);
326 if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0')
333 * Create temporary files for diff and sdiff to share if file1
334 * or file2 are not regular files. This allows sdiff and diff
335 * to read the same inputs if one or both inputs are stdin.
337 * If any temporary files were created, their names would be
338 * saved in tmp1 or tmp2. tmp1 should never equal tmp2.
341 /* file1 and file2 are the same, so copy to same temp file. */
342 if (strcmp(filename1, filename2) == 0) {
343 if ((tmp1 = mktmpcpy(filename1)))
344 filename1 = filename2 = tmp1;
345 /* Copy file1 and file2 into separate temp files. */
347 if ((tmp1 = mktmpcpy(filename1)))
349 if ((tmp2 = mktmpcpy(filename2)))
353 diffargv[diffargc++] = filename1;
354 diffargv[diffargc++] = filename2;
355 /* Add NULL to end of array to indicate end of array. */
356 diffargv[diffargc++] = NULL;
358 /* Subtract column divider and divide by two. */
359 width = (wflag - 3) / 2;
360 /* Make sure line_width can fit in size_t. */
361 if (width > (SIZE_MAX - 3) / 2)
362 errx(2, "width is too large: %zu", width);
363 line_width = width * 2 + 3;
368 switch (pid = fork()) {
371 /* We don't read from the pipe. */
373 if (dup2(fd[1], STDOUT_FILENO) == -1)
374 err(2, "child could not duplicate descriptor");
375 /* Free unused descriptor. */
377 execvp(diffprog, diffargv);
378 err(2, "could not execute diff: %s", diffprog);
381 err(2, "could not fork");
386 /* We don't write to the pipe. */
389 /* Open pipe to diff command. */
390 if ((diffpipe = fdopen(fd[0], "r")) == NULL)
391 err(2, "could not open diff pipe");
393 if ((file1 = fopen(filename1, "r")) == NULL)
394 err(2, "could not open %s", filename1);
395 if ((file2 = fopen(filename2, "r")) == NULL)
396 err(2, "could not open %s", filename2);
397 if (!istextfile(file1) || !istextfile(file2)) {
398 /* Close open files and pipe, delete temps */
401 if (diffpipe != NULL)
405 warn("Error deleting %s.", tmp1);
408 warn("Error deleting %s.", tmp2);
411 binexec(diffprog, filename1, filename2);
413 /* Line numbers start at one. */
414 file1ln = file2ln = 1;
416 /* Read and parse diff output. */
417 while (parsecmd(diffpipe, file1, file2) != EOF)
421 /* Wait for diff to exit. */
422 if (waitpid(pid, &status, 0) == -1 || !WIFEXITED(status) ||
423 WEXITSTATUS(status) >= 2)
424 err(2, "diff exited abnormally.");
426 /* Delete and free unneeded temporary files. */
429 warn("Error deleting %s.", tmp1);
432 warn("Error deleting %s.", tmp2);
435 filename1 = filename2 = tmp1 = tmp2 = NULL;
437 /* No more diffs, so print common lines. */
439 while ((s1 = xfgets(file1)))
440 enqueue(s1, ' ', NULL);
446 enqueue(s1, ' ', s2);
452 /* Process unmodified lines. */
455 /* Return diff exit status. */
456 return (WEXITSTATUS(status));
460 * When sdiff detects a binary file as input, executes them with
461 * diff to maintain the same behavior as GNU sdiff with binary input.
464 binexec(char *diffprog, char *f1, char *f2)
467 char *args[] = {diffprog, f1, f2, (char *) 0};
468 execv(diffprog, args);
470 /* If execv() fails, sdiff's execution will continue below. */
471 errx(1, "could not execute diff process");
475 * Checks whether a file appears to be a text file.
485 for (i = 0; i <= MAX_CHECK; i++) {
499 * Prints an individual column (left or right), taking into account
500 * that tabs are variable-width. Takes a string, the current column
501 * the cursor is on the screen, and the maximum value of the column.
502 * The column value is updated as we go along.
505 printcol(const char *s, size_t *col, const size_t col_max)
508 for (; *s && *col < col_max; ++s) {
514 * If rounding to next multiple of eight causes
515 * an integer overflow, just return.
517 if (*col > SIZE_MAX - 8)
520 /* Round to next multiple of eight. */
521 new_col = (*col / 8 + 1) * 8;
524 * If printing the tab goes past the column
525 * width, don't print it and just quit.
527 if (new_col > col_max)
539 * Prompts user to either choose between two strings or edit one, both,
543 prompt(const char *s1, const char *s2)
547 /* Print command prompt. */
550 /* Get user input. */
551 for (; (cmd = xfgets(stdin)); free(cmd)) {
554 /* Skip leading whitespace. */
555 for (p = cmd; isspace(*p); ++p)
561 if (eparse(p, s1, s2) == -1)
566 /* Choose left column as-is. */
568 fprintf(outfp, "%s\n", s1);
569 /* End of command parsing. */
575 /* Choose right column as-is. */
577 fprintf(outfp, "%s\n", s2);
578 /* End of command parsing. */
587 /* Interactive usage help. */
593 /* Prompt user again. */
601 * If there was no error, we received an EOF from stdin, so we
610 * Takes two strings, separated by a column divider. NULL strings are
611 * treated as empty columns. If the divider is the ` ' character, the
612 * second column is not printed (-l flag). In this case, the second
613 * string must be NULL. When the second column is NULL, the divider
614 * does not print the trailing space following the divider character.
616 * Takes into account that tabs can take multiple columns.
619 println(const char *s1, const char div, const char *s2)
623 /* Print first column. Skips if s1 == NULL. */
626 /* Skip angle bracket and space. */
627 printcol(s1, &col, width);
631 /* Otherwise, we pad this column up to width. */
632 for (; col < width; ++col)
635 /* Only print left column. */
636 if (div == ' ' && !s2) {
642 * Print column divider. If there is no second column, we don't
643 * need to add the space for padding.
646 printf(" %c\n", div);
652 /* Skip angle bracket and space. */
653 printcol(s2, &col, line_width);
659 * Reads a line from file and returns as a string. If EOF is reached,
660 * NULL is returned. The returned string must be freed afterwards.
673 if ((l = getline(&s, &linecap, file)) == -1) {
675 err(2, "error reading file");
686 * Parse ed commands from diffpipe and print lines from file1 (lines
687 * to change or delete) or file2 (lines to add or change).
691 parsecmd(FILE *diffpipe, FILE *file1, FILE *file2)
693 size_t file1start, file1end, file2start, file2end, n;
694 /* ed command line and pointer to characters in line */
699 /* Read ed command. */
700 if (!(line = xfgets(diffpipe)))
704 /* Go to character after line number. */
709 file1start = strtonum(line, 0, INT_MAX, &errstr);
711 errx(2, "file1 start is %s: %s", errstr, line);
713 /* A range is specified for file1. */
716 /* Go to character after file2end. */
721 file1end = strtonum(q, 0, INT_MAX, &errstr);
723 errx(2, "file1 end is %s: %s", errstr, line);
724 if (file1start > file1end)
725 errx(2, "invalid line range in file1: %s", line);
727 file1end = file1start;
730 /* Check that cmd is valid. */
731 if (!(cmd == 'a' || cmd == 'c' || cmd == 'd'))
732 errx(2, "ed command not recognized: %c: %s", cmd, line);
735 /* Go to character after line number. */
740 file2start = strtonum(q, 0, INT_MAX, &errstr);
742 errx(2, "file2 start is %s: %s", errstr, line);
745 * There should either be a comma signifying a second line
746 * number or the line should just end here.
748 if (c != ',' && c != '\0')
749 errx(2, "invalid line range in file2: %c: %s", c, line);
753 file2end = strtonum(p, 0, INT_MAX, &errstr);
755 errx(2, "file2 end is %s: %s", errstr, line);
756 if (file2start >= file2end)
757 errx(2, "invalid line range in file2: %s", line);
759 file2end = file2start;
761 /* Appends happen _after_ stated line. */
763 if (file1start != file1end)
764 errx(2, "append cannot have a file1 range: %s",
766 if (file1start == SIZE_MAX)
767 errx(2, "file1 line range too high: %s", line);
768 file1start = ++file1end;
771 * I'm not sure what the deal is with the line numbers for
774 else if (cmd == 'd') {
775 if (file2start != file2end)
776 errx(2, "delete cannot have a file2 range: %s",
778 if (file2start == SIZE_MAX)
779 errx(2, "file2 line range too high: %s", line);
780 file2start = ++file2end;
784 * Continue reading file1 and file2 until we reach line numbers
785 * specified by diff. Should only happen with -I flag.
787 for (; file1ln < file1start && file2ln < file2start;
788 ++file1ln, ++file2ln) {
791 if (!(s1 = xfgets(file1)))
792 errx(2, "file1 shorter than expected");
793 if (!(s2 = xfgets(file2)))
794 errx(2, "file2 shorter than expected");
796 /* If the -l flag was specified, print only left column. */
800 * XXX - If -l and -I are both specified, all
801 * unchanged or ignored lines are shown with a
802 * `(' divider. This matches GNU sdiff, but I
803 * believe it is a bug. Just check out:
804 * gsdiff -l -I '^$' samefile samefile.
807 enqueue(s1, '(', NULL);
809 enqueue(s1, ' ', NULL);
811 enqueue(s1, ' ', s2);
813 /* Ignore deleted lines. */
814 for (; file1ln < file1start; ++file1ln) {
817 if (!(s = xfgets(file1)))
818 errx(2, "file1 shorter than expected");
820 enqueue(s, '(', NULL);
822 /* Ignore added lines. */
823 for (; file2ln < file2start; ++file2ln) {
826 if (!(s = xfgets(file2)))
827 errx(2, "file2 shorter than expected");
829 /* If -l flag was given, don't print right column. */
833 enqueue(NULL, ')', s);
836 /* Process unmodified or skipped lines. */
841 printa(file2, file2end);
842 n = file2end - file2start + 1;
845 printc(file1, file1end, file2, file2end);
846 n = file1end - file1start + 1 + 1 + file2end - file2start + 1;
849 printd(file1, file1end);
850 n = file1end - file1start + 1;
853 errx(2, "invalid diff command: %c: %s", cmd, line);
857 /* Skip to next ed line. */
859 if (!(line = xfgets(diffpipe)))
860 errx(2, "diff ended early");
868 * Queues up a diff line.
871 enqueue(char *left, char div, char *right)
873 struct diffline *diffp;
875 if (!(diffp = malloc(sizeof(struct diffline))))
879 diffp->right = right;
880 STAILQ_INSERT_TAIL(&diffhead, diffp, diffentries);
884 * Free a diffline structure and its elements.
887 freediff(struct diffline *diffp)
896 * Append second string into first. Repeated appends to the same string
897 * are cached, making this an O(n) function, where n = strlen(append).
900 astrcat(char **s, const char *append)
902 /* Length of string in previous run. */
903 static size_t offset = 0;
906 * String from previous run. Compared to *s to see if we are
907 * dealing with the same string. If so, we can use offset.
909 static const char *oldstr = NULL;
913 * First string is NULL, so just copy append.
916 if (!(*s = strdup(append)))
919 /* Keep track of string. */
927 * *s is a string so concatenate.
930 /* Did we process the same string in the last run? */
932 * If this is a different string from the one we just processed
940 /* Size = strlen(*s) + \n + strlen(append) + '\0'. */
941 newsiz = offset + 1 + strlen(append) + 1;
943 /* Resize *s to fit new string. */
944 newstr = realloc(*s, newsiz);
949 /* *s + offset should be end of string. */
951 strlcpy(*s + offset, "\n", newsiz - offset);
952 strlcat(*s + offset, append, newsiz - offset);
954 /* New string length should be exactly newsiz - 1 characters. */
955 /* Store generated string's values. */
961 * Process diff set queue, printing, prompting, and saving each diff
962 * line stored in queue.
967 struct diffline *diffp;
968 char divc, *left, *right;
970 /* Don't process empty queue. */
971 if (STAILQ_EMPTY(&diffhead))
974 /* Remember the divider. */
975 divc = STAILQ_FIRST(&diffhead)->div;
980 * Go through set of diffs, concatenating each line in left or
981 * right column into two long strings, `left' and `right'.
983 STAILQ_FOREACH(diffp, &diffhead, diffentries) {
985 * Print changed lines if -s was given,
986 * print all lines if -s was not given.
988 if (!sflag || diffp->div == '|' || diffp->div == '<' ||
990 println(diffp->left, diffp->div, diffp->right);
992 /* Append new lines to diff set. */
994 astrcat(&left, diffp->left);
996 astrcat(&right, diffp->right);
999 /* Empty queue and free each diff line and its elements. */
1000 while (!STAILQ_EMPTY(&diffhead)) {
1001 diffp = STAILQ_FIRST(&diffhead);
1002 STAILQ_REMOVE_HEAD(&diffhead, diffentries);
1006 /* Write to outfp, prompting user if lines are different. */
1009 case ' ': case '(': case ')':
1010 fprintf(outfp, "%s\n", left);
1012 case '|': case '<': case '>':
1013 prompt(left, right);
1016 errx(2, "invalid divider: %c", divc);
1019 /* Free left and right. */
1025 * Print lines following an (a)ppend command.
1028 printa(FILE *file, size_t line2)
1032 for (; file2ln <= line2; ++file2ln) {
1033 if (!(line = xfgets(file)))
1034 errx(2, "append ended early");
1035 enqueue(NULL, '>', line);
1041 * Print lines following a (c)hange command, from file1ln to file1end
1042 * and from file2ln to file2end.
1045 printc(FILE *file1, size_t file1end, FILE *file2, size_t file2end)
1048 STAILQ_ENTRY(fileline) fileentries;
1051 STAILQ_HEAD(, fileline) delqhead = STAILQ_HEAD_INITIALIZER(delqhead);
1053 /* Read lines to be deleted. */
1054 for (; file1ln <= file1end; ++file1ln) {
1055 struct fileline *linep;
1058 /* Read lines from both. */
1059 if (!(line1 = xfgets(file1)))
1060 errx(2, "error reading file1 in delete in change");
1062 /* Add to delete queue. */
1063 if (!(linep = malloc(sizeof(struct fileline))))
1065 linep->line = line1;
1066 STAILQ_INSERT_TAIL(&delqhead, linep, fileentries);
1069 /* Process changed lines.. */
1070 for (; !STAILQ_EMPTY(&delqhead) && file2ln <= file2end;
1072 struct fileline *del;
1076 if (!(add = xfgets(file2)))
1077 errx(2, "error reading add in change");
1079 del = STAILQ_FIRST(&delqhead);
1080 enqueue(del->line, '|', add);
1081 STAILQ_REMOVE_HEAD(&delqhead, fileentries);
1083 * Free fileline structure but not its elements since
1084 * they are queued up.
1090 /* Process remaining lines to add. */
1091 for (; file2ln <= file2end; ++file2ln) {
1095 if (!(add = xfgets(file2)))
1096 errx(2, "error reading add in change");
1098 enqueue(NULL, '>', add);
1102 /* Process remaining lines to delete. */
1103 while (!STAILQ_EMPTY(&delqhead)) {
1104 struct fileline *filep;
1106 filep = STAILQ_FIRST(&delqhead);
1107 enqueue(filep->line, '<', NULL);
1108 STAILQ_REMOVE_HEAD(&delqhead, fileentries);
1115 * Print deleted lines from file, from file1ln to file1end.
1118 printd(FILE *file1, size_t file1end)
1122 /* Print out lines file1ln to line2. */
1123 for (; file1ln <= file1end; ++file1ln) {
1124 if (!(line1 = xfgets(file1)))
1125 errx(2, "file1 ended early in delete");
1126 enqueue(line1, '<', NULL);
1132 * Interactive mode usage.
1138 puts("e:\tedit blank diff\n"
1139 "eb:\tedit both diffs concatenated\n"
1140 "el:\tedit left diff\n"
1141 "er:\tedit right diff\n"
1142 "l | 1:\tchoose left diff\n"
1143 "r | 2:\tchoose right diff\n"
1144 "s:\tsilent mode--don't print identical lines\n"
1145 "v:\tverbose mode--print identical lines\n"
1154 "usage: sdiff [-abdilstW] [-I regexp] [-o outfile] [-w width] file1"