xref: /csrg-svn/usr.bin/cmp/regular.c (revision 51413)
1*51413Sbostic /*-
2*51413Sbostic  * Copyright (c) 1991 The Regents of the University of California.
3*51413Sbostic  * All rights reserved.
4*51413Sbostic  *
5*51413Sbostic  * %sccs.include.redist.c%
6*51413Sbostic  */
7*51413Sbostic 
8*51413Sbostic #ifndef lint
9*51413Sbostic static char sccsid[] = "@(#)regular.c	5.1 (Berkeley) 10/27/91";
10*51413Sbostic #endif /* not lint */
11*51413Sbostic 
12*51413Sbostic #include <sys/param.h>
13*51413Sbostic #include <sys/mman.h>
14*51413Sbostic #include <sys/stat.h>
15*51413Sbostic #include <errno.h>
16*51413Sbostic #include <stdlib.h>
17*51413Sbostic #include <stdio.h>
18*51413Sbostic #include <string.h>
19*51413Sbostic #include "extern.h"
20*51413Sbostic 
21*51413Sbostic void
22*51413Sbostic c_regular(fd1, file1, skip1, len1, fd2, file2, skip2, len2)
23*51413Sbostic 	int fd1, fd2;
24*51413Sbostic 	char *file1, *file2;
25*51413Sbostic 	off_t skip1, len1, skip2, len2;
26*51413Sbostic {
27*51413Sbostic 	register u_char ch, *p1, *p2;
28*51413Sbostic 	register off_t byte, length, line;
29*51413Sbostic 	int dfound;
30*51413Sbostic 
31*51413Sbostic 	if (sflag && len1 != len2)
32*51413Sbostic 		exit(1);
33*51413Sbostic 
34*51413Sbostic 	if (skip1 >= len1)
35*51413Sbostic 		eofmsg(file1);
36*51413Sbostic 	len1 -= skip1;
37*51413Sbostic 	if (skip2 >= len2)
38*51413Sbostic 		eofmsg(file2);
39*51413Sbostic 	len2 -= skip2;
40*51413Sbostic 
41*51413Sbostic 	length = MIN(len1, len2);
42*51413Sbostic 	if ((p1 = (u_char *)mmap(NULL,
43*51413Sbostic 	    length, PROT_READ, MAP_FILE, fd1, skip1)) == (u_char *)-1)
44*51413Sbostic 		err("%s: %s", file1, strerror(errno));
45*51413Sbostic 	if ((p2 = (u_char *)mmap(NULL,
46*51413Sbostic 	    length, PROT_READ, MAP_FILE, fd2, skip2)) == (u_char *)-1)
47*51413Sbostic 		err("%s: %s", file2, strerror(errno));
48*51413Sbostic 
49*51413Sbostic 	dfound = 0;
50*51413Sbostic 	for (byte = line = 1; length--; ++p1, ++p2, ++byte) {
51*51413Sbostic 		if ((ch = *p1) != *p2)
52*51413Sbostic 			if (lflag) {
53*51413Sbostic 				dfound = 1;
54*51413Sbostic 				(void)printf("%6ld %3o %3o\n", byte, ch, *p2);
55*51413Sbostic 			} else
56*51413Sbostic 				diffmsg(file1, file2, byte, line);
57*51413Sbostic 				/* NOTREACHED */
58*51413Sbostic 		if (ch == '\n')
59*51413Sbostic 			++line;
60*51413Sbostic 	}
61*51413Sbostic 
62*51413Sbostic 	if (len1 != len2)
63*51413Sbostic 		eofmsg (len1 > len2 ? file2 : file1);
64*51413Sbostic 	if (dfound)
65*51413Sbostic 		exit(1);
66*51413Sbostic }
67