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