xref: /csrg-svn/usr.bin/cmp/special.c (revision 66652)
151414Sbostic /*-
2*66652Spendry  * Copyright (c) 1991, 1993, 1994
361944Sbostic  *	The Regents of the University of California.  All rights reserved.
451414Sbostic  *
551414Sbostic  * %sccs.include.redist.c%
651414Sbostic  */
751414Sbostic 
851414Sbostic #ifndef lint
9*66652Spendry static char sccsid[] = "@(#)special.c	8.3 (Berkeley) 04/02/94";
1051414Sbostic #endif /* not lint */
1151414Sbostic 
1251414Sbostic #include <sys/types.h>
1366588Spendry 
1466588Spendry #include <err.h>
1551414Sbostic #include <stdlib.h>
1651414Sbostic #include <stdio.h>
1751414Sbostic #include <string.h>
1866588Spendry 
1951414Sbostic #include "extern.h"
2051414Sbostic 
2151414Sbostic void
c_special(fd1,file1,skip1,fd2,file2,skip2)2251414Sbostic c_special(fd1, file1, skip1, fd2, file2, skip2)
2351414Sbostic 	int fd1, fd2;
2451414Sbostic 	char *file1, *file2;
2566588Spendry 	off_t skip1, skip2;
2651414Sbostic {
2766588Spendry 	int ch1, ch2;
2866588Spendry 	off_t byte, line;
2951414Sbostic 	FILE *fp1, *fp2;
3051414Sbostic 	int dfound;
3151414Sbostic 
3251414Sbostic 	if ((fp1 = fdopen(fd1, "r")) == NULL)
3366588Spendry 		err(ERR_EXIT, "%s", file1);
3451414Sbostic 	if ((fp2 = fdopen(fd2, "r")) == NULL)
3566588Spendry 		err(ERR_EXIT, "%s", file2);
3651414Sbostic 
3751414Sbostic 	while (skip1--)
3851414Sbostic 		if (getc(fp1) == EOF)
3951414Sbostic 			goto eof;
4051414Sbostic 	while (skip2--)
4151414Sbostic 		if (getc(fp2) == EOF)
4251414Sbostic 			goto eof;
4351414Sbostic 
4451414Sbostic 	dfound = 0;
4551414Sbostic 	for (byte = line = 1;; ++byte) {
4651414Sbostic 		ch1 = getc(fp1);
4751414Sbostic 		ch2 = getc(fp2);
4851414Sbostic 		if (ch1 == EOF || ch2 == EOF)
4951414Sbostic 			break;
5051414Sbostic 		if (ch1 != ch2)
5151414Sbostic 			if (lflag) {
5251414Sbostic 				dfound = 1;
5354187Sbostic 				(void)printf("%6qd %3o %3o\n", byte, ch1, ch2);
5451414Sbostic 			} else
5551414Sbostic 				diffmsg(file1, file2, byte, line);
5651414Sbostic 				/* NOTREACHED */
5751414Sbostic 		if (ch1 == '\n')
5851414Sbostic 			++line;
5951414Sbostic 	}
6051414Sbostic 
6151414Sbostic eof:	if (ferror(fp1))
6266588Spendry 		err(ERR_EXIT, "%s", file1);
6351414Sbostic 	if (ferror(fp2))
6466588Spendry 		err(ERR_EXIT, "%s", file2);
6551414Sbostic 	if (feof(fp1)) {
6651414Sbostic 		if (!feof(fp2))
6751414Sbostic 			eofmsg(file1);
6851414Sbostic 	} else
6951414Sbostic 		if (feof(fp2))
7051414Sbostic 			eofmsg(file2);
7151414Sbostic 	if (dfound)
7266588Spendry 		exit(DIFF_EXIT);
7351414Sbostic }
74