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