xref: /freebsd/usr.bin/cmp/regular.c (revision b601c69bdbe8755d26570261d7fd4c02ee4eff74)
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$
34  *
35  */
36 
37 #ifndef lint
38 static const char sccsid[] = "@(#)regular.c	8.3 (Berkeley) 4/2/94";
39 #endif /* not lint */
40 
41 #include <sys/param.h>
42 #include <sys/mman.h>
43 #include <sys/stat.h>
44 
45 #include <err.h>
46 #include <limits.h>
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <unistd.h>
51 
52 #include "extern.h"
53 
54 #define ROUNDPAGE(i) ((i) & ~pagemask)
55 
56 void
57 c_regular(fd1, file1, skip1, len1, fd2, file2, skip2, len2)
58 	int fd1, fd2;
59 	char *file1, *file2;
60 	off_t skip1, len1, skip2, len2;
61 {
62 	u_char ch, *p1, *p2;
63 	off_t byte, length, line;
64 	int dfound;
65 	off_t pagemask, off1, off2;
66 	size_t pagesize;
67 
68 	if (skip1 > len1)
69 		eofmsg(file1);
70 	len1 -= skip1;
71 	if (skip2 > len2)
72 		eofmsg(file2);
73 	len2 -= skip2;
74 
75 	if (sflag && len1 != len2)
76 		exit(DIFF_EXIT);
77 
78 	pagesize = getpagesize();
79 	pagemask = (off_t)pagesize - 1;
80 	off1 = ROUNDPAGE(skip1);
81 	off2 = ROUNDPAGE(skip2);
82 
83 	length = MIN(len1, len2);
84 	if (length > SIZE_T_MAX)
85 		return (c_special(fd1, file1, skip1, fd2, file2, skip2));
86 
87 	if ((p1 = (u_char *)mmap(NULL, (size_t)len1 + skip1 % pagesize,
88 	    PROT_READ, MAP_SHARED, fd1, off1)) == (u_char *)MAP_FAILED)
89 		err(ERR_EXIT, "%s", file1);
90 
91 	madvise(p1, len1 + skip1 % pagesize, MADV_SEQUENTIAL);
92 	if ((p2 = (u_char *)mmap(NULL, (size_t)len2 + skip2 % pagesize,
93 	    PROT_READ, MAP_SHARED, fd2, off2)) == (u_char *)MAP_FAILED)
94 		err(ERR_EXIT, "%s", file2);
95 	madvise(p2, len2 + skip2 % pagesize, MADV_SEQUENTIAL);
96 
97 	dfound = 0;
98 	p1 += skip1 - off1;
99 	p2 += skip2 - off2;
100 	for (byte = line = 1; length--; ++p1, ++p2, ++byte) {
101 		if ((ch = *p1) != *p2) {
102 			if (xflag) {
103 				dfound = 1;
104 				(void)printf("%08qx %02x %02x\n", byte - 1, ch, *p2);
105 			} else if (lflag) {
106 				dfound = 1;
107 				(void)printf("%6qd %3o %3o\n", byte, ch, *p2);
108 			} else
109 				diffmsg(file1, file2, byte, line);
110 				/* NOTREACHED */
111 		}
112 		if (ch == '\n')
113 			++line;
114 	}
115 
116 	if (len1 != len2)
117 		eofmsg (len1 > len2 ? file2 : file1);
118 	if (dfound)
119 		exit(DIFF_EXIT);
120 }
121