1149388Sbrian/*-
2149388Sbrian * Copyright (c) 2005 Brian Somers <brian@FreeBSD.org>
3149388Sbrian * All rights reserved.
4149388Sbrian *
5149388Sbrian * Redistribution and use in source and binary forms, with or without
6149388Sbrian * modification, are permitted provided that the following conditions
7149388Sbrian * are met:
8149388Sbrian * 1. Redistributions of source code must retain the above copyright
9149388Sbrian *    notice, this list of conditions and the following disclaimer.
10149388Sbrian * 2. Redistributions in binary form must reproduce the above copyright
11149388Sbrian *    notice, this list of conditions and the following disclaimer in the
12149388Sbrian *    documentation and/or other materials provided with the distribution.
13149388Sbrian *
14149388Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15149388Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16149388Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17149388Sbrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18149388Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19149388Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20149388Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21149388Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22149388Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23149388Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24149388Sbrian * SUCH DAMAGE.
25149388Sbrian */
26149388Sbrian
27149388Sbrian#include <sys/cdefs.h>
28149388Sbrian__FBSDID("$FreeBSD$");
29149388Sbrian
30149388Sbrian#include <sys/types.h>
31149388Sbrian#include <err.h>
32149388Sbrian#include <limits.h>
33149388Sbrian#include <stdio.h>
34149388Sbrian#include <stdlib.h>
35149388Sbrian#include <unistd.h>
36149388Sbrian
37149388Sbrian#include "extern.h"
38149388Sbrian
39149388Sbrianvoid
40149388Sbrianc_link(const char *file1, off_t skip1, const char *file2, off_t skip2)
41149388Sbrian{
42149388Sbrian	char buf1[PATH_MAX], *p1;
43149388Sbrian	char buf2[PATH_MAX], *p2;
44149388Sbrian	int dfound, len1, len2;
45149388Sbrian	off_t byte;
46149388Sbrian	u_char ch;
47149388Sbrian
48149388Sbrian	if ((len1 = readlink(file1, buf1, sizeof(buf1) - 1)) < 0) {
49149388Sbrian		if (!sflag)
50149388Sbrian			err(ERR_EXIT, "%s", file1);
51149388Sbrian		else
52149388Sbrian			exit(ERR_EXIT);
53149388Sbrian	}
54149388Sbrian
55149388Sbrian	if ((len2 = readlink(file2, buf2, sizeof(buf2) - 1)) < 0) {
56149388Sbrian		if (!sflag)
57149388Sbrian			err(ERR_EXIT, "%s", file2);
58149388Sbrian		else
59149388Sbrian			exit(ERR_EXIT);
60149388Sbrian	}
61149388Sbrian
62149388Sbrian	if (skip1 > len1)
63149388Sbrian		skip1 = len1;
64149388Sbrian	buf1[len1] = '\0';
65149388Sbrian
66149388Sbrian	if (skip2 > len2)
67149388Sbrian		skip2 = len2;
68149388Sbrian	buf2[len2] = '\0';
69149388Sbrian
70149388Sbrian	dfound = 0;
71149388Sbrian	byte = 1;
72149388Sbrian	for (p1 = buf1 + skip1, p2 = buf2 + skip2; *p1 && *p2; p1++, p2++) {
73149388Sbrian		if ((ch = *p1) != *p2) {
74149388Sbrian			if (xflag) {
75149388Sbrian				dfound = 1;
76149388Sbrian				(void)printf("%08llx %02x %02x\n",
77149388Sbrian				    (long long)byte - 1, ch, *p2);
78149388Sbrian			} else if (lflag) {
79149388Sbrian				dfound = 1;
80149388Sbrian				(void)printf("%6lld %3o %3o\n",
81149388Sbrian				    (long long)byte, ch, *p2);
82149388Sbrian			} else
83149388Sbrian				diffmsg(file1, file2, byte, 1);
84149388Sbrian				/* NOTREACHED */
85149388Sbrian		}
86149388Sbrian		byte++;
87149388Sbrian	}
88149388Sbrian
89149388Sbrian	if (*p1 || *p2)
90149388Sbrian		eofmsg (*p1 ? file2 : file1);
91149388Sbrian	if (dfound)
92149388Sbrian		exit(DIFF_EXIT);
93149388Sbrian}
94