xref: /csrg-svn/usr.bin/cmp/regular.c (revision 66652)
151413Sbostic /*-
2*66652Spendry  * Copyright (c) 1991, 1993, 1994
361944Sbostic  *	The Regents of the University of California.  All rights reserved.
451413Sbostic  *
551413Sbostic  * %sccs.include.redist.c%
651413Sbostic  */
751413Sbostic 
851413Sbostic #ifndef lint
9*66652Spendry static char sccsid[] = "@(#)regular.c	8.3 (Berkeley) 04/02/94";
1051413Sbostic #endif /* not lint */
1151413Sbostic 
1251413Sbostic #include <sys/param.h>
1351413Sbostic #include <sys/mman.h>
1451413Sbostic #include <sys/stat.h>
1554364Sbostic 
1666589Spendry #include <err.h>
1754364Sbostic #include <limits.h>
1851413Sbostic #include <stdlib.h>
1951413Sbostic #include <stdio.h>
2051413Sbostic #include <string.h>
2166589Spendry 
2251413Sbostic #include "extern.h"
2351413Sbostic 
2451413Sbostic void
c_regular(fd1,file1,skip1,len1,fd2,file2,skip2,len2)2551413Sbostic c_regular(fd1, file1, skip1, len1, fd2, file2, skip2, len2)
2651413Sbostic 	int fd1, fd2;
2751413Sbostic 	char *file1, *file2;
2851413Sbostic 	off_t skip1, len1, skip2, len2;
2951413Sbostic {
3066589Spendry 	u_char ch, *p1, *p2;
3166589Spendry 	off_t byte, length, line;
3251413Sbostic 	int dfound;
3351413Sbostic 
3451413Sbostic 	if (sflag && len1 != len2)
3551413Sbostic 		exit(1);
3651413Sbostic 
3751733Sbostic 	if (skip1 > len1)
3851413Sbostic 		eofmsg(file1);
3951413Sbostic 	len1 -= skip1;
4051733Sbostic 	if (skip2 > len2)
4151413Sbostic 		eofmsg(file2);
4251413Sbostic 	len2 -= skip2;
4351413Sbostic 
4451413Sbostic 	length = MIN(len1, len2);
4554364Sbostic 	if (length > SIZE_T_MAX)
4654364Sbostic 		return (c_special(fd1, file1, skip1, fd2, file2, skip2));
4754364Sbostic 
4851413Sbostic 	if ((p1 = (u_char *)mmap(NULL,
4954364Sbostic 	    (size_t)length, PROT_READ, 0, fd1, skip1)) == (u_char *)-1)
5066589Spendry 		err(ERR_EXIT, "%s", file1);
5151413Sbostic 	if ((p2 = (u_char *)mmap(NULL,
5254364Sbostic 	    (size_t)length, PROT_READ, 0, fd2, skip2)) == (u_char *)-1)
5366589Spendry 		err(ERR_EXIT, "%s", file2);
5451413Sbostic 
5551413Sbostic 	dfound = 0;
5651413Sbostic 	for (byte = line = 1; length--; ++p1, ++p2, ++byte) {
5751413Sbostic 		if ((ch = *p1) != *p2)
5851413Sbostic 			if (lflag) {
5951413Sbostic 				dfound = 1;
6054187Sbostic 				(void)printf("%6qd %3o %3o\n", byte, ch, *p2);
6151413Sbostic 			} else
6251413Sbostic 				diffmsg(file1, file2, byte, line);
6351413Sbostic 				/* NOTREACHED */
6451413Sbostic 		if (ch == '\n')
6551413Sbostic 			++line;
6651413Sbostic 	}
6751413Sbostic 
6851413Sbostic 	if (len1 != len2)
6951413Sbostic 		eofmsg (len1 > len2 ? file2 : file1);
7051413Sbostic 	if (dfound)
7166589Spendry 		exit(DIFF_EXIT);
7251413Sbostic }
73