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