regular.c revision 200420
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
36static char sccsid[] = "@(#)regular.c	8.3 (Berkeley) 4/2/94";
37#endif
38#endif
39
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: head/usr.bin/cmp/regular.c 200420 2009-12-11 23:35:38Z delphij $");
42
43#include <sys/param.h>
44#include <sys/mman.h>
45#include <sys/stat.h>
46
47#include <err.h>
48#include <limits.h>
49#include <signal.h>
50#include <stdlib.h>
51#include <stdio.h>
52#include <unistd.h>
53
54#include "extern.h"
55
56static u_char *remmap(u_char *, int, off_t);
57static void segv_handler(int);
58#define MMAP_CHUNK (8*1024*1024)
59
60#define ROUNDPAGE(i) ((i) & ~pagemask)
61
62void
63c_regular(int fd1, const char *file1, off_t skip1, off_t len1,
64    int fd2, const char *file2, off_t skip2, off_t len2)
65{
66	u_char ch, *p1, *p2, *m1, *m2, *e1, *e2;
67	off_t byte, length, line;
68	int dfound;
69	off_t pagemask, off1, off2;
70	size_t pagesize;
71	struct sigaction act, oact;
72
73	if (skip1 > len1)
74		eofmsg(file1);
75	len1 -= skip1;
76	if (skip2 > len2)
77		eofmsg(file2);
78	len2 -= skip2;
79
80	if (sflag && len1 != len2)
81		exit(DIFF_EXIT);
82
83	sigemptyset(&act.sa_mask);
84	act.sa_flags = SA_NODEFER;
85	act.sa_handler = segv_handler;
86	if (sigaction(SIGSEGV, &act, &oact))
87		err(ERR_EXIT, "sigaction()");
88
89	pagesize = getpagesize();
90	pagemask = (off_t)pagesize - 1;
91	off1 = ROUNDPAGE(skip1);
92	off2 = ROUNDPAGE(skip2);
93
94	length = MIN(len1, len2);
95
96	if ((m1 = remmap(NULL, fd1, off1)) == NULL) {
97		c_special(fd1, file1, skip1, fd2, file2, skip2);
98		return;
99	}
100
101	if ((m2 = remmap(NULL, fd2, off2)) == NULL) {
102		munmap(m1, MMAP_CHUNK);
103		c_special(fd1, file1, skip1, fd2, file2, skip2);
104		return;
105	}
106
107	dfound = 0;
108	e1 = m1 + MMAP_CHUNK;
109	e2 = m2 + MMAP_CHUNK;
110	p1 = m1 + (skip1 - off1);
111	p2 = m2 + (skip2 - off2);
112
113	for (byte = line = 1; length--; ++byte) {
114		if ((ch = *p1) != *p2) {
115			if (xflag) {
116				dfound = 1;
117				(void)printf("%08llx %02x %02x\n",
118				    (long long)byte - 1, ch, *p2);
119			} else if (lflag) {
120				dfound = 1;
121				(void)printf("%6lld %3o %3o\n",
122				    (long long)byte, ch, *p2);
123			} else
124				diffmsg(file1, file2, byte, line);
125				/* NOTREACHED */
126		}
127		if (ch == '\n')
128			++line;
129		if (++p1 == e1) {
130			off1 += MMAP_CHUNK;
131			if ((p1 = m1 = remmap(m1, fd1, off1)) == NULL) {
132				munmap(m2, MMAP_CHUNK);
133				err(ERR_EXIT, "remmap %s", file1);
134			}
135			e1 = m1 + MMAP_CHUNK;
136		}
137		if (++p2 == e2) {
138			off2 += MMAP_CHUNK;
139			if ((p2 = m2 = remmap(m2, fd2, off2)) == NULL) {
140				munmap(m1, MMAP_CHUNK);
141				err(ERR_EXIT, "remmap %s", file2);
142			}
143			e2 = m2 + MMAP_CHUNK;
144		}
145	}
146	munmap(m1, MMAP_CHUNK);
147	munmap(m2, MMAP_CHUNK);
148
149	if (sigaction(SIGSEGV, &oact, NULL))
150		err(ERR_EXIT, "sigaction()");
151
152	if (len1 != len2)
153		eofmsg (len1 > len2 ? file2 : file1);
154	if (dfound)
155		exit(DIFF_EXIT);
156}
157
158static u_char *
159remmap(u_char *mem, int fd, off_t offset)
160{
161	if (mem != NULL)
162		munmap(mem, MMAP_CHUNK);
163	mem = mmap(NULL, MMAP_CHUNK, PROT_READ, MAP_SHARED, fd, offset);
164	if (mem == MAP_FAILED)
165		return (NULL);
166	madvise(mem, MMAP_CHUNK, MADV_SEQUENTIAL);
167	return (mem);
168}
169
170static void
171segv_handler(int sig __unused) {
172	static const char msg[] = "cmp: Input/output error (caught SIGSEGV)\n";
173
174	write(STDERR_FILENO, msg, sizeof(msg));
175	_exit(EXIT_FAILURE);
176}
177