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