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