xref: /dragonfly/usr.bin/cmp/regular.c (revision 3bafb5c1)
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  * $FreeBSD: src/usr.bin/cmp/regular.c,v 1.7.2.3 2001/11/21 10:47:54 dwmalone Exp $
34  * $DragonFly: src/usr.bin/cmp/regular.c,v 1.4 2003/11/03 19:31:28 eirikn Exp $
35  *
36  * @(#)regular.c	8.3 (Berkeley) 4/2/94
37  */
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 <stdlib.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <unistd.h>
49 
50 #include "extern.h"
51 
52 static u_char *remmap(u_char *, int, off_t);
53 #define MMAP_CHUNK (8*1024*1024)
54 
55 #define ROUNDPAGE(i) ((i) & ~pagemask)
56 
57 void
58 c_regular(int fd1, const char *file1, off_t skip1, off_t len1, int fd2,
59           const char *file2, off_t skip2, off_t len2)
60 {
61 	u_char ch, *p1, *p2, *m1, *m2, *e1, *e2;
62 	off_t byte, length, line;
63 	int dfound;
64 	off_t pagemask, off1, off2;
65 	size_t pagesize;
66 
67 	if (skip1 > len1)
68 		eofmsg(file1);
69 	len1 -= skip1;
70 	if (skip2 > len2)
71 		eofmsg(file2);
72 	len2 -= skip2;
73 
74 	if (sflag && len1 != len2)
75 		exit(DIFF_EXIT);
76 
77 	pagesize = getpagesize();
78 	pagemask = (off_t)pagesize - 1;
79 	off1 = ROUNDPAGE(skip1);
80 	off2 = ROUNDPAGE(skip2);
81 
82 	length = MIN(len1, len2);
83 
84 	if ((m1 = remmap(NULL, fd1, off1)) == NULL) {
85 		c_special(fd1, file1, skip1, fd2, file2, skip2);
86 		return;
87 	}
88 
89 	if ((m2 = remmap(NULL, fd2, off2)) == NULL) {
90 		munmap(m1, MMAP_CHUNK);
91 		c_special(fd1, file1, skip1, fd2, file2, skip2);
92 		return;
93 	}
94 
95 	dfound = 0;
96 	e1 = m1 + MMAP_CHUNK;
97 	e2 = m2 + MMAP_CHUNK;
98 	p1 = m1 + (skip1 - off1);
99 	p2 = m2 + (skip2 - off2);
100 
101 	for (byte = line = 1; length--; ++byte) {
102 		if ((ch = *p1) != *p2) {
103 			if (xflag) {
104 				dfound = 1;
105 				printf("%08jx %02x %02x\n",
106 				       (intmax_t)byte - 1, ch, *p2);
107 			} else if (lflag) {
108 				dfound = 1;
109 				printf("%6jd %3o %3o\n",
110 				       (intmax_t)byte, ch, *p2);
111 			} else {
112 				diffmsg(file1, file2, byte, line);
113 				/* NOTREACHED */
114 			}
115 		}
116 		if (ch == '\n')
117 			++line;
118 		if (++p1 == e1) {
119 			off1 += MMAP_CHUNK;
120 			if ((p1 = m1 = remmap(m1, fd1, off1)) == NULL) {
121 				munmap(m2, MMAP_CHUNK);
122 				err(ERR_EXIT, "remmap %s", file1);
123 			}
124 			e1 = m1 + MMAP_CHUNK;
125 		}
126 		if (++p2 == e2) {
127 			off2 += MMAP_CHUNK;
128 			if ((p2 = m2 = remmap(m2, fd2, off2)) == NULL) {
129 				munmap(m1, MMAP_CHUNK);
130 				err(ERR_EXIT, "remmap %s", file2);
131 			}
132 			e2 = m2 + MMAP_CHUNK;
133 		}
134 	}
135 	munmap(m1, MMAP_CHUNK);
136 	munmap(m2, MMAP_CHUNK);
137 
138 	if (len1 != len2)
139 		eofmsg (len1 > len2 ? file2 : file1);
140 	if (dfound)
141 		exit(DIFF_EXIT);
142 }
143 
144 static u_char *
145 remmap(u_char *mem, int fd, off_t offset)
146 {
147 	if (mem != NULL)
148 		munmap(mem, MMAP_CHUNK);
149 	mem = mmap(NULL, MMAP_CHUNK, PROT_READ, MAP_SHARED, fd, offset);
150 	if (mem == MAP_FAILED)
151 		return (NULL);
152 	madvise(mem, MMAP_CHUNK, MADV_SEQUENTIAL);
153 	return (mem);
154 }
155