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