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