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