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