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 static char 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;
216 char **diffargv, *diffprog = diff_path, *filename1, *filename2,
217 *tmp1, *tmp2, *s1, *s2;
220 char speed_lf[] = "--speed-large-files";
223 * Process diff flags.
226 * Allocate memory for diff arguments and NULL.
227 * Each flag has at most one argument, so doubling argc gives an
228 * upper limit of how many diff args can be passed. argv[0],
229 * file1, and file2 won't have arguments so doubling them will
230 * waste some memory; however we need an extra space for the
231 * NULL at the end, so it sort of works out.
233 if (!(diffargv = calloc(argc, sizeof(char **) * 2)))
236 /* Add first argument, the program name. */
237 diffargv[diffargc++] = diffprog;
239 /* create a dynamic string for merging single-switch options */
240 if ( asprintf(&diffargv[diffargc++], "-") < 0 )
243 while ((ch = getopt_long(argc, argv, "aBbdEHI:ilo:stWw:",
244 longopts, NULL)) != -1) {
248 /* only compatible --long-name-form with diff */
249 case FCASE_IGNORE_OPT:
250 case FCASE_SENSITIVE_OPT:
255 /* combine no-arg single switches */
264 diffargv[1] = realloc(diffargv[1], sizeof(char) * strlen(diffargv[1]) + 2);
266 * In diff, the 'W' option is 'w' and the 'w' is 'W'.
269 sprintf(diffargv[1], "%sw", diffargv[1]);
271 sprintf(diffargv[1], "%s%c", diffargv[1], ch);
274 diffargv[diffargc++] = speed_lf;
277 diffargv[0] = diffprog = optarg;
281 diffargv[diffargc++] = I_arg;
282 diffargv[diffargc++] = optarg;
294 wflag = strtonum(optarg, WIDTH_MIN,
297 errx(2, "width is %s: %s", errstr, optarg);
300 for (i = 0; help_msg[i] != NULL; i++)
301 printf("%s\n", help_msg[i]);
310 /* no single switches were used */
311 if (strcmp(diffargv[1], "-") == 0 ) {
312 for ( i = 1; i < argc-1; i++) {
313 diffargv[i] = diffargv[i+1];
315 diffargv[diffargc-1] = NULL;
325 if (outfile && (outfp = fopen(outfile, "w")) == NULL)
326 err(2, "could not open: %s", optarg);
328 if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0')
335 * Create temporary files for diff and sdiff to share if file1
336 * or file2 are not regular files. This allows sdiff and diff
337 * to read the same inputs if one or both inputs are stdin.
339 * If any temporary files were created, their names would be
340 * saved in tmp1 or tmp2. tmp1 should never equal tmp2.
343 /* file1 and file2 are the same, so copy to same temp file. */
344 if (strcmp(filename1, filename2) == 0) {
345 if ((tmp1 = mktmpcpy(filename1)))
346 filename1 = filename2 = tmp1;
347 /* Copy file1 and file2 into separate temp files. */
349 if ((tmp1 = mktmpcpy(filename1)))
351 if ((tmp2 = mktmpcpy(filename2)))
355 diffargv[diffargc++] = filename1;
356 diffargv[diffargc++] = filename2;
357 /* Add NULL to end of array to indicate end of array. */
358 diffargv[diffargc++] = NULL;
360 /* Subtract column divider and divide by two. */
361 width = (wflag - 3) / 2;
362 /* Make sure line_width can fit in size_t. */
363 if (width > (SIZE_MAX - 3) / 2)
364 errx(2, "width is too large: %zu", width);
365 line_width = width * 2 + 3;
370 switch (pid = fork()) {
373 /* We don't read from the pipe. */
375 if (dup2(fd[1], STDOUT_FILENO) == -1)
376 err(2, "child could not duplicate descriptor");
377 /* Free unused descriptor. */
379 execvp(diffprog, diffargv);
380 err(2, "could not execute diff: %s", diffprog);
383 err(2, "could not fork");
388 /* We don't write to the pipe. */
391 /* Open pipe to diff command. */
392 if ((diffpipe = fdopen(fd[0], "r")) == NULL)
393 err(2, "could not open diff pipe");
395 if ((file1 = fopen(filename1, "r")) == NULL)
396 err(2, "could not open %s", filename1);
397 if ((file2 = fopen(filename2, "r")) == NULL)
398 err(2, "could not open %s", filename2);
399 if (!istextfile(file1) || !istextfile(file2)) {
400 /* Close open files and pipe, delete temps */
403 if (diffpipe != NULL)
407 warn("Error deleting %s.", tmp1);
410 warn("Error deleting %s.", tmp2);
413 binexec(diffprog, filename1, filename2);
415 /* Line numbers start at one. */
416 file1ln = file2ln = 1;
418 /* Read and parse diff output. */
419 while (parsecmd(diffpipe, file1, file2) != EOF)
423 /* Wait for diff to exit. */
424 if (waitpid(pid, &status, 0) == -1 || !WIFEXITED(status) ||
425 WEXITSTATUS(status) >= 2)
426 err(2, "diff exited abnormally.");
428 /* Delete and free unneeded temporary files. */
431 warn("Error deleting %s.", tmp1);
434 warn("Error deleting %s.", tmp2);
437 filename1 = filename2 = tmp1 = tmp2 = NULL;
439 /* No more diffs, so print common lines. */
441 while ((s1 = xfgets(file1)))
442 enqueue(s1, ' ', NULL);
448 enqueue(s1, ' ', s2);
454 /* Process unmodified lines. */
457 /* Return diff exit status. */
458 return (WEXITSTATUS(status));
462 * When sdiff detects a binary file as input, executes them with
463 * diff to maintain the same behavior as GNU sdiff with binary input.
466 binexec(char *diffprog, char *f1, char *f2)
469 char *args[] = {diffprog, f1, f2, (char *) 0};
470 execv(diffprog, args);
472 /* If execv() fails, sdiff's execution will continue below. */
473 errx(1, "could not execute diff process");
477 * Checks whether a file appears to be a text file.
487 for (i = 0; i <= MAX_CHECK; i++) {
501 * Prints an individual column (left or right), taking into account
502 * that tabs are variable-width. Takes a string, the current column
503 * the cursor is on the screen, and the maximum value of the column.
504 * The column value is updated as we go along.
507 printcol(const char *s, size_t *col, const size_t col_max)
510 for (; *s && *col < col_max; ++s) {
516 * If rounding to next multiple of eight causes
517 * an integer overflow, just return.
519 if (*col > SIZE_MAX - 8)
522 /* Round to next multiple of eight. */
523 new_col = (*col / 8 + 1) * 8;
526 * If printing the tab goes past the column
527 * width, don't print it and just quit.
529 if (new_col > col_max)
541 * Prompts user to either choose between two strings or edit one, both,
545 prompt(const char *s1, const char *s2)
549 /* Print command prompt. */
552 /* Get user input. */
553 for (; (cmd = xfgets(stdin)); free(cmd)) {
556 /* Skip leading whitespace. */
557 for (p = cmd; isspace(*p); ++p)
563 if (eparse(p, s1, s2) == -1)
568 /* Choose left column as-is. */
570 fprintf(outfp, "%s\n", s1);
571 /* End of command parsing. */
577 /* Choose right column as-is. */
579 fprintf(outfp, "%s\n", s2);
580 /* End of command parsing. */
589 /* Interactive usage help. */
595 /* Prompt user again. */
603 * If there was no error, we received an EOF from stdin, so we
612 * Takes two strings, separated by a column divider. NULL strings are
613 * treated as empty columns. If the divider is the ` ' character, the
614 * second column is not printed (-l flag). In this case, the second
615 * string must be NULL. When the second column is NULL, the divider
616 * does not print the trailing space following the divider character.
618 * Takes into account that tabs can take multiple columns.
621 println(const char *s1, const char divider, const char *s2)
625 /* Print first column. Skips if s1 == NULL. */
628 /* Skip angle bracket and space. */
629 printcol(s1, &col, width);
633 /* Otherwise, we pad this column up to width. */
634 for (; col < width; ++col)
637 /* Only print left column. */
638 if (divider == ' ' && !s2) {
644 * Print column divider. If there is no second column, we don't
645 * need to add the space for padding.
648 printf(" %c\n", divider);
651 printf(" %c ", divider);
654 /* Skip angle bracket and space. */
655 printcol(s2, &col, line_width);
661 * Reads a line from file and returns as a string. If EOF is reached,
662 * NULL is returned. The returned string must be freed afterwards.
675 if ((l = getline(&s, &linecap, file)) == -1) {
677 err(2, "error reading file");
688 * Parse ed commands from diffpipe and print lines from file1 (lines
689 * to change or delete) or file2 (lines to add or change).
693 parsecmd(FILE *diffpipe, FILE *file1, FILE *file2)
695 size_t file1start, file1end, file2start, file2end, n;
696 /* ed command line and pointer to characters in line */
701 /* Read ed command. */
702 if (!(line = xfgets(diffpipe)))
706 /* Go to character after line number. */
711 file1start = strtonum(line, 0, INT_MAX, &errstr);
713 errx(2, "file1 start is %s: %s", errstr, line);
715 /* A range is specified for file1. */
718 /* Go to character after file2end. */
723 file1end = strtonum(q, 0, INT_MAX, &errstr);
725 errx(2, "file1 end is %s: %s", errstr, line);
726 if (file1start > file1end)
727 errx(2, "invalid line range in file1: %s", line);
729 file1end = file1start;
732 /* Check that cmd is valid. */
733 if (!(cmd == 'a' || cmd == 'c' || cmd == 'd'))
734 errx(2, "ed command not recognized: %c: %s", cmd, line);
737 /* Go to character after line number. */
742 file2start = strtonum(q, 0, INT_MAX, &errstr);
744 errx(2, "file2 start is %s: %s", errstr, line);
747 * There should either be a comma signifying a second line
748 * number or the line should just end here.
750 if (c != ',' && c != '\0')
751 errx(2, "invalid line range in file2: %c: %s", c, line);
755 file2end = strtonum(p, 0, INT_MAX, &errstr);
757 errx(2, "file2 end is %s: %s", errstr, line);
758 if (file2start >= file2end)
759 errx(2, "invalid line range in file2: %s", line);
761 file2end = file2start;
763 /* Appends happen _after_ stated line. */
765 if (file1start != file1end)
766 errx(2, "append cannot have a file1 range: %s",
768 if (file1start == SIZE_MAX)
769 errx(2, "file1 line range too high: %s", line);
770 file1start = ++file1end;
773 * I'm not sure what the deal is with the line numbers for
776 else if (cmd == 'd') {
777 if (file2start != file2end)
778 errx(2, "delete cannot have a file2 range: %s",
780 if (file2start == SIZE_MAX)
781 errx(2, "file2 line range too high: %s", line);
782 file2start = ++file2end;
786 * Continue reading file1 and file2 until we reach line numbers
787 * specified by diff. Should only happen with -I flag.
789 for (; file1ln < file1start && file2ln < file2start;
790 ++file1ln, ++file2ln) {
793 if (!(s1 = xfgets(file1)))
794 errx(2, "file1 shorter than expected");
795 if (!(s2 = xfgets(file2)))
796 errx(2, "file2 shorter than expected");
798 /* If the -l flag was specified, print only left column. */
802 * XXX - If -l and -I are both specified, all
803 * unchanged or ignored lines are shown with a
804 * `(' divider. This matches GNU sdiff, but I
805 * believe it is a bug. Just check out:
806 * gsdiff -l -I '^$' samefile samefile.
809 enqueue(s1, '(', NULL);
811 enqueue(s1, ' ', NULL);
813 enqueue(s1, ' ', s2);
815 /* Ignore deleted lines. */
816 for (; file1ln < file1start; ++file1ln) {
819 if (!(s = xfgets(file1)))
820 errx(2, "file1 shorter than expected");
822 enqueue(s, '(', NULL);
824 /* Ignore added lines. */
825 for (; file2ln < file2start; ++file2ln) {
828 if (!(s = xfgets(file2)))
829 errx(2, "file2 shorter than expected");
831 /* If -l flag was given, don't print right column. */
835 enqueue(NULL, ')', s);
838 /* Process unmodified or skipped lines. */
843 printa(file2, file2end);
844 n = file2end - file2start + 1;
847 printc(file1, file1end, file2, file2end);
848 n = file1end - file1start + 1 + 1 + file2end - file2start + 1;
851 printd(file1, file1end);
852 n = file1end - file1start + 1;
855 errx(2, "invalid diff command: %c: %s", cmd, line);
859 /* Skip to next ed line. */
861 if (!(line = xfgets(diffpipe)))
862 errx(2, "diff ended early");
870 * Queues up a diff line.
873 enqueue(char *left, char divider, char *right)
875 struct diffline *diffp;
877 if (!(diffp = malloc(sizeof(struct diffline))))
880 diffp->div = divider;
881 diffp->right = right;
882 STAILQ_INSERT_TAIL(&diffhead, diffp, diffentries);
886 * Free a diffline structure and its elements.
889 freediff(struct diffline *diffp)
898 * Append second string into first. Repeated appends to the same string
899 * are cached, making this an O(n) function, where n = strlen(append).
902 astrcat(char **s, const char *append)
904 /* Length of string in previous run. */
905 static size_t offset = 0;
908 * String from previous run. Compared to *s to see if we are
909 * dealing with the same string. If so, we can use offset.
911 static const char *oldstr = NULL;
915 * First string is NULL, so just copy append.
918 if (!(*s = strdup(append)))
921 /* Keep track of string. */
929 * *s is a string so concatenate.
932 /* Did we process the same string in the last run? */
934 * If this is a different string from the one we just processed
942 /* Size = strlen(*s) + \n + strlen(append) + '\0'. */
943 newsiz = offset + 1 + strlen(append) + 1;
945 /* Resize *s to fit new string. */
946 newstr = realloc(*s, newsiz);
951 /* *s + offset should be end of string. */
953 strlcpy(*s + offset, "\n", newsiz - offset);
954 strlcat(*s + offset, append, newsiz - offset);
956 /* New string length should be exactly newsiz - 1 characters. */
957 /* Store generated string's values. */
963 * Process diff set queue, printing, prompting, and saving each diff
964 * line stored in queue.
969 struct diffline *diffp;
970 char divc, *left, *right;
972 /* Don't process empty queue. */
973 if (STAILQ_EMPTY(&diffhead))
976 /* Remember the divider. */
977 divc = STAILQ_FIRST(&diffhead)->div;
982 * Go through set of diffs, concatenating each line in left or
983 * right column into two long strings, `left' and `right'.
985 STAILQ_FOREACH(diffp, &diffhead, diffentries) {
987 * Print changed lines if -s was given,
988 * print all lines if -s was not given.
990 if (!sflag || diffp->div == '|' || diffp->div == '<' ||
992 println(diffp->left, diffp->div, diffp->right);
994 /* Append new lines to diff set. */
996 astrcat(&left, diffp->left);
998 astrcat(&right, diffp->right);
1001 /* Empty queue and free each diff line and its elements. */
1002 while (!STAILQ_EMPTY(&diffhead)) {
1003 diffp = STAILQ_FIRST(&diffhead);
1004 STAILQ_REMOVE_HEAD(&diffhead, diffentries);
1008 /* Write to outfp, prompting user if lines are different. */
1011 case ' ': case '(': case ')':
1012 fprintf(outfp, "%s\n", left);
1014 case '|': case '<': case '>':
1015 prompt(left, right);
1018 errx(2, "invalid divider: %c", divc);
1021 /* Free left and right. */
1027 * Print lines following an (a)ppend command.
1030 printa(FILE *file, size_t line2)
1034 for (; file2ln <= line2; ++file2ln) {
1035 if (!(line = xfgets(file)))
1036 errx(2, "append ended early");
1037 enqueue(NULL, '>', line);
1043 * Print lines following a (c)hange command, from file1ln to file1end
1044 * and from file2ln to file2end.
1047 printc(FILE *file1, size_t file1end, FILE *file2, size_t file2end)
1050 STAILQ_ENTRY(fileline) fileentries;
1053 STAILQ_HEAD(, fileline) delqhead = STAILQ_HEAD_INITIALIZER(delqhead);
1055 /* Read lines to be deleted. */
1056 for (; file1ln <= file1end; ++file1ln) {
1057 struct fileline *linep;
1060 /* Read lines from both. */
1061 if (!(line1 = xfgets(file1)))
1062 errx(2, "error reading file1 in delete in change");
1064 /* Add to delete queue. */
1065 if (!(linep = malloc(sizeof(struct fileline))))
1067 linep->line = line1;
1068 STAILQ_INSERT_TAIL(&delqhead, linep, fileentries);
1071 /* Process changed lines.. */
1072 for (; !STAILQ_EMPTY(&delqhead) && file2ln <= file2end;
1074 struct fileline *del;
1078 if (!(add = xfgets(file2)))
1079 errx(2, "error reading add in change");
1081 del = STAILQ_FIRST(&delqhead);
1082 enqueue(del->line, '|', add);
1083 STAILQ_REMOVE_HEAD(&delqhead, fileentries);
1085 * Free fileline structure but not its elements since
1086 * they are queued up.
1092 /* Process remaining lines to add. */
1093 for (; file2ln <= file2end; ++file2ln) {
1097 if (!(add = xfgets(file2)))
1098 errx(2, "error reading add in change");
1100 enqueue(NULL, '>', add);
1104 /* Process remaining lines to delete. */
1105 while (!STAILQ_EMPTY(&delqhead)) {
1106 struct fileline *filep;
1108 filep = STAILQ_FIRST(&delqhead);
1109 enqueue(filep->line, '<', NULL);
1110 STAILQ_REMOVE_HEAD(&delqhead, fileentries);
1117 * Print deleted lines from file, from file1ln to file1end.
1120 printd(FILE *file1, size_t file1end)
1124 /* Print out lines file1ln to line2. */
1125 for (; file1ln <= file1end; ++file1ln) {
1126 if (!(line1 = xfgets(file1)))
1127 errx(2, "file1 ended early in delete");
1128 enqueue(line1, '<', NULL);
1134 * Interactive mode usage.
1140 puts("e:\tedit blank diff\n"
1141 "eb:\tedit both diffs concatenated\n"
1142 "el:\tedit left diff\n"
1143 "er:\tedit right diff\n"
1144 "l | 1:\tchoose left diff\n"
1145 "r | 2:\tchoose right diff\n"
1146 "s:\tsilent mode--don't print identical lines\n"
1147 "v:\tverbose mode--print identical lines\n"
1156 "usage: sdiff [-abdilstHW] [-I regexp] [-o outfile] [-w width] file1"