1 /* Commandline diff utility to test diff implementations. */
3 * Copyright (c) 2018 Martin Pieuchot
4 * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
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.
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.
31 #include <diff/arraylist.h>
32 #include <diff/diff_main.h>
33 #include <diff/diff_output.h>
36 /* stupid shims to compile and test on linux */
39 static const char *getprogname()
45 __dead void usage(void);
46 int diffreg(char *, char *, int);
47 int openfile(const char *, char **, struct stat *);
53 "usage: %s [-p] file1 file2\n"
55 " -p Use Patience Diff (slower but often nicer)\n"
60 static bool do_patience = false;
63 main(int argc, char *argv[])
67 while ((ch = getopt(argc, argv, "p")) != -1) {
83 rc = diffreg(argv[0], argv[1], 0);
84 if (rc != DIFF_RC_OK) {
85 fprintf(stderr, "diff: %s\n", strerror(rc));
91 const struct diff_algo_config myers_then_patience;
92 const struct diff_algo_config myers_then_myers_divide;
93 const struct diff_algo_config patience;
94 const struct diff_algo_config myers_divide;
96 const struct diff_algo_config myers_then_patience = (struct diff_algo_config){
97 .impl = diff_algo_myers,
98 .permitted_state_size = 1024 * 1024 * sizeof(int),
99 .fallback_algo = &patience,
102 const struct diff_algo_config myers_then_myers_divide =
103 (struct diff_algo_config){
104 .impl = diff_algo_myers,
105 .permitted_state_size = 1024 * 1024 * sizeof(int),
106 .fallback_algo = &myers_divide,
109 const struct diff_algo_config patience = (struct diff_algo_config){
110 .impl = diff_algo_patience,
111 /* After subdivision, do Patience again: */
112 .inner_algo = &patience,
113 /* If subdivision failed, do Myers Divide et Impera: */
114 .fallback_algo = &myers_then_myers_divide,
117 const struct diff_algo_config myers_divide = (struct diff_algo_config){
118 .impl = diff_algo_myers_divide,
119 /* When division succeeded, start from the top: */
120 .inner_algo = &myers_then_myers_divide,
121 /* (fallback_algo = NULL implies diff_algo_none). */
124 const struct diff_config diff_config = {
125 .atomize_func = diff_atomize_text_by_line,
126 .algo = &myers_then_myers_divide,
129 const struct diff_config diff_config_patience = {
130 .atomize_func = diff_atomize_text_by_line,
131 .algo = &myers_then_patience,
135 diffreg(char *file1, char *file2, int flags)
139 struct stat st1, st2;
140 struct diff_input_info info = {
144 struct diff_result *result;
146 const struct diff_config *cfg;
148 cfg = do_patience ? &diff_config_patience : &diff_config;
150 fd1 = openfile(file1, &str1, &st1);
151 fd2 = openfile(file2, &str2, &st2);
153 result = diff_main(cfg, fd1, str1, st1.st_size, fd2, str2, st2.st_size);
155 rc = diff_output_plain(stdout, &info, result);
157 rc = diff_output_unidiff(stdout, &info, result, 3);
159 diff_result_free(result);
162 munmap(str1, st1.st_size);
164 munmap(str2, st2.st_size);
172 openfile(const char *path, char **p, struct stat *st)
176 fd = open(path, O_RDONLY);
180 if (fstat(fd, st) == -1)
184 *p = mmap(NULL, st->st_size, PROT_READ, MAP_PRIVATE, fd, 0);
185 if (*p == MAP_FAILED)
187 *p = NULL; /* fall back on file I/O */