Add a -h option to tell cmp not to follow symbolic links.

MFC after:	3 weeks
Sponsored by:	Sophos/ActiveState
This commit is contained in:
Brian Somers 2005-08-23 13:13:13 +00:00
parent 70037ab36a
commit e1bfde1b04
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=149388
5 changed files with 123 additions and 7 deletions

View File

@ -2,6 +2,6 @@
# $FreeBSD$
PROG= cmp
SRCS= cmp.c misc.c regular.c special.c
SRCS= cmp.c link.c misc.c regular.c special.c
.include <bsd.prog.mk>

View File

@ -44,7 +44,7 @@
.Sh SYNOPSIS
.Nm
.Op Fl l | Fl s | Fl x
.Op Fl z
.Op Fl hz
.Ar file1 file2
.Op Ar skip1 Op Ar skip2
.Sh DESCRIPTION
@ -61,6 +61,8 @@ Bytes and lines are numbered beginning with one.
.Pp
The following options are available:
.Bl -tag -width flag
.It Fl h
Don't follow symbolic links.
.It Fl l
Print the byte number (decimal) and the differing
byte values (octal) for each difference.

View File

@ -50,6 +50,7 @@ __FBSDID("$FreeBSD$");
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
@ -67,11 +68,15 @@ main(int argc, char *argv[])
{
struct stat sb1, sb2;
off_t skip1, skip2;
int ch, fd1, fd2, special;
int ch, fd1, fd2, oflag, special;
const char *file1, *file2;
while ((ch = getopt(argc, argv, "lsxz")) != -1)
oflag = O_RDONLY;
while ((ch = getopt(argc, argv, "hlsxz")) != -1)
switch (ch) {
case 'h': /* Don't follow symlinks */
oflag |= O_NOFOLLOW;
break;
case 'l': /* print all differences */
lflag = 1;
break;
@ -106,7 +111,7 @@ main(int argc, char *argv[])
fd1 = 0;
file1 = "stdin";
}
else if ((fd1 = open(file1, O_RDONLY, 0)) < 0) {
else if ((fd1 = open(file1, oflag, 0)) < 0 && errno != EMLINK) {
if (!sflag)
err(ERR_EXIT, "%s", file1);
else
@ -120,7 +125,7 @@ main(int argc, char *argv[])
fd2 = 0;
file2 = "stdin";
}
else if ((fd2 = open(file2, O_RDONLY, 0)) < 0) {
else if ((fd2 = open(file2, oflag, 0)) < 0 && errno != EMLINK) {
if (!sflag)
err(ERR_EXIT, "%s", file2);
else
@ -130,6 +135,21 @@ main(int argc, char *argv[])
skip1 = argc > 2 ? strtol(argv[2], NULL, 0) : 0;
skip2 = argc == 4 ? strtol(argv[3], NULL, 0) : 0;
if (fd1 == -1) {
if (fd2 == -1) {
c_link(file1, skip1, file2, skip2);
exit(0);
} else if (!sflag)
errx(ERR_EXIT, "%s: Not a symbolic link", file2);
else
exit(ERR_EXIT);
} else if (fd2 == -1) {
if (!sflag)
errx(ERR_EXIT, "%s: Not a symbolic link", file1);
else
exit(ERR_EXIT);
}
if (!special) {
if (fstat(fd1, &sb1)) {
if (!sflag)
@ -171,6 +191,6 @@ usage(void)
{
(void)fprintf(stderr,
"usage: cmp [-l | -s | -x] [-z] file1 file2 [skip1 [skip2]]\n");
"usage: cmp [-l | -s | -x] [-hz] file1 file2 [skip1 [skip2]]\n");
exit(ERR_EXIT);
}

View File

@ -40,6 +40,7 @@
#define DIFF_EXIT 1
#define ERR_EXIT 2 /* error exit code */
void c_link(const char *, off_t, const char *, off_t);
void c_regular(int, const char *, off_t, off_t, int, const char *, off_t, off_t);
void c_special(int, const char *, off_t, int, const char *, off_t);
void diffmsg(const char *, const char *, off_t, off_t);

93
usr.bin/cmp/link.c Normal file
View File

@ -0,0 +1,93 @@
/*-
* Copyright (c) 2005 Brian Somers <brian@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <err.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "extern.h"
void
c_link(const char *file1, off_t skip1, const char *file2, off_t skip2)
{
char buf1[PATH_MAX], *p1;
char buf2[PATH_MAX], *p2;
int dfound, len1, len2;
off_t byte;
u_char ch;
if ((len1 = readlink(file1, buf1, sizeof(buf1) - 1)) < 0) {
if (!sflag)
err(ERR_EXIT, "%s", file1);
else
exit(ERR_EXIT);
}
if ((len2 = readlink(file2, buf2, sizeof(buf2) - 1)) < 0) {
if (!sflag)
err(ERR_EXIT, "%s", file2);
else
exit(ERR_EXIT);
}
if (skip1 > len1)
skip1 = len1;
buf1[len1] = '\0';
if (skip2 > len2)
skip2 = len2;
buf2[len2] = '\0';
dfound = 0;
byte = 1;
for (p1 = buf1 + skip1, p2 = buf2 + skip2; *p1 && *p2; p1++, p2++) {
if ((ch = *p1) != *p2) {
if (xflag) {
dfound = 1;
(void)printf("%08llx %02x %02x\n",
(long long)byte - 1, ch, *p2);
} else if (lflag) {
dfound = 1;
(void)printf("%6lld %3o %3o\n",
(long long)byte, ch, *p2);
} else
diffmsg(file1, file2, byte, 1);
/* NOTREACHED */
}
byte++;
}
if (*p1 || *p2)
eofmsg (*p1 ? file2 : file1);
if (dfound)
exit(DIFF_EXIT);
}