]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/diff/diff.c
MFC r357648:
[FreeBSD/FreeBSD.git] / usr.bin / diff / diff.c
1 /*      $OpenBSD: diff.c,v 1.65 2015/12/29 19:04:46 gsoares Exp $       */
2
3 /*
4  * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  * Sponsored in part by the Defense Advanced Research Projects
19  * Agency (DARPA) and Air Force Research Laboratory, Air Force
20  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
21  */
22
23 #include <sys/cdefs.h>
24 __FBSDID("$FreeBSD$");
25
26 #include <sys/stat.h>
27
28 #include <ctype.h>
29 #include <err.h>
30 #include <getopt.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <limits.h>
36
37 #include "diff.h"
38 #include "xmalloc.h"
39
40 int      lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag, Wflag;
41 int      diff_format, diff_context, status, ignore_file_case, suppress_common;
42 int      tabsize = 8, width = 130;
43 char    *start, *ifdefname, *diffargs, *label[2], *ignore_pats;
44 char    *group_format = NULL;
45 struct stat stb1, stb2;
46 struct excludes *excludes_list;
47 regex_t  ignore_re;
48
49 #define OPTIONS "0123456789aBbC:cdD:efHhI:iL:lnNPpqrS:sTtU:uwW:X:x:y"
50 enum {
51         OPT_TSIZE = CHAR_MAX + 1,
52         OPT_STRIPCR,
53         OPT_IGN_FN_CASE,
54         OPT_NO_IGN_FN_CASE,
55         OPT_NORMAL,
56         OPT_HORIZON_LINES,
57         OPT_CHANGED_GROUP_FORMAT,
58         OPT_SUPPRESS_COMMON,
59 };
60
61 static struct option longopts[] = {
62         { "text",                       no_argument,            0,      'a' },
63         { "ignore-space-change",        no_argument,            0,      'b' },
64         { "context",                    optional_argument,      0,      'C' },
65         { "ifdef",                      required_argument,      0,      'D' },
66         { "minimal",                    no_argument,            0,      'd' },
67         { "ed",                         no_argument,            0,      'e' },
68         { "forward-ed",                 no_argument,            0,      'f' },
69         { "speed-large-files",          no_argument,            NULL,   'H' },
70         { "ignore-blank-lines",         no_argument,            0,      'B' },
71         { "ignore-matching-lines",      required_argument,      0,      'I' },
72         { "ignore-case",                no_argument,            0,      'i' },
73         { "paginate",                   no_argument,            NULL,   'l' },
74         { "label",                      required_argument,      0,      'L' },
75         { "new-file",                   no_argument,            0,      'N' },
76         { "rcs",                        no_argument,            0,      'n' },
77         { "unidirectional-new-file",    no_argument,            0,      'P' },
78         { "show-c-function",            no_argument,            0,      'p' },
79         { "brief",                      no_argument,            0,      'q' },
80         { "recursive",                  no_argument,            0,      'r' },
81         { "report-identical-files",     no_argument,            0,      's' },
82         { "starting-file",              required_argument,      0,      'S' },
83         { "expand-tabs",                no_argument,            0,      't' },
84         { "initial-tab",                no_argument,            0,      'T' },
85         { "unified",                    optional_argument,      0,      'U' },
86         { "ignore-all-space",           no_argument,            0,      'w' },
87         { "width",                      required_argument,      0,      'W' },
88         { "exclude",                    required_argument,      0,      'x' },
89         { "exclude-from",               required_argument,      0,      'X' },
90         { "side-by-side",               no_argument,            NULL,   'y' },
91         { "ignore-file-name-case",      no_argument,            NULL,   OPT_IGN_FN_CASE },
92         { "horizon-lines",              required_argument,      NULL,   OPT_HORIZON_LINES },
93         { "no-ignore-file-name-case",   no_argument,            NULL,   OPT_NO_IGN_FN_CASE },
94         { "normal",                     no_argument,            NULL,   OPT_NORMAL },
95         { "strip-trailing-cr",          no_argument,            NULL,   OPT_STRIPCR },
96         { "tabsize",                    optional_argument,      NULL,   OPT_TSIZE },
97         { "changed-group-format",       required_argument,      NULL,   OPT_CHANGED_GROUP_FORMAT},
98         { "suppress-common-lines",      no_argument,            NULL,   OPT_SUPPRESS_COMMON },
99         { NULL,                         0,                      0,      '\0'}
100 };
101
102 void usage(void) __dead2;
103 void push_excludes(char *);
104 void push_ignore_pats(char *);
105 void read_excludes_file(char *file);
106 void set_argstr(char **, char **);
107
108 int
109 main(int argc, char **argv)
110 {
111         const char *errstr = NULL;
112         char *ep, **oargv;
113         long  l;
114         int   ch, dflags, lastch, gotstdin, prevoptind, newarg;
115
116         oargv = argv;
117         gotstdin = 0;
118         dflags = 0;
119         lastch = '\0';
120         prevoptind = 1;
121         newarg = 1;
122         diff_context = 3;
123         diff_format = 0;
124         while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) {
125                 switch (ch) {
126                 case '0': case '1': case '2': case '3': case '4':
127                 case '5': case '6': case '7': case '8': case '9':
128                         if (newarg)
129                                 usage();        /* disallow -[0-9]+ */
130                         else if (lastch == 'c' || lastch == 'u')
131                                 diff_context = 0;
132                         else if (!isdigit(lastch) || diff_context > INT_MAX / 10)
133                                 usage();
134                         diff_context = (diff_context * 10) + (ch - '0');
135                         break;
136                 case 'a':
137                         dflags |= D_FORCEASCII;
138                         break;
139                 case 'b':
140                         dflags |= D_FOLDBLANKS;
141                         break;
142                 case 'C':
143                 case 'c':
144                         cflag = 1;
145                         diff_format = D_CONTEXT;
146                         if (optarg != NULL) {
147                                 l = strtol(optarg, &ep, 10);
148                                 if (*ep != '\0' || l < 0 || l >= INT_MAX)
149                                         usage();
150                                 diff_context = (int)l;
151                         }
152                         break;
153                 case 'd':
154                         dflags |= D_MINIMAL;
155                         break;
156                 case 'D':
157                         diff_format = D_IFDEF;
158                         ifdefname = optarg;
159                         break;
160                 case 'e':
161                         diff_format = D_EDIT;
162                         break;
163                 case 'f':
164                         diff_format = D_REVERSE;
165                         break;
166                 case 'H':
167                         /* ignore but needed for compatibility with GNU diff */
168                         break;
169                 case 'h':
170                         /* silently ignore for backwards compatibility */
171                         break;
172                 case 'B':
173                         dflags |= D_SKIPBLANKLINES;
174                         break;
175                 case 'I':
176                         push_ignore_pats(optarg);
177                         break;
178                 case 'i':
179                         dflags |= D_IGNORECASE;
180                         break;
181                 case 'L':
182                         if (label[0] == NULL)
183                                 label[0] = optarg;
184                         else if (label[1] == NULL)
185                                 label[1] = optarg;
186                         else
187                                 usage();
188                         break;
189                 case 'l':
190                         lflag = 1;
191                         break;
192                 case 'N':
193                         Nflag = 1;
194                         break;
195                 case 'n':
196                         diff_format = D_NREVERSE;
197                         break;
198                 case 'p':
199                         if (diff_format == 0)
200                                 diff_format = D_CONTEXT;
201                         dflags |= D_PROTOTYPE;
202                         break;
203                 case 'P':
204                         Pflag = 1;
205                         break;
206                 case 'r':
207                         rflag = 1;
208                         break;
209                 case 'q':
210                         diff_format = D_BRIEF;
211                         break;
212                 case 'S':
213                         start = optarg;
214                         break;
215                 case 's':
216                         sflag = 1;
217                         break;
218                 case 'T':
219                         Tflag = 1;
220                         break;
221                 case 't':
222                         dflags |= D_EXPANDTABS;
223                         break;
224                 case 'U':
225                 case 'u':
226                         diff_format = D_UNIFIED;
227                         if (optarg != NULL) {
228                                 l = strtol(optarg, &ep, 10);
229                                 if (*ep != '\0' || l < 0 || l >= INT_MAX)
230                                         usage();
231                                 diff_context = (int)l;
232                         }
233                         break;
234                 case 'w':
235                         dflags |= D_IGNOREBLANKS;
236                         break;
237                 case 'W':
238                         Wflag = 1;
239                         width = (int) strtonum(optarg, 1, INT_MAX, &errstr);
240                         if (errstr) {
241                                 warnx("Invalid argument for width");
242                                 usage();
243                         }
244                         break;
245                 case 'X':
246                         read_excludes_file(optarg);
247                         break;
248                 case 'x':
249                         push_excludes(optarg);
250                         break;
251                 case 'y':
252                         diff_format = D_SIDEBYSIDE;
253                         break;
254                 case OPT_CHANGED_GROUP_FORMAT:
255                         diff_format = D_GFORMAT;
256                         group_format = optarg;
257                         break;
258                 case OPT_HORIZON_LINES:
259                         break; /* XXX TODO for compatibility with GNU diff3 */
260                 case OPT_IGN_FN_CASE:
261                         ignore_file_case = 1;
262                         break;
263                 case OPT_NO_IGN_FN_CASE:
264                         ignore_file_case = 0;
265                         break;
266                 case OPT_NORMAL:
267                         diff_format = D_NORMAL;
268                         break;
269                 case OPT_TSIZE:
270                         tabsize = (int) strtonum(optarg, 1, INT_MAX, &errstr);
271                         if (errstr) {
272                                 warnx("Invalid argument for tabsize");
273                                 usage();
274                         }
275                         break;
276                 case OPT_STRIPCR:
277                         dflags |= D_STRIPCR;
278                         break;
279                 case OPT_SUPPRESS_COMMON:
280                         suppress_common = 1;
281                         break;
282                 default:
283                         usage();
284                         break;
285                 }
286                 lastch = ch;
287                 newarg = optind != prevoptind;
288                 prevoptind = optind;
289         }
290         argc -= optind;
291         argv += optind;
292
293 #ifdef __OpenBSD__
294         if (pledge("stdio rpath tmppath", NULL) == -1)
295                 err(2, "pledge");
296 #endif
297
298         /*
299          * Do sanity checks, fill in stb1 and stb2 and call the appropriate
300          * driver routine.  Both drivers use the contents of stb1 and stb2.
301          */
302         if (argc != 2)
303                 usage();
304         if (ignore_pats != NULL) {
305                 char buf[BUFSIZ];
306                 int error;
307
308                 if ((error = regcomp(&ignore_re, ignore_pats,
309                                      REG_NEWLINE | REG_EXTENDED)) != 0) {
310                         regerror(error, &ignore_re, buf, sizeof(buf));
311                         if (*ignore_pats != '\0')
312                                 errx(2, "%s: %s", ignore_pats, buf);
313                         else
314                                 errx(2, "%s", buf);
315                 }
316         }
317         if (strcmp(argv[0], "-") == 0) {
318                 fstat(STDIN_FILENO, &stb1);
319                 gotstdin = 1;
320         } else if (stat(argv[0], &stb1) != 0)
321                 err(2, "%s", argv[0]);
322         if (strcmp(argv[1], "-") == 0) {
323                 fstat(STDIN_FILENO, &stb2);
324                 gotstdin = 1;
325         } else if (stat(argv[1], &stb2) != 0)
326                 err(2, "%s", argv[1]);
327         if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode)))
328                 errx(2, "can't compare - to a directory");
329         set_argstr(oargv, argv);
330         if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
331                 if (diff_format == D_IFDEF)
332                         errx(2, "-D option not supported with directories");
333                 diffdir(argv[0], argv[1], dflags);
334         } else {
335                 if (S_ISDIR(stb1.st_mode)) {
336                         argv[0] = splice(argv[0], argv[1]);
337                         if (stat(argv[0], &stb1) < 0)
338                                 err(2, "%s", argv[0]);
339                 }
340                 if (S_ISDIR(stb2.st_mode)) {
341                         argv[1] = splice(argv[1], argv[0]);
342                         if (stat(argv[1], &stb2) < 0)
343                                 err(2, "%s", argv[1]);
344                 }
345                 print_status(diffreg(argv[0], argv[1], dflags, 1), argv[0],
346                     argv[1], "");
347         }
348         exit(status);
349 }
350
351 void
352 set_argstr(char **av, char **ave)
353 {
354         size_t argsize;
355         char **ap;
356
357         argsize = 4 + *ave - *av + 1;
358         diffargs = xmalloc(argsize);
359         strlcpy(diffargs, "diff", argsize);
360         for (ap = av + 1; ap < ave; ap++) {
361                 if (strcmp(*ap, "--") != 0) {
362                         strlcat(diffargs, " ", argsize);
363                         strlcat(diffargs, *ap, argsize);
364                 }
365         }
366 }
367
368 /*
369  * Read in an excludes file and push each line.
370  */
371 void
372 read_excludes_file(char *file)
373 {
374         FILE *fp;
375         char *buf, *pattern;
376         size_t len;
377
378         if (strcmp(file, "-") == 0)
379                 fp = stdin;
380         else if ((fp = fopen(file, "r")) == NULL)
381                 err(2, "%s", file);
382         while ((buf = fgetln(fp, &len)) != NULL) {
383                 if (buf[len - 1] == '\n')
384                         len--;
385                 if ((pattern = strndup(buf, len)) == NULL)
386                         err(2, "xstrndup");
387                 push_excludes(pattern);
388         }
389         if (strcmp(file, "-") != 0)
390                 fclose(fp);
391 }
392
393 /*
394  * Push a pattern onto the excludes list.
395  */
396 void
397 push_excludes(char *pattern)
398 {
399         struct excludes *entry;
400
401         entry = xmalloc(sizeof(*entry));
402         entry->pattern = pattern;
403         entry->next = excludes_list;
404         excludes_list = entry;
405 }
406
407 void
408 push_ignore_pats(char *pattern)
409 {
410         size_t len;
411
412         if (ignore_pats == NULL)
413                 ignore_pats = xstrdup(pattern);
414         else {
415                 /* old + "|" + new + NUL */
416                 len = strlen(ignore_pats) + strlen(pattern) + 2;
417                 ignore_pats = xreallocarray(ignore_pats, 1, len);
418                 strlcat(ignore_pats, "|", len);
419                 strlcat(ignore_pats, pattern, len);
420         }
421 }
422
423 void
424 print_only(const char *path, size_t dirlen, const char *entry)
425 {
426         if (dirlen > 1)
427                 dirlen--;
428         printf("Only in %.*s: %s\n", (int)dirlen, path, entry);
429 }
430
431 void
432 print_status(int val, char *path1, char *path2, const char *entry)
433 {
434         switch (val) {
435         case D_BINARY:
436                 printf("Binary files %s%s and %s%s differ\n",
437                     path1, entry, path2, entry);
438                 break;
439         case D_DIFFER:
440                 if (diff_format == D_BRIEF)
441                         printf("Files %s%s and %s%s differ\n",
442                             path1, entry, path2, entry);
443                 break;
444         case D_SAME:
445                 if (sflag)
446                         printf("Files %s%s and %s%s are identical\n",
447                             path1, entry, path2, entry);
448                 break;
449         case D_MISMATCH1:
450                 printf("File %s%s is a directory while file %s%s is a regular file\n",
451                     path1, entry, path2, entry);
452                 break;
453         case D_MISMATCH2:
454                 printf("File %s%s is a regular file while file %s%s is a directory\n",
455                     path1, entry, path2, entry);
456                 break;
457         case D_SKIPPED1:
458                 printf("File %s%s is not a regular file or directory and was skipped\n",
459                     path1, entry);
460                 break;
461         case D_SKIPPED2:
462                 printf("File %s%s is not a regular file or directory and was skipped\n",
463                     path2, entry);
464                 break;
465         }
466 }
467
468 void
469 usage(void)
470 {
471         (void)fprintf(stderr,
472             "usage: diff [-aBbdilpTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n"
473             "            [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n"
474             "            [-I pattern] [-L label] file1 file2\n"
475             "       diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n"
476             "            [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n"
477             "            -C number file1 file2\n"
478             "       diff [-aBbdiltw] [-I pattern] [--ignore-case] [--no-ignore-case]\n"
479             "            [--normal] [--strip-trailing-cr] [--tabsize] -D string file1 file2\n"
480             "       diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n"
481             "            [--no-ignore-case] [--normal] [--tabsize] [--strip-trailing-cr]\n"
482             "            -U number file1 file2\n"
483             "       diff [-aBbdilNPprsTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n"
484             "            [--no-ignore-case] [--normal] [--tabsize] [-I pattern] [-L label]\n"
485             "            [-S name] [-X file] [-x pattern] dir1 dir2\n"
486             "       diff [-aBbditwW] [--expand-tabs] [--ignore-all-blanks]\n"
487             "            [--ignore-blank-lines] [--ignore-case] [--minimal]\n"
488             "            [--no-ignore-file-name-case] [--strip-trailing-cr]\n"
489             "            [--suppress-common-lines] [--tabsize] [--text] [--width]\n"
490             "            -y | --side-by-side file1 file2\n");
491
492         exit(2);
493 }