xref: /csrg-svn/usr.bin/cmp/regular.c (revision 54364)
151413Sbostic /*-
251413Sbostic  * Copyright (c) 1991 The Regents of the University of California.
351413Sbostic  * All rights reserved.
451413Sbostic  *
551413Sbostic  * %sccs.include.redist.c%
651413Sbostic  */
751413Sbostic 
851413Sbostic #ifndef lint
9*54364Sbostic static char sccsid[] = "@(#)regular.c	5.5 (Berkeley) 06/24/92";
1051413Sbostic #endif /* not lint */
1151413Sbostic 
1251413Sbostic #include <sys/param.h>
1351413Sbostic #include <sys/mman.h>
1451413Sbostic #include <sys/stat.h>
15*54364Sbostic 
16*54364Sbostic #include <limits.h>
1751413Sbostic #include <errno.h>
1851413Sbostic #include <stdlib.h>
1951413Sbostic #include <stdio.h>
2051413Sbostic #include <string.h>
2151413Sbostic #include "extern.h"
2251413Sbostic 
2351413Sbostic void
2451413Sbostic c_regular(fd1, file1, skip1, len1, fd2, file2, skip2, len2)
2551413Sbostic 	int fd1, fd2;
2651413Sbostic 	char *file1, *file2;
2751413Sbostic 	off_t skip1, len1, skip2, len2;
2851413Sbostic {
2951413Sbostic 	register u_char ch, *p1, *p2;
3051413Sbostic 	register off_t byte, length, line;
3151413Sbostic 	int dfound;
3251413Sbostic 
3351413Sbostic 	if (sflag && len1 != len2)
3451413Sbostic 		exit(1);
3551413Sbostic 
3651733Sbostic 	if (skip1 > len1)
3751413Sbostic 		eofmsg(file1);
3851413Sbostic 	len1 -= skip1;
3951733Sbostic 	if (skip2 > len2)
4051413Sbostic 		eofmsg(file2);
4151413Sbostic 	len2 -= skip2;
4251413Sbostic 
4351413Sbostic 	length = MIN(len1, len2);
44*54364Sbostic 	if (length > SIZE_T_MAX)
45*54364Sbostic 		return (c_special(fd1, file1, skip1, fd2, file2, skip2));
46*54364Sbostic 
4751413Sbostic 	if ((p1 = (u_char *)mmap(NULL,
48*54364Sbostic 	    (size_t)length, PROT_READ, 0, fd1, skip1)) == (u_char *)-1)
4951413Sbostic 		err("%s: %s", file1, strerror(errno));
5051413Sbostic 	if ((p2 = (u_char *)mmap(NULL,
51*54364Sbostic 	    (size_t)length, PROT_READ, 0, fd2, skip2)) == (u_char *)-1)
5251413Sbostic 		err("%s: %s", file2, strerror(errno));
5351413Sbostic 
5451413Sbostic 	dfound = 0;
5551413Sbostic 	for (byte = line = 1; length--; ++p1, ++p2, ++byte) {
5651413Sbostic 		if ((ch = *p1) != *p2)
5751413Sbostic 			if (lflag) {
5851413Sbostic 				dfound = 1;
5954187Sbostic 				(void)printf("%6qd %3o %3o\n", byte, ch, *p2);
6051413Sbostic 			} else
6151413Sbostic 				diffmsg(file1, file2, byte, line);
6251413Sbostic 				/* NOTREACHED */
6351413Sbostic 		if (ch == '\n')
6451413Sbostic 			++line;
6551413Sbostic 	}
6651413Sbostic 
6751413Sbostic 	if (len1 != len2)
6851413Sbostic 		eofmsg (len1 > len2 ? file2 : file1);
6951413Sbostic 	if (dfound)
7051413Sbostic 		exit(1);
7151413Sbostic }
72