xref: /freebsd/usr.bin/cmp/link.c (revision 3c37828ee1874754e1c5e96268016113c1e02ba2)
1e1bfde1bSBrian Somers /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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/types.h>
30d350e8d7SDag-Erling Smørgrav 
31e1bfde1bSBrian Somers #include <err.h>
32e1bfde1bSBrian Somers #include <limits.h>
331f766174SEd Maste #include <stdbool.h>
34e1bfde1bSBrian Somers #include <stdio.h>
35e1bfde1bSBrian Somers #include <stdlib.h>
36e1bfde1bSBrian Somers #include <unistd.h>
37e1bfde1bSBrian Somers 
38e1bfde1bSBrian Somers #include "extern.h"
39e1bfde1bSBrian Somers 
40*3c37828eSDag-Erling Smørgrav int
c_link(const char * file1,off_t skip1,const char * file2,off_t skip2,off_t limit)414e380e84SKyle Evans c_link(const char *file1, off_t skip1, const char *file2, off_t skip2,
424e380e84SKyle Evans     off_t limit)
43e1bfde1bSBrian Somers {
44e1bfde1bSBrian Somers 	char buf1[PATH_MAX], *p1;
45e1bfde1bSBrian Somers 	char buf2[PATH_MAX], *p2;
46d350e8d7SDag-Erling Smørgrav 	ssize_t len1, len2;
47d350e8d7SDag-Erling Smørgrav 	int dfound;
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;
754e380e84SKyle Evans 	for (p1 = buf1 + skip1, p2 = buf2 + skip2;
764e380e84SKyle Evans 	    *p1 && *p2 && (limit == 0 || byte <= limit); p1++, p2++) {
77e1bfde1bSBrian Somers 		if ((ch = *p1) != *p2) {
78e1bfde1bSBrian Somers 			if (xflag) {
79e1bfde1bSBrian Somers 				dfound = 1;
80e1bfde1bSBrian Somers 				(void)printf("%08llx %02x %02x\n",
81e1bfde1bSBrian Somers 				    (long long)byte - 1, ch, *p2);
82e1bfde1bSBrian Somers 			} else if (lflag) {
83e1bfde1bSBrian Somers 				dfound = 1;
84f66b9b40SKyle Evans 				if (bflag)
85f66b9b40SKyle Evans 					(void)printf("%6lld %3o %c %3o %c\n",
86f66b9b40SKyle Evans 					    (long long)byte, ch, ch, *p2, *p2);
87f66b9b40SKyle Evans 				else
88e1bfde1bSBrian Somers 					(void)printf("%6lld %3o %3o\n",
89e1bfde1bSBrian Somers 					    (long long)byte, ch, *p2);
90*3c37828eSDag-Erling Smørgrav 			} else {
91f66b9b40SKyle Evans 				diffmsg(file1, file2, byte, 1, ch, *p2);
92*3c37828eSDag-Erling Smørgrav 				return (DIFF_EXIT);
93*3c37828eSDag-Erling Smørgrav 			}
94e1bfde1bSBrian Somers 		}
95e1bfde1bSBrian Somers 		byte++;
96e1bfde1bSBrian Somers 	}
97e1bfde1bSBrian Somers 
98*3c37828eSDag-Erling Smørgrav 	if (*p1 || *p2) {
99e1bfde1bSBrian Somers 		eofmsg (*p1 ? file2 : file1);
100*3c37828eSDag-Erling Smørgrav 		return (DIFF_EXIT);
101*3c37828eSDag-Erling Smørgrav 	}
102*3c37828eSDag-Erling Smørgrav 	return (dfound ? DIFF_EXIT : 0);
103e1bfde1bSBrian Somers }
104