xref: /freebsd/usr.bin/cmp/special.c (revision 3416500aef140042c64bc149cb1ec6620483bc44)
1 /*-
2  * Copyright (c) 1991, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #if 0
31 #ifndef lint
32 static char sccsid[] = "@(#)special.c	8.3 (Berkeley) 4/2/94";
33 #endif
34 #endif
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/types.h>
40 
41 #include <err.h>
42 #include <stdlib.h>
43 #include <stdio.h>
44 
45 #include "extern.h"
46 
47 void
48 c_special(int fd1, const char *file1, off_t skip1,
49     int fd2, const char *file2, off_t skip2)
50 {
51 	int ch1, ch2;
52 	off_t byte, line;
53 	FILE *fp1, *fp2;
54 	int dfound;
55 
56 	if ((fp1 = fdopen(fd1, "r")) == NULL)
57 		err(ERR_EXIT, "%s", file1);
58 	if ((fp2 = fdopen(fd2, "r")) == NULL)
59 		err(ERR_EXIT, "%s", file2);
60 
61 	dfound = 0;
62 	while (skip1--)
63 		if (getc(fp1) == EOF)
64 			goto eof;
65 	while (skip2--)
66 		if (getc(fp2) == EOF)
67 			goto eof;
68 
69 	for (byte = line = 1;; ++byte) {
70 		ch1 = getc(fp1);
71 		ch2 = getc(fp2);
72 		if (ch1 == EOF || ch2 == EOF)
73 			break;
74 		if (ch1 != ch2) {
75 			if (xflag) {
76 				dfound = 1;
77 				(void)printf("%08llx %02x %02x\n",
78 				    (long long)byte - 1, ch1, ch2);
79 			} else if (lflag) {
80 				dfound = 1;
81 				(void)printf("%6lld %3o %3o\n",
82 				    (long long)byte, ch1, ch2);
83 			} else {
84 				diffmsg(file1, file2, byte, line);
85 				/* NOTREACHED */
86 			}
87 		}
88 		if (ch1 == '\n')
89 			++line;
90 	}
91 
92 eof:	if (ferror(fp1))
93 		err(ERR_EXIT, "%s", file1);
94 	if (ferror(fp2))
95 		err(ERR_EXIT, "%s", file2);
96 	if (feof(fp1)) {
97 		if (!feof(fp2))
98 			eofmsg(file1);
99 	} else
100 		if (feof(fp2))
101 			eofmsg(file2);
102 	fclose(fp2);
103 	fclose(fp1);
104 	if (dfound)
105 		exit(DIFF_EXIT);
106 }
107