Blob


1 /* Commandline diff utility to test diff implementations. */
2 /*
3 * Copyright (c) 2018 Martin Pieuchot
4 * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
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 */
19 #include <sys/mman.h>
20 #include <sys/stat.h>
22 #include <err.h>
23 #include <fcntl.h>
24 #include <inttypes.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
29 #ifdef __linux__
30 /* stupid shims to compile and test on linux */
31 #define __dead
33 static const char *getprogname()
34 {
35 return "diff";
36 }
37 #endif
39 __dead void usage(void);
40 int diffreg(char *, char *, int);
41 char *mmapfile(const char *, struct stat *);
43 __dead void
44 usage(void)
45 {
46 fprintf(stderr, "usage: %s file1 file2\n", getprogname());
47 exit(1);
48 }
50 int
51 main(int argc, char *argv[])
52 {
53 int ch;
55 while ((ch = getopt(argc, argv, "")) != -1) {
56 switch (ch) {
57 default:
58 usage();
59 }
60 }
62 argc -= optind;
63 argv += optind;
65 if (argc != 2)
66 usage();
68 return diffreg(argv[0], argv[1], 0);
69 }
71 #include <diff/diff_main.h>
72 #include <diff/diff_output.h>
74 const struct diff_algo_config myers, patience, myers_divide;
76 const struct diff_algo_config myers = (struct diff_algo_config){
77 .impl = diff_algo_myers,
78 .permitted_state_size = 104 * sizeof(int),
79 .fallback_algo = &patience,
80 };
82 const struct diff_algo_config patience = (struct diff_algo_config){
83 .impl = diff_algo_patience,
84 .inner_algo = &patience, // After subdivision, do Patience again.
85 .fallback_algo = &myers_divide, // If subdivision failed, do Myers Divide et Impera.
86 };
88 const struct diff_algo_config myers_divide = (struct diff_algo_config){
89 .impl = diff_algo_myers_divide,
90 .inner_algo = &myers, // When division succeeded, start from the top.
91 // (fallback_algo = NULL implies diff_algo_none).
92 };
94 const struct diff_config diff_config = {
95 .atomize_func = diff_atomize_text_by_line,
96 .algo = &myers,
97 };
99 int
100 diffreg(char *file1, char *file2, int flags)
102 char *str1, *str2;
103 struct stat st1, st2;
104 struct diff_input_info info = {
105 .left_path = file1,
106 .right_path = file2,
107 };
109 str1 = mmapfile(file1, &st1);
110 str2 = mmapfile(file2, &st2);
112 diff_plain(stdout, &diff_config, &info, str1, st1.st_size, str2, st2.st_size);
114 munmap(str1, st1.st_size);
115 munmap(str2, st2.st_size);
117 return 0;
120 char *
121 mmapfile(const char *path, struct stat *st)
123 int fd;
124 char *p;
126 fd = open(path, O_RDONLY);
127 if (fd == -1)
128 err(2, "%s", path);
130 if (fstat(fd, st) == -1)
131 err(2, "%s", path);
133 if ((uintmax_t)st->st_size > SIZE_MAX)
134 errx(2, "%s: file too big to fit memory", path);
136 p = mmap(NULL, st->st_size, PROT_READ, MAP_PRIVATE, fd, 0);
137 if (p == MAP_FAILED)
138 err(2, "mmap");
140 close(fd);
142 return p;