cmp: accept SI suffixes for skip1 and skip2

This is compatible with GNU cmp.

Reviewed by:	bapt (earlier version), markj, imp
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D32071
This commit is contained in:
Kyle Evans 2021-09-23 00:17:07 -05:00
parent 747a47261e
commit f6787614fd
4 changed files with 46 additions and 3 deletions

View File

@ -6,6 +6,8 @@
PROG= cmp
SRCS= cmp.c link.c misc.c regular.c special.c
LIBADD= util
HAS_TESTS=
SUBDIR.${MK_TESTS}+= tests

View File

@ -31,7 +31,7 @@
.\" @(#)cmp.1 8.1 (Berkeley) 6/6/93
.\" $FreeBSD$
.\"
.Dd June 20, 2020
.Dd September 23, 2021
.Dt CMP 1
.Os
.Sh NAME
@ -86,6 +86,11 @@ and
respectively, where the comparison will begin.
The offset is decimal by default, but may be expressed as a hexadecimal
or octal value by preceding it with a leading ``0x'' or ``0''.
.Pp
.Ar skip1
and
.Ar skip2
may also be specified with SI size suffixes.
.Sh EXIT STATUS
The
.Nm
@ -164,8 +169,17 @@ The
and
.Fl z
options are extensions to the standard.
.Ar skip1
and
.Ar skip2
arguments are extensions to the standard.
.Sh HISTORY
A
.Nm
command appeared in
.At v1 .
.Sh BUGS
The phrase
.Dq SI size suffixes
above refers to the traditional power of two convention, as described in
.Xr expand_number 3 .

View File

@ -58,6 +58,8 @@ __FBSDID("$FreeBSD$");
#include <string.h>
#include <unistd.h>
#include <libutil.h>
#include "extern.h"
bool lflag, sflag, xflag, zflag;
@ -81,6 +83,7 @@ main(int argc, char *argv[])
bool special;
const char *file1, *file2;
skip1 = skip2 = 0;
oflag = O_RDONLY;
while ((ch = getopt_long(argc, argv, "+hlsxz", long_opts, NULL)) != -1)
switch (ch) {
@ -145,8 +148,15 @@ main(int argc, char *argv[])
exit(ERR_EXIT);
}
skip1 = argc > 2 ? strtol(argv[2], NULL, 0) : 0;
skip2 = argc == 4 ? strtol(argv[3], NULL, 0) : 0;
if (argc > 2 && expand_number(argv[2], &skip1) < 0) {
fprintf(stderr, "Invalid skip1: %s\n", argv[2]);
usage();
}
if (argc == 4 && expand_number(argv[3], &skip2) < 0) {
fprintf(stderr, "Invalid skip2: %s\n", argv[3]);
usage();
}
if (sflag && skip1 == 0 && skip2 == 0)
zflag = true;

View File

@ -75,9 +75,26 @@ pr252542_body()
atf_check -s exit:1 -o ignore cmp -z a b 4 3
}
atf_test_case skipsuff
skipsuff_head()
{
atf_set "descr" "Test cmp(1) accepting SI suffixes on skips"
}
skipsuff_body()
{
jot -nb a -s '' 1028 > a
jot -nb b -s '' 1024 > b
jot -nb a -s '' 4 >> b
atf_check -s exit:1 -o ignore cmp -s a b
atf_check -s exit:0 cmp -s a b 1k 1k
}
atf_init_test_cases()
{
atf_add_test_case special
atf_add_test_case symlink
atf_add_test_case pr252542
atf_add_test_case skipsuff
}