xref: /original-bsd/usr.bin/cmp/regular.c (revision 5e3ce5bf)
1d023313eSbostic /*-
2*5e3ce5bfSpendry  * Copyright (c) 1991, 1993, 1994
3fe0843a8Sbostic  *	The Regents of the University of California.  All rights reserved.
4d023313eSbostic  *
5d023313eSbostic  * %sccs.include.redist.c%
6d023313eSbostic  */
7d023313eSbostic 
8d023313eSbostic #ifndef lint
9*5e3ce5bfSpendry static char sccsid[] = "@(#)regular.c	8.3 (Berkeley) 04/02/94";
10d023313eSbostic #endif /* not lint */
11d023313eSbostic 
12d023313eSbostic #include <sys/param.h>
13d023313eSbostic #include <sys/mman.h>
14d023313eSbostic #include <sys/stat.h>
156e9394adSbostic 
163ec1396fSpendry #include <err.h>
176e9394adSbostic #include <limits.h>
18d023313eSbostic #include <stdlib.h>
19d023313eSbostic #include <stdio.h>
20d023313eSbostic #include <string.h>
213ec1396fSpendry 
22d023313eSbostic #include "extern.h"
23d023313eSbostic 
24d023313eSbostic void
c_regular(fd1,file1,skip1,len1,fd2,file2,skip2,len2)25d023313eSbostic c_regular(fd1, file1, skip1, len1, fd2, file2, skip2, len2)
26d023313eSbostic 	int fd1, fd2;
27d023313eSbostic 	char *file1, *file2;
28d023313eSbostic 	off_t skip1, len1, skip2, len2;
29d023313eSbostic {
303ec1396fSpendry 	u_char ch, *p1, *p2;
313ec1396fSpendry 	off_t byte, length, line;
32d023313eSbostic 	int dfound;
33d023313eSbostic 
34d023313eSbostic 	if (sflag && len1 != len2)
35d023313eSbostic 		exit(1);
36d023313eSbostic 
373994124cSbostic 	if (skip1 > len1)
38d023313eSbostic 		eofmsg(file1);
39d023313eSbostic 	len1 -= skip1;
403994124cSbostic 	if (skip2 > len2)
41d023313eSbostic 		eofmsg(file2);
42d023313eSbostic 	len2 -= skip2;
43d023313eSbostic 
44d023313eSbostic 	length = MIN(len1, len2);
456e9394adSbostic 	if (length > SIZE_T_MAX)
466e9394adSbostic 		return (c_special(fd1, file1, skip1, fd2, file2, skip2));
476e9394adSbostic 
48d023313eSbostic 	if ((p1 = (u_char *)mmap(NULL,
496e9394adSbostic 	    (size_t)length, PROT_READ, 0, fd1, skip1)) == (u_char *)-1)
503ec1396fSpendry 		err(ERR_EXIT, "%s", file1);
51d023313eSbostic 	if ((p2 = (u_char *)mmap(NULL,
526e9394adSbostic 	    (size_t)length, PROT_READ, 0, fd2, skip2)) == (u_char *)-1)
533ec1396fSpendry 		err(ERR_EXIT, "%s", file2);
54d023313eSbostic 
55d023313eSbostic 	dfound = 0;
56d023313eSbostic 	for (byte = line = 1; length--; ++p1, ++p2, ++byte) {
57d023313eSbostic 		if ((ch = *p1) != *p2)
58d023313eSbostic 			if (lflag) {
59d023313eSbostic 				dfound = 1;
6094dd32dfSbostic 				(void)printf("%6qd %3o %3o\n", byte, ch, *p2);
61d023313eSbostic 			} else
62d023313eSbostic 				diffmsg(file1, file2, byte, line);
63d023313eSbostic 				/* NOTREACHED */
64d023313eSbostic 		if (ch == '\n')
65d023313eSbostic 			++line;
66d023313eSbostic 	}
67d023313eSbostic 
68d023313eSbostic 	if (len1 != len2)
69d023313eSbostic 		eofmsg (len1 > len2 ? file2 : file1);
70d023313eSbostic 	if (dfound)
713ec1396fSpendry 		exit(DIFF_EXIT);
72d023313eSbostic }
73