From 3e6902efc802ab57fc4e9bf798f2d271b152e7f9 Mon Sep 17 00:00:00 2001 From: Brian Somers Date: Fri, 14 Jul 2000 16:54:59 +0000 Subject: [PATCH] Add the -z flag to check file sizes first Correct the cmp.1 usage message Correct the -l/-s incompatibility message Submitted by: Mark Knight --- usr.bin/cmp/cmp.1 | 6 +++++- usr.bin/cmp/cmp.c | 18 ++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/usr.bin/cmp/cmp.1 b/usr.bin/cmp/cmp.1 index 60baf924da31..c8a5f2586d3a 100644 --- a/usr.bin/cmp/cmp.1 +++ b/usr.bin/cmp/cmp.1 @@ -43,7 +43,8 @@ .Nd compare two files .Sh SYNOPSIS .Nm cmp -.Op Fl l | Fl s +.Op Fl l | Fl s | Fl x +.Op Fl z .Ar file1 file2 .Op Ar skip1 Op Ar skip2 .Sh DESCRIPTION @@ -71,6 +72,9 @@ Like .Fl l but prints in hexadecimal and using zero as index for the first byte in the files. +.It Fl z +For regular files compare file sizes first, and fail the comparison if they +are not equal. .El .Pp The optional arguments diff --git a/usr.bin/cmp/cmp.c b/usr.bin/cmp/cmp.c index 2e1d7984e6c2..cfa43fe23fdc 100644 --- a/usr.bin/cmp/cmp.c +++ b/usr.bin/cmp/cmp.c @@ -56,7 +56,7 @@ static const char sccsid[] = "@(#)cmp.c 8.3 (Berkeley) 4/2/94"; #include "extern.h" -int lflag, sflag, xflag; +int lflag, sflag, xflag, zflag; static void usage __P((void)); @@ -70,18 +70,22 @@ main(argc, argv) int ch, fd1, fd2, special; char *file1, *file2; - while ((ch = getopt(argc, argv, "-lsx")) != -1) + while ((ch = getopt(argc, argv, "-lsxz")) != -1) switch (ch) { case 'l': /* print all differences */ lflag = 1; break; case 's': /* silent run */ sflag = 1; + zflag = 1; break; case 'x': /* hex output */ lflag = 1; xflag = 1; break; + case 'z': /* compare size first */ + zflag = 1; + break; case '-': /* stdin (must be after options) */ --optind; goto endargs; @@ -94,7 +98,7 @@ main(argc, argv) argc -= optind; if (lflag && sflag) - errx(ERR_EXIT, "only one of -l and -s may be specified"); + errx(ERR_EXIT, "specifying -s with -l or -x is not permitted"); if (argc < 2 || argc > 4) usage(); @@ -154,6 +158,12 @@ main(argc, argv) if (special) c_special(fd1, file1, skip1, fd2, file2, skip2); else + if (zflag && sb1.st_size != sb2.st_size) { + if (!sflag) + (void) printf("%s %s differ: size\n", + file1, file2); + exit(DIFF_EXIT); + } c_regular(fd1, file1, skip1, sb1.st_size, fd2, file2, skip2, sb2.st_size); exit(0); @@ -164,6 +174,6 @@ usage() { (void)fprintf(stderr, - "usage: cmp [-l | -s] file1 file2 [skip1 [skip2]]\n"); + "usage: cmp [-l | -s | -x] [-z] file1 file2 [skip1 [skip2]]\n"); exit(ERR_EXIT); }