From 8cf3c8952235c756487d763a755ee223c358b12e Mon Sep 17 00:00:00 2001 From: brian Date: Tue, 23 Aug 2005 13:13:13 +0000 Subject: Add a -h option to tell cmp not to follow symbolic links. MFC after: 3 weeks Sponsored by: Sophos/ActiveState --- usr.bin/cmp/Makefile | 2 +- usr.bin/cmp/cmp.1 | 4 ++- usr.bin/cmp/cmp.c | 30 ++++++++++++++--- usr.bin/cmp/extern.h | 1 + usr.bin/cmp/link.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 123 insertions(+), 7 deletions(-) create mode 100644 usr.bin/cmp/link.c (limited to 'usr.bin/cmp') diff --git a/usr.bin/cmp/Makefile b/usr.bin/cmp/Makefile index 010ece5..d93f54b 100644 --- a/usr.bin/cmp/Makefile +++ b/usr.bin/cmp/Makefile @@ -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 diff --git a/usr.bin/cmp/cmp.1 b/usr.bin/cmp/cmp.1 index 2f82bc3..22ed932 100644 --- a/usr.bin/cmp/cmp.1 +++ b/usr.bin/cmp/cmp.1 @@ -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. diff --git a/usr.bin/cmp/cmp.c b/usr.bin/cmp/cmp.c index 0032188..304aa7de 100644 --- a/usr.bin/cmp/cmp.c +++ b/usr.bin/cmp/cmp.c @@ -50,6 +50,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include #include #include @@ -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); } diff --git a/usr.bin/cmp/extern.h b/usr.bin/cmp/extern.h index 75afb2b..1dd1c82 100644 --- a/usr.bin/cmp/extern.h +++ b/usr.bin/cmp/extern.h @@ -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); diff --git a/usr.bin/cmp/link.c b/usr.bin/cmp/link.c new file mode 100644 index 0000000..fe45def --- /dev/null +++ b/usr.bin/cmp/link.c @@ -0,0 +1,93 @@ +/*- + * Copyright (c) 2005 Brian Somers + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include + +#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); +} -- cgit v1.1