1e1bfde1bSBrian Somers /*- 21de7b4b8SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 31de7b4b8SPedro F. Giffuni * 4e1bfde1bSBrian Somers * Copyright (c) 2005 Brian Somers <brian@FreeBSD.org> 5e1bfde1bSBrian Somers * All rights reserved. 6e1bfde1bSBrian Somers * 7e1bfde1bSBrian Somers * Redistribution and use in source and binary forms, with or without 8e1bfde1bSBrian Somers * modification, are permitted provided that the following conditions 9e1bfde1bSBrian Somers * are met: 10e1bfde1bSBrian Somers * 1. Redistributions of source code must retain the above copyright 11e1bfde1bSBrian Somers * notice, this list of conditions and the following disclaimer. 12e1bfde1bSBrian Somers * 2. Redistributions in binary form must reproduce the above copyright 13e1bfde1bSBrian Somers * notice, this list of conditions and the following disclaimer in the 14e1bfde1bSBrian Somers * documentation and/or other materials provided with the distribution. 15e1bfde1bSBrian Somers * 16e1bfde1bSBrian Somers * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17e1bfde1bSBrian Somers * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18e1bfde1bSBrian Somers * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19e1bfde1bSBrian Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20e1bfde1bSBrian Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21e1bfde1bSBrian Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22e1bfde1bSBrian Somers * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23e1bfde1bSBrian Somers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24e1bfde1bSBrian Somers * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25e1bfde1bSBrian Somers * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26e1bfde1bSBrian Somers * SUCH DAMAGE. 27e1bfde1bSBrian Somers */ 28e1bfde1bSBrian Somers 29e1bfde1bSBrian Somers #include <sys/cdefs.h> 30e1bfde1bSBrian Somers __FBSDID("$FreeBSD$"); 31e1bfde1bSBrian Somers 32e1bfde1bSBrian Somers #include <sys/types.h> 33e1bfde1bSBrian Somers #include <err.h> 34e1bfde1bSBrian Somers #include <limits.h> 35*1f766174SEd Maste #include <stdbool.h> 36e1bfde1bSBrian Somers #include <stdio.h> 37e1bfde1bSBrian Somers #include <stdlib.h> 38e1bfde1bSBrian Somers #include <unistd.h> 39e1bfde1bSBrian Somers 40e1bfde1bSBrian Somers #include "extern.h" 41e1bfde1bSBrian Somers 42e1bfde1bSBrian Somers void 43e1bfde1bSBrian Somers c_link(const char *file1, off_t skip1, const char *file2, off_t skip2) 44e1bfde1bSBrian Somers { 45e1bfde1bSBrian Somers char buf1[PATH_MAX], *p1; 46e1bfde1bSBrian Somers char buf2[PATH_MAX], *p2; 47e1bfde1bSBrian Somers int dfound, len1, len2; 48e1bfde1bSBrian Somers off_t byte; 49e1bfde1bSBrian Somers u_char ch; 50e1bfde1bSBrian Somers 51e1bfde1bSBrian Somers if ((len1 = readlink(file1, buf1, sizeof(buf1) - 1)) < 0) { 52e1bfde1bSBrian Somers if (!sflag) 53e1bfde1bSBrian Somers err(ERR_EXIT, "%s", file1); 54e1bfde1bSBrian Somers else 55e1bfde1bSBrian Somers exit(ERR_EXIT); 56e1bfde1bSBrian Somers } 57e1bfde1bSBrian Somers 58e1bfde1bSBrian Somers if ((len2 = readlink(file2, buf2, sizeof(buf2) - 1)) < 0) { 59e1bfde1bSBrian Somers if (!sflag) 60e1bfde1bSBrian Somers err(ERR_EXIT, "%s", file2); 61e1bfde1bSBrian Somers else 62e1bfde1bSBrian Somers exit(ERR_EXIT); 63e1bfde1bSBrian Somers } 64e1bfde1bSBrian Somers 65e1bfde1bSBrian Somers if (skip1 > len1) 66e1bfde1bSBrian Somers skip1 = len1; 67e1bfde1bSBrian Somers buf1[len1] = '\0'; 68e1bfde1bSBrian Somers 69e1bfde1bSBrian Somers if (skip2 > len2) 70e1bfde1bSBrian Somers skip2 = len2; 71e1bfde1bSBrian Somers buf2[len2] = '\0'; 72e1bfde1bSBrian Somers 73e1bfde1bSBrian Somers dfound = 0; 74e1bfde1bSBrian Somers byte = 1; 75e1bfde1bSBrian Somers for (p1 = buf1 + skip1, p2 = buf2 + skip2; *p1 && *p2; p1++, p2++) { 76e1bfde1bSBrian Somers if ((ch = *p1) != *p2) { 77e1bfde1bSBrian Somers if (xflag) { 78e1bfde1bSBrian Somers dfound = 1; 79e1bfde1bSBrian Somers (void)printf("%08llx %02x %02x\n", 80e1bfde1bSBrian Somers (long long)byte - 1, ch, *p2); 81e1bfde1bSBrian Somers } else if (lflag) { 82e1bfde1bSBrian Somers dfound = 1; 83e1bfde1bSBrian Somers (void)printf("%6lld %3o %3o\n", 84e1bfde1bSBrian Somers (long long)byte, ch, *p2); 85e1bfde1bSBrian Somers } else 86e1bfde1bSBrian Somers diffmsg(file1, file2, byte, 1); 87e1bfde1bSBrian Somers /* NOTREACHED */ 88e1bfde1bSBrian Somers } 89e1bfde1bSBrian Somers byte++; 90e1bfde1bSBrian Somers } 91e1bfde1bSBrian Somers 92e1bfde1bSBrian Somers if (*p1 || *p2) 93e1bfde1bSBrian Somers eofmsg (*p1 ? file2 : file1); 94e1bfde1bSBrian Somers if (dfound) 95e1bfde1bSBrian Somers exit(DIFF_EXIT); 96e1bfde1bSBrian Somers } 97