git: 5d3ac4ddf9db - stable/13 - cmp: accept SI suffixes for skip1 and skip2
Kyle Evans
kevans at FreeBSD.org
Sun Oct 3 05:15:48 UTC 2021
The branch stable/13 has been updated by kevans:
URL: https://cgit.FreeBSD.org/src/commit/?id=5d3ac4ddf9db8a42968d31f11a8b18cdc198b4ff
commit 5d3ac4ddf9db8a42968d31f11a8b18cdc198b4ff
Author: Kyle Evans <kevans at FreeBSD.org>
AuthorDate: 2021-09-23 05:17:07 +0000
Commit: Kyle Evans <kevans at FreeBSD.org>
CommitDate: 2021-10-03 05:15:01 +0000
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.
(cherry picked from commit f6787614fd4db2a9d5e649af0e819852ebd5a19d)
---
usr.bin/cmp/Makefile | 2 ++
usr.bin/cmp/cmp.1 | 16 +++++++++++++++-
usr.bin/cmp/cmp.c | 14 ++++++++++++--
usr.bin/cmp/tests/cmp_test2.sh | 17 +++++++++++++++++
4 files changed, 46 insertions(+), 3 deletions(-)
diff --git a/usr.bin/cmp/Makefile b/usr.bin/cmp/Makefile
index bcde84f0255a..0392fd368503 100644
--- a/usr.bin/cmp/Makefile
+++ b/usr.bin/cmp/Makefile
@@ -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
diff --git a/usr.bin/cmp/cmp.1 b/usr.bin/cmp/cmp.1
index fc05fb893147..6980f73e7be5 100644
--- a/usr.bin/cmp/cmp.1
+++ b/usr.bin/cmp/cmp.1
@@ -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 .
diff --git a/usr.bin/cmp/cmp.c b/usr.bin/cmp/cmp.c
index 47f9b671985c..bab69125e83e 100644
--- a/usr.bin/cmp/cmp.c
+++ b/usr.bin/cmp/cmp.c
@@ -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;
diff --git a/usr.bin/cmp/tests/cmp_test2.sh b/usr.bin/cmp/tests/cmp_test2.sh
index 7f9801fc92bd..334e9f357730 100755
--- a/usr.bin/cmp/tests/cmp_test2.sh
+++ b/usr.bin/cmp/tests/cmp_test2.sh
@@ -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
}
More information about the dev-commits-src-all
mailing list