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