xref: /freebsd/usr.bin/cmp/regular.c (revision 9336e0699bda8a301cd2bfa37106b6ec5e32012e)
1 /*-
2  * Copyright (c) 1991, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #if 0
35 #ifndef lint
36 static char sccsid[] = "@(#)regular.c	8.3 (Berkeley) 4/2/94";
37 #endif
38 #endif
39 
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42 
43 #include <sys/param.h>
44 #include <sys/mman.h>
45 #include <sys/stat.h>
46 
47 #include <err.h>
48 #include <errno.h>
49 #include <limits.h>
50 #include <signal.h>
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include <unistd.h>
55 
56 #include "extern.h"
57 
58 static u_char *remmap(u_char *, int, off_t);
59 static void segv_handler(int);
60 #define MMAP_CHUNK (8*1024*1024)
61 
62 #define ROUNDPAGE(i) ((i) & ~pagemask)
63 
64 void
65 c_regular(int fd1, const char *file1, off_t skip1, off_t len1,
66     int fd2, const char *file2, off_t skip2, off_t len2)
67 {
68 	u_char ch, *p1, *p2, *m1, *m2, *e1, *e2;
69 	off_t byte, length, line;
70 	int dfound;
71 	off_t pagemask, off1, off2;
72 	size_t pagesize;
73 	struct sigaction act, oact;
74 
75 	if (skip1 > len1)
76 		eofmsg(file1);
77 	len1 -= skip1;
78 	if (skip2 > len2)
79 		eofmsg(file2);
80 	len2 -= skip2;
81 
82 	if (sflag && len1 != len2)
83 		exit(DIFF_EXIT);
84 
85 	sigemptyset(&act.sa_mask);
86 	act.sa_flags = SA_NODEFER;
87 	act.sa_handler = segv_handler;
88 	if (sigaction(SIGSEGV, &act, &oact))
89 		err(ERR_EXIT, "sigaction()");
90 
91 	pagesize = getpagesize();
92 	pagemask = (off_t)pagesize - 1;
93 	off1 = ROUNDPAGE(skip1);
94 	off2 = ROUNDPAGE(skip2);
95 
96 	length = MIN(len1, len2);
97 
98 	if ((m1 = remmap(NULL, fd1, off1)) == NULL) {
99 		c_special(fd1, file1, skip1, fd2, file2, skip2);
100 		return;
101 	}
102 
103 	if ((m2 = remmap(NULL, fd2, off2)) == NULL) {
104 		munmap(m1, MMAP_CHUNK);
105 		c_special(fd1, file1, skip1, fd2, file2, skip2);
106 		return;
107 	}
108 
109 	dfound = 0;
110 	e1 = m1 + MMAP_CHUNK;
111 	e2 = m2 + MMAP_CHUNK;
112 	p1 = m1 + (skip1 - off1);
113 	p2 = m2 + (skip2 - off2);
114 
115 	for (byte = line = 1; length--; ++byte) {
116 		if ((ch = *p1) != *p2) {
117 			if (xflag) {
118 				dfound = 1;
119 				(void)printf("%08llx %02x %02x\n",
120 				    (long long)byte - 1, ch, *p2);
121 			} else if (lflag) {
122 				dfound = 1;
123 				(void)printf("%6lld %3o %3o\n",
124 				    (long long)byte, ch, *p2);
125 			} else
126 				diffmsg(file1, file2, byte, line);
127 				/* NOTREACHED */
128 		}
129 		if (ch == '\n')
130 			++line;
131 		if (++p1 == e1) {
132 			off1 += MMAP_CHUNK;
133 			if ((p1 = m1 = remmap(m1, fd1, off1)) == NULL) {
134 				munmap(m2, MMAP_CHUNK);
135 				err(ERR_EXIT, "remmap %s", file1);
136 			}
137 			e1 = m1 + MMAP_CHUNK;
138 		}
139 		if (++p2 == e2) {
140 			off2 += MMAP_CHUNK;
141 			if ((p2 = m2 = remmap(m2, fd2, off2)) == NULL) {
142 				munmap(m1, MMAP_CHUNK);
143 				err(ERR_EXIT, "remmap %s", file2);
144 			}
145 			e2 = m2 + MMAP_CHUNK;
146 		}
147 	}
148 	munmap(m1, MMAP_CHUNK);
149 	munmap(m2, MMAP_CHUNK);
150 
151 	if (sigaction(SIGSEGV, &oact, NULL))
152 		err(ERR_EXIT, "sigaction()");
153 
154 	if (len1 != len2)
155 		eofmsg (len1 > len2 ? file2 : file1);
156 	if (dfound)
157 		exit(DIFF_EXIT);
158 }
159 
160 static u_char *
161 remmap(u_char *mem, int fd, off_t offset)
162 {
163 	if (mem != NULL)
164 		munmap(mem, MMAP_CHUNK);
165 	mem = mmap(NULL, MMAP_CHUNK, PROT_READ, MAP_SHARED, fd, offset);
166 	if (mem == MAP_FAILED)
167 		return (NULL);
168 	madvise(mem, MMAP_CHUNK, MADV_SEQUENTIAL);
169 	return (mem);
170 }
171 
172 static void
173 segv_handler(int sig) {
174 	static const char msg[] = "cmp: Input/output error (caught SIGSEGV)\n";
175 
176 	write(STDERR_FILENO, msg, sizeof(msg));
177 	_exit(EXIT_FAILURE);
178 }
179