/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2005 Brian Somers <[email protected]>4* 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*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#include <sys/types.h>2930#include <err.h>31#include <limits.h>32#include <stdbool.h>33#include <stdio.h>34#include <stdlib.h>35#include <unistd.h>3637#include "extern.h"3839int40c_link(const char *file1, off_t skip1, const char *file2, off_t skip2,41off_t limit)42{43char buf1[PATH_MAX], *p1;44char buf2[PATH_MAX], *p2;45ssize_t len1, len2;46int dfound;47off_t byte;48u_char ch;4950if ((len1 = readlink(file1, buf1, sizeof(buf1) - 1)) < 0) {51if (!sflag)52err(ERR_EXIT, "%s", file1);53else54exit(ERR_EXIT);55}5657if ((len2 = readlink(file2, buf2, sizeof(buf2) - 1)) < 0) {58if (!sflag)59err(ERR_EXIT, "%s", file2);60else61exit(ERR_EXIT);62}6364if (skip1 > len1)65skip1 = len1;66buf1[len1] = '\0';6768if (skip2 > len2)69skip2 = len2;70buf2[len2] = '\0';7172dfound = 0;73byte = 1;74for (p1 = buf1 + skip1, p2 = buf2 + skip2;75*p1 && *p2 && (limit == 0 || byte <= limit); p1++, p2++) {76if ((ch = *p1) != *p2) {77if (xflag) {78dfound = 1;79(void)printf("%08llx %02x %02x\n",80(long long)byte - 1, ch, *p2);81} else if (lflag) {82dfound = 1;83if (bflag)84(void)printf("%6lld %3o %c %3o %c\n",85(long long)byte, ch, ch, *p2, *p2);86else87(void)printf("%6lld %3o %3o\n",88(long long)byte, ch, *p2);89} else {90diffmsg(file1, file2, byte, 1, ch, *p2);91return (DIFF_EXIT);92}93}94byte++;95}9697if (*p1 || *p2) {98eofmsg (*p1 ? file2 : file1);99return (DIFF_EXIT);100}101return (dfound ? DIFF_EXIT : 0);102}103104105