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