jcs's openbsd hax
openbsd
1/* $OpenBSD: diff.c,v 1.69 2026/02/18 15:25:01 deraadt Exp $ */
2
3/*
4 * Copyright (c) 2003 Todd C. Miller <millert@openbsd.org>
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/stat.h>
24
25#include <ctype.h>
26#include <err.h>
27#include <errno.h>
28#include <getopt.h>
29#include <stdlib.h>
30#include <stdio.h>
31#include <stdarg.h>
32#include <string.h>
33#include <unistd.h>
34#include <limits.h>
35
36#include "diff.h"
37#include "xmalloc.h"
38
39int Nflag, Pflag, rflag, sflag, Tflag;
40int diff_format, diff_context, status;
41char *start, *ifdefname, *diffargs, *label[2], *ignore_pats;
42struct stat stb1, stb2;
43struct excludes *excludes_list;
44regex_t ignore_re;
45
46#define OPTIONS "0123456789abC:cdD:efhI:iL:nNPpqrS:sTtU:uwX:x:"
47static struct option longopts[] = {
48 { "text", no_argument, 0, 'a' },
49 { "ignore-space-change", no_argument, 0, 'b' },
50 { "context", optional_argument, 0, 'C' },
51 { "ifdef", required_argument, 0, 'D' },
52 { "minimal", no_argument, 0, 'd' },
53 { "ed", no_argument, 0, 'e' },
54 { "forward-ed", no_argument, 0, 'f' },
55 { "ignore-matching-lines", required_argument, 0, 'I' },
56 { "ignore-case", no_argument, 0, 'i' },
57 { "label", required_argument, 0, 'L' },
58 { "new-file", no_argument, 0, 'N' },
59 { "rcs", no_argument, 0, 'n' },
60 { "unidirectional-new-file", no_argument, 0, 'P' },
61 { "show-c-function", no_argument, 0, 'p' },
62 { "brief", no_argument, 0, 'q' },
63 { "recursive", no_argument, 0, 'r' },
64 { "report-identical-files", no_argument, 0, 's' },
65 { "starting-file", required_argument, 0, 'S' },
66 { "expand-tabs", no_argument, 0, 't' },
67 { "initial-tab", no_argument, 0, 'T' },
68 { "unified", optional_argument, 0, 'U' },
69 { "ignore-all-space", no_argument, 0, 'w' },
70 { "exclude", required_argument, 0, 'x' },
71 { "exclude-from", required_argument, 0, 'X' },
72 { NULL, 0, 0, '\0'}
73};
74
75__dead void usage(void);
76void push_excludes(char *);
77void push_ignore_pats(char *);
78void read_excludes_file(char *file);
79void set_argstr(char **, char **);
80
81int
82main(int argc, char **argv)
83{
84 char *ep, **oargv;
85 long l;
86 int ch, dflags, lastch, gotstdin, prevoptind, newarg;
87
88 oargv = argv;
89 gotstdin = 0;
90 dflags = 0;
91 lastch = '\0';
92 prevoptind = 1;
93 newarg = 1;
94 while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) {
95 switch (ch) {
96 case '0': case '1': case '2': case '3': case '4':
97 case '5': case '6': case '7': case '8': case '9':
98 if (newarg)
99 usage(); /* disallow -[0-9]+ */
100 else if (lastch == 'c' || lastch == 'u')
101 diff_context = 0;
102 else if (!isdigit(lastch) || diff_context > INT_MAX / 10)
103 usage();
104 diff_context = (diff_context * 10) + (ch - '0');
105 break;
106 case 'a':
107 dflags |= D_FORCEASCII;
108 break;
109 case 'b':
110 dflags |= D_FOLDBLANKS;
111 break;
112 case 'C':
113 case 'c':
114 diff_format = D_CONTEXT;
115 if (optarg != NULL) {
116 l = strtol(optarg, &ep, 10);
117 if (*ep != '\0' || l < 0 || l >= INT_MAX)
118 usage();
119 diff_context = (int)l;
120 } else
121 diff_context = 3;
122 break;
123 case 'd':
124 dflags |= D_MINIMAL;
125 break;
126 case 'D':
127 diff_format = D_IFDEF;
128 ifdefname = optarg;
129 break;
130 case 'e':
131 diff_format = D_EDIT;
132 break;
133 case 'f':
134 diff_format = D_REVERSE;
135 break;
136 case 'h':
137 /* silently ignore for backwards compatibility */
138 break;
139 case 'I':
140 push_ignore_pats(optarg);
141 break;
142 case 'i':
143 dflags |= D_IGNORECASE;
144 break;
145 case 'L':
146 if (label[0] == NULL)
147 label[0] = optarg;
148 else if (label[1] == NULL)
149 label[1] = optarg;
150 else
151 usage();
152 break;
153 case 'N':
154 Nflag = 1;
155 break;
156 case 'n':
157 diff_format = D_NREVERSE;
158 break;
159 case 'p':
160 dflags |= D_PROTOTYPE;
161 break;
162 case 'P':
163 Pflag = 1;
164 break;
165 case 'r':
166 rflag = 1;
167 break;
168 case 'q':
169 diff_format = D_BRIEF;
170 break;
171 case 'S':
172 start = optarg;
173 break;
174 case 's':
175 sflag = 1;
176 break;
177 case 'T':
178 Tflag = 1;
179 break;
180 case 't':
181 dflags |= D_EXPANDTABS;
182 break;
183 case 'U':
184 case 'u':
185 diff_format = D_UNIFIED;
186 if (optarg != NULL) {
187 l = strtol(optarg, &ep, 10);
188 if (*ep != '\0' || l < 0 || l >= INT_MAX)
189 usage();
190 diff_context = (int)l;
191 } else
192 diff_context = 3;
193 break;
194 case 'w':
195 dflags |= D_IGNOREBLANKS;
196 break;
197 case 'X':
198 read_excludes_file(optarg);
199 break;
200 case 'x':
201 push_excludes(optarg);
202 break;
203 default:
204 usage();
205 break;
206 }
207 lastch = ch;
208 newarg = optind != prevoptind;
209 prevoptind = optind;
210 }
211 argc -= optind;
212 argv += optind;
213
214 if (unveil("/tmp", "rwc") == -1)
215 err(2, "unveil /tmp");
216 if (unveil("/", "r") == -1)
217 err(2, "unveil /");
218 if (pledge("stdio rpath wpath cpath", NULL) == -1)
219 err(2, "pledge");
220
221 /*
222 * Do sanity checks, fill in stb1 and stb2 and call the appropriate
223 * driver routine. Both drivers use the contents of stb1 and stb2.
224 */
225 if (argc != 2)
226 usage();
227 if (ignore_pats != NULL) {
228 char buf[BUFSIZ];
229 int error;
230
231 if ((error = regcomp(&ignore_re, ignore_pats,
232 REG_NEWLINE | REG_EXTENDED)) != 0) {
233 regerror(error, &ignore_re, buf, sizeof(buf));
234 if (*ignore_pats != '\0')
235 errx(2, "%s: %s", ignore_pats, buf);
236 else
237 errx(2, "%s", buf);
238 }
239 }
240 if (strcmp(argv[0], "-") == 0) {
241 fstat(STDIN_FILENO, &stb1);
242 gotstdin = 1;
243 } else if (stat(argv[0], &stb1) != 0)
244 err(2, "%s", argv[0]);
245 if (strcmp(argv[1], "-") == 0) {
246 fstat(STDIN_FILENO, &stb2);
247 gotstdin = 1;
248 } else if (stat(argv[1], &stb2) != 0)
249 err(2, "%s", argv[1]);
250 if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode)))
251 errx(2, "can't compare - to a directory");
252 set_argstr(oargv, argv);
253 if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
254 if (diff_format == D_IFDEF)
255 errx(2, "-D option not supported with directories");
256 diffdir(argv[0], argv[1], dflags);
257 } else {
258 if (S_ISDIR(stb1.st_mode)) {
259 argv[0] = splice(argv[0], argv[1]);
260 if (stat(argv[0], &stb1) == -1)
261 err(2, "%s", argv[0]);
262 }
263 if (S_ISDIR(stb2.st_mode)) {
264 argv[1] = splice(argv[1], argv[0]);
265 if (stat(argv[1], &stb2) == -1)
266 err(2, "%s", argv[1]);
267 }
268 print_status(diffreg(argv[0], argv[1], dflags), argv[0], argv[1],
269 "");
270 }
271 exit(status);
272}
273
274void
275set_argstr(char **av, char **ave)
276{
277 size_t argsize;
278 char **ap;
279
280 argsize = 4 + *ave - *av + 1;
281 diffargs = xmalloc(argsize);
282 strlcpy(diffargs, "diff", argsize);
283 for (ap = av + 1; ap < ave; ap++) {
284 if (strcmp(*ap, "--") != 0) {
285 strlcat(diffargs, " ", argsize);
286 strlcat(diffargs, *ap, argsize);
287 }
288 }
289}
290
291/*
292 * Read in an excludes file and push each line.
293 */
294void
295read_excludes_file(char *file)
296{
297 FILE *fp;
298 char *buf, *pattern;
299 size_t len;
300
301 if (strcmp(file, "-") == 0)
302 fp = stdin;
303 else if ((fp = fopen(file, "r")) == NULL)
304 err(2, "%s", file);
305 while ((buf = fgetln(fp, &len)) != NULL) {
306 if (buf[len - 1] == '\n')
307 len--;
308 pattern = xmalloc(len + 1);
309 memcpy(pattern, buf, len);
310 pattern[len] = '\0';
311 push_excludes(pattern);
312 }
313 if (strcmp(file, "-") != 0)
314 fclose(fp);
315}
316
317/*
318 * Push a pattern onto the excludes list.
319 */
320void
321push_excludes(char *pattern)
322{
323 struct excludes *entry;
324
325 entry = xmalloc(sizeof(*entry));
326 entry->pattern = pattern;
327 entry->next = excludes_list;
328 excludes_list = entry;
329}
330
331void
332push_ignore_pats(char *pattern)
333{
334 size_t len;
335
336 if (ignore_pats == NULL)
337 ignore_pats = xstrdup(pattern);
338 else {
339 /* old + "|" + new + NUL */
340 len = strlen(ignore_pats) + strlen(pattern) + 2;
341 ignore_pats = xreallocarray(ignore_pats, 1, len);
342 strlcat(ignore_pats, "|", len);
343 strlcat(ignore_pats, pattern, len);
344 }
345}
346
347void
348print_only(const char *path, size_t dirlen, const char *entry)
349{
350 if (dirlen > 1)
351 dirlen--;
352 printf("Only in %.*s: %s\n", (int)dirlen, path, entry);
353}
354
355void
356print_status(int val, char *path1, char *path2, char *entry)
357{
358 switch (val) {
359 case D_BINARY:
360 printf("Binary files %s%s and %s%s differ\n",
361 path1, entry, path2, entry);
362 break;
363 case D_DIFFER:
364 if (diff_format == D_BRIEF)
365 printf("Files %s%s and %s%s differ\n",
366 path1, entry, path2, entry);
367 break;
368 case D_SAME:
369 if (sflag)
370 printf("Files %s%s and %s%s are identical\n",
371 path1, entry, path2, entry);
372 break;
373 case D_MISMATCH1:
374 printf("File %s%s is a directory while file %s%s is a regular file\n",
375 path1, entry, path2, entry);
376 break;
377 case D_MISMATCH2:
378 printf("File %s%s is a regular file while file %s%s is a directory\n",
379 path1, entry, path2, entry);
380 break;
381 case D_SKIPPED1:
382 printf("File %s%s is not a regular file or directory and was skipped\n",
383 path1, entry);
384 break;
385 case D_SKIPPED2:
386 printf("File %s%s is not a regular file or directory and was skipped\n",
387 path2, entry);
388 break;
389 }
390}
391
392__dead void
393usage(void)
394{
395 (void)fprintf(stderr,
396 "usage: diff [-abdipTtw] [-c | -e | -f | -n | -q | -u] [-I pattern] [-L label]\n"
397 " file1 file2\n"
398 " diff [-abdipTtw] [-I pattern] [-L label] -C number file1 file2\n"
399 " diff [-abditw] [-I pattern] -D string file1 file2\n"
400 " diff [-abdipTtw] [-I pattern] [-L label] -U number file1 file2\n"
401 " diff [-abdiNPprsTtw] [-c | -e | -f | -n | -q | -u] [-I pattern]\n"
402 " [-L label] [-S name] [-X file] [-x pattern] dir1 dir2\n");
403
404 exit(2);
405}