/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1991, 1993, 19944* The Regents of the University of California. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the University nor the names of its contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031#include <sys/types.h>3233#include <capsicum_helpers.h>34#include <err.h>35#include <stdbool.h>36#include <stdlib.h>37#include <stdio.h>3839#include "extern.h"4041int42c_special(int fd1, const char *file1, off_t skip1,43int fd2, const char *file2, off_t skip2, off_t limit)44{45int ch1, ch2;46off_t byte, line;47FILE *fp1, *fp2;48int dfound;4950if (caph_limit_stream(fd1, CAPH_READ) < 0)51err(ERR_EXIT, "caph_limit_stream(%s)", file1);52if (caph_limit_stream(fd2, CAPH_READ) < 0)53err(ERR_EXIT, "caph_limit_stream(%s)", file2);54if (caph_enter() < 0)55err(ERR_EXIT, "unable to enter capability mode");5657if ((fp1 = fdopen(fd1, "r")) == NULL)58err(ERR_EXIT, "%s", file1);59(void)setvbuf(fp1, NULL, _IOFBF, 65536);60if ((fp2 = fdopen(fd2, "r")) == NULL)61err(ERR_EXIT, "%s", file2);62(void)setvbuf(fp2, NULL, _IOFBF, 65536);6364dfound = 0;65while (skip1--)66if (getc(fp1) == EOF)67goto eof;68while (skip2--)69if (getc(fp2) == EOF)70goto eof;7172for (byte = line = 1; limit == 0 || byte <= limit; ++byte) {73#ifdef SIGINFO74if (info) {75(void)fprintf(stderr, "%s %s char %zu line %zu\n",76file1, file2, (size_t)byte, (size_t)line);77info = 0;78}79#endif80ch1 = getc(fp1);81ch2 = getc(fp2);82if (ch1 == EOF || ch2 == EOF)83break;84if (ch1 != ch2) {85if (xflag) {86dfound = 1;87(void)printf("%08llx %02x %02x\n",88(long long)byte - 1, ch1, ch2);89} else if (lflag) {90dfound = 1;91if (bflag)92(void)printf("%6lld %3o %c %3o %c\n",93(long long)byte, ch1, ch1, ch2,94ch2);95else96(void)printf("%6lld %3o %3o\n",97(long long)byte, ch1, ch2);98} else {99diffmsg(file1, file2, byte, line, ch1, ch2);100return (DIFF_EXIT);101}102}103if (ch1 == '\n')104++line;105}106107eof: if (ferror(fp1))108err(ERR_EXIT, "%s", file1);109if (ferror(fp2))110err(ERR_EXIT, "%s", file2);111if (feof(fp1)) {112if (!feof(fp2)) {113eofmsg(file1);114return (DIFF_EXIT);115}116} else {117if (feof(fp2)) {118eofmsg(file2);119return (DIFF_EXIT);120}121}122fclose(fp2);123fclose(fp1);124return (dfound ? DIFF_EXIT : 0);125}126127128