xref: /csrg-svn/usr.bin/cmp/special.c (revision 61944)
151414Sbostic /*-
2*61944Sbostic  * Copyright (c) 1991, 1993
3*61944Sbostic  *	The Regents of the University of California.  All rights reserved.
451414Sbostic  *
551414Sbostic  * %sccs.include.redist.c%
651414Sbostic  */
751414Sbostic 
851414Sbostic #ifndef lint
9*61944Sbostic static char sccsid[] = "@(#)special.c	8.1 (Berkeley) 06/06/93";
1051414Sbostic #endif /* not lint */
1151414Sbostic 
1251414Sbostic #include <sys/types.h>
1351414Sbostic #include <errno.h>
1451414Sbostic #include <stdlib.h>
1551414Sbostic #include <stdio.h>
1651414Sbostic #include <string.h>
1751414Sbostic #include "extern.h"
1851414Sbostic 
1951414Sbostic void
2051414Sbostic c_special(fd1, file1, skip1, fd2, file2, skip2)
2151414Sbostic 	int fd1, fd2;
2251414Sbostic 	char *file1, *file2;
2351414Sbostic 	register off_t skip1, skip2;
2451414Sbostic {
2551414Sbostic 	register int ch1, ch2;
2651414Sbostic 	register off_t byte, line;
2751414Sbostic 	FILE *fp1, *fp2;
2851414Sbostic 	int dfound;
2951414Sbostic 
3051414Sbostic 	if ((fp1 = fdopen(fd1, "r")) == NULL)
3151414Sbostic 		err("%s: %s", file1, strerror(errno));
3251414Sbostic 	if ((fp2 = fdopen(fd2, "r")) == NULL)
3351414Sbostic 		err("%s: %s", file1, strerror(errno));
3451414Sbostic 
3551414Sbostic 	while (skip1--)
3651414Sbostic 		if (getc(fp1) == EOF)
3751414Sbostic 			goto eof;
3851414Sbostic 	while (skip2--)
3951414Sbostic 		if (getc(fp2) == EOF)
4051414Sbostic 			goto eof;
4151414Sbostic 
4251414Sbostic 	dfound = 0;
4351414Sbostic 	for (byte = line = 1;; ++byte) {
4451414Sbostic 		ch1 = getc(fp1);
4551414Sbostic 		ch2 = getc(fp2);
4651414Sbostic 		if (ch1 == EOF || ch2 == EOF)
4751414Sbostic 			break;
4851414Sbostic 		if (ch1 != ch2)
4951414Sbostic 			if (lflag) {
5051414Sbostic 				dfound = 1;
5154187Sbostic 				(void)printf("%6qd %3o %3o\n", byte, ch1, ch2);
5251414Sbostic 			} else
5351414Sbostic 				diffmsg(file1, file2, byte, line);
5451414Sbostic 				/* NOTREACHED */
5551414Sbostic 		if (ch1 == '\n')
5651414Sbostic 			++line;
5751414Sbostic 	}
5851414Sbostic 
5951414Sbostic eof:	if (ferror(fp1))
6051414Sbostic 		err("%s: %s", file1, strerror(errno));
6151414Sbostic 	if (ferror(fp2))
6251414Sbostic 		err("%s: %s", file2, strerror(errno));
6351414Sbostic 	if (feof(fp1)) {
6451414Sbostic 		if (!feof(fp2))
6551414Sbostic 			eofmsg(file1);
6651414Sbostic 	} else
6751414Sbostic 		if (feof(fp2))
6851414Sbostic 			eofmsg(file2);
6951414Sbostic 	if (dfound)
7051414Sbostic 		exit(1);
7151414Sbostic }
72