xref: /original-bsd/usr.bin/cmp/special.c (revision 3413c235)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)special.c	8.2 (Berkeley) 04/01/94";
10 #endif /* not lint */
11 
12 #include <sys/types.h>
13 
14 #include <err.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 
19 #include "extern.h"
20 
21 void
22 c_special(fd1, file1, skip1, fd2, file2, skip2)
23 	int fd1, fd2;
24 	char *file1, *file2;
25 	off_t skip1, skip2;
26 {
27 	int ch1, ch2;
28 	off_t byte, line;
29 	FILE *fp1, *fp2;
30 	int dfound;
31 
32 	if ((fp1 = fdopen(fd1, "r")) == NULL)
33 		err(ERR_EXIT, "%s", file1);
34 	if ((fp2 = fdopen(fd2, "r")) == NULL)
35 		err(ERR_EXIT, "%s", file2);
36 
37 	while (skip1--)
38 		if (getc(fp1) == EOF)
39 			goto eof;
40 	while (skip2--)
41 		if (getc(fp2) == EOF)
42 			goto eof;
43 
44 	dfound = 0;
45 	for (byte = line = 1;; ++byte) {
46 		ch1 = getc(fp1);
47 		ch2 = getc(fp2);
48 		if (ch1 == EOF || ch2 == EOF)
49 			break;
50 		if (ch1 != ch2)
51 			if (lflag) {
52 				dfound = 1;
53 				(void)printf("%6qd %3o %3o\n", byte, ch1, ch2);
54 			} else
55 				diffmsg(file1, file2, byte, line);
56 				/* NOTREACHED */
57 		if (ch1 == '\n')
58 			++line;
59 	}
60 
61 eof:	if (ferror(fp1))
62 		err(ERR_EXIT, "%s", file1);
63 	if (ferror(fp2))
64 		err(ERR_EXIT, "%s", file2);
65 	if (feof(fp1)) {
66 		if (!feof(fp2))
67 			eofmsg(file1);
68 	} else
69 		if (feof(fp2))
70 			eofmsg(file2);
71 	if (dfound)
72 		exit(DIFF_EXIT);
73 }
74