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