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 <markk@knigma.org>
This commit is contained in:
Brian Somers 2000-07-14 16:54:59 +00:00
parent 59efcec109
commit 3e6902efc8
2 changed files with 19 additions and 5 deletions

View File

@ -43,7 +43,8 @@
.Nd compare two files .Nd compare two files
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm cmp .Nm cmp
.Op Fl l | Fl s .Op Fl l | Fl s | Fl x
.Op Fl z
.Ar file1 file2 .Ar file1 file2
.Op Ar skip1 Op Ar skip2 .Op Ar skip1 Op Ar skip2
.Sh DESCRIPTION .Sh DESCRIPTION
@ -71,6 +72,9 @@ Like
.Fl l .Fl l
but prints in hexadecimal and using zero as index but prints in hexadecimal and using zero as index
for the first byte in the files. 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 .El
.Pp .Pp
The optional arguments The optional arguments

View File

@ -56,7 +56,7 @@ static const char sccsid[] = "@(#)cmp.c 8.3 (Berkeley) 4/2/94";
#include "extern.h" #include "extern.h"
int lflag, sflag, xflag; int lflag, sflag, xflag, zflag;
static void usage __P((void)); static void usage __P((void));
@ -70,18 +70,22 @@ main(argc, argv)
int ch, fd1, fd2, special; int ch, fd1, fd2, special;
char *file1, *file2; char *file1, *file2;
while ((ch = getopt(argc, argv, "-lsx")) != -1) while ((ch = getopt(argc, argv, "-lsxz")) != -1)
switch (ch) { switch (ch) {
case 'l': /* print all differences */ case 'l': /* print all differences */
lflag = 1; lflag = 1;
break; break;
case 's': /* silent run */ case 's': /* silent run */
sflag = 1; sflag = 1;
zflag = 1;
break; break;
case 'x': /* hex output */ case 'x': /* hex output */
lflag = 1; lflag = 1;
xflag = 1; xflag = 1;
break; break;
case 'z': /* compare size first */
zflag = 1;
break;
case '-': /* stdin (must be after options) */ case '-': /* stdin (must be after options) */
--optind; --optind;
goto endargs; goto endargs;
@ -94,7 +98,7 @@ main(argc, argv)
argc -= optind; argc -= optind;
if (lflag && sflag) 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) if (argc < 2 || argc > 4)
usage(); usage();
@ -154,6 +158,12 @@ main(argc, argv)
if (special) if (special)
c_special(fd1, file1, skip1, fd2, file2, skip2); c_special(fd1, file1, skip1, fd2, file2, skip2);
else 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, c_regular(fd1, file1, skip1, sb1.st_size,
fd2, file2, skip2, sb2.st_size); fd2, file2, skip2, sb2.st_size);
exit(0); exit(0);
@ -164,6 +174,6 @@ usage()
{ {
(void)fprintf(stderr, (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); exit(ERR_EXIT);
} }