commit: 0e9fac8c698d80d58ca0a2f383ff64f30a5694a5
parent 708242e74bc7ae6283c0cec80528562257361dc0
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 13 Jan 2023 11:03:41 +0100
Drop dependency on libbsd
Diffstat:
7 files changed, 64 insertions(+), 65 deletions(-)
diff --git a/README.md b/README.md
@@ -1,5 +1,5 @@
<!--
-SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
-->
@@ -12,7 +12,6 @@ Tested on Linux(musl), FreeBSD, NetBSD, OpenBSD:
# Dependencies
- POSIX System
-- (optional) libbsd: <https://libbsd.freedesktop.org/> To build `bin/strings` and `bin/humanize`
- (optional, test) ATF: <https://github.com/jmmv/atf>
- (optional, test) Kyua: <https://github.com/jmmv/kyua>
- (optional, test) bwrap: <https://github.com/containers/bubblewrap/> For safely overlaying false files on the root filesystem
diff --git a/cmd/humanize.c b/cmd/humanize.c
@@ -1,18 +1,14 @@
// Collection of Unix tools, comparable to coreutils
-// SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+// SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
-#define _DEFAULT_SOURCE
-// strtonum
-#define _OPENBSD_SOURCE
-// On non-BSDs use libbsd
+#define _POSIX_C_SOURCE 200809L
#include <err.h> // errx
#include <errno.h> // EINVAL, ERANGE
#include <limits.h> // LLONG_MIN, LLONG_MAX
#include <stdbool.h> // bool
-#include <stdio.h> // fprintf
+#include <stdio.h> // fprintf, perror
#include <stdlib.h> // strtonum
-#include <string.h> // strerror
#include <unistd.h> // opt*, getopt
void
@@ -61,11 +57,11 @@ main(int argc, char *argv[])
iec = false;
break;
case ':':
- fprintf(stderr, "Error: Missing operand for option: '-%c'\n", optopt);
+ fprintf(stderr, "humanize: Error: Missing operand for option: '-%c'\n", optopt);
usage();
return 1;
case '?':
- fprintf(stderr, "Error: Unrecognised option: '-%c'\n", optopt);
+ fprintf(stderr, "humanize: Error: Unrecognised option: '-%c'\n", optopt);
usage();
return 1;
}
@@ -85,11 +81,19 @@ main(int argc, char *argv[])
const char *errstr = NULL;
char buf[32] = "";
- // 1024^(6+1) goes higher than long long; don't ask for the moon
- long long n = strtonum(argv[argi], LLONG_MIN, LLONG_MAX, &errstr);
- if(errstr)
+ errno = 0;
+ long long n = strtoll(argv[argi], NULL, 10);
+ if(n == LLONG_MIN)
{
- errx(1, "strtonum: %s is %s", argv[argi], errstr);
+ errx(1, "%s is too small", argv[argi]);
+ }
+ if(n == LLONG_MAX)
+ {
+ errx(1, "%s is too large", argv[argi]);
+ }
+ if(errno != 0)
+ {
+ perror("humanize: strtoll");
}
dtosi(n, buf, 32, iec);
diff --git a/cmd/sleep.c b/cmd/sleep.c
@@ -1,28 +1,25 @@
// Collection of Unix tools, comparable to coreutils
-// SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+// SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
-#define _DEFAULT_SOURCE
-// strtou
-#define _NETBSD_SOURCE
-// On non-BSDs use libbsd
+#define _POSIX_C_SOURCE 200809L
#include <errno.h> // ENOTSUP
#include <inttypes.h> // strtou
#include <limits.h> // UINT_MAX, _POSIX_ARG_MAX
-#include <stdio.h> // fprintf
+#include <stdio.h> // fprintf, perror
#include <stdlib.h> // exit
-#include <string.h> // strnlen, strerror
+#include <string.h> // strnlen
#include <unistd.h> // sleep
uintmax_t
argtonum(char *arg)
{
char *end;
- int err = 0;
- uintmax_t dur = strtou(arg, &end, 10, 1, UINT_MAX, &err);
- if(err != 0 && err != ENOTSUP)
+ errno = 0;
+ unsigned long dur = strtoul(arg, &end, 10);
+ if(errno != 0)
{
- fprintf(stderr, "sleep: Error parsing '%s' as a number: %s\n", arg, strerror(err));
+ perror("sleep: strtoul");
exit(1);
}
if(end[0] != 0)
diff --git a/cmd/strings.c b/cmd/strings.c
@@ -1,18 +1,17 @@
// Collection of Unix tools, comparable to coreutils
-// SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+// SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
#define _POSIX_C_SOURCE 200809L
#include <ctype.h> /* isprint() */
#include <errno.h> /* errno */
#include <fcntl.h> /* open(), O_RDONLY */
+#include <limits.h> /* LONG_MIN, LONG_MAX */
#include <stdio.h> /* fprintf(), BUFSIZ */
+#include <stdlib.h> /* strtol() */
#include <string.h> /* strerror(), strncmp(), memset() */
#include <unistd.h> /* read(), write(), close(), getopt(), optarg, optind */
-#define _OPENBSD_SOURCE
-#include <stdlib.h> /* (BSD) strtonum() */
-
size_t opt_min_strlen = 4;
char *opt_offset_format = NULL;
@@ -116,10 +115,29 @@ main(int argc, char *argv[])
/* Structure is always ignored */
break;
case 'n':
- opt_min_strlen = (size_t)strtonum(optarg, 1, 4096, &errstr);
- if(errstr)
+ errno = 0;
+ char *endptr = "";
+ opt_min_strlen = strtol(optarg, &endptr, 10);
+ if(*endptr != 0)
+ {
+ // extraneous characters is invalid
+ errno = EINVAL;
+ }
+ if(errno != 0)
+ {
+ fprintf(stderr, "strings: Option `-n %s`: %s\n", optarg, strerror(errno));
+ usage();
+ return 1;
+ }
+ if(opt_min_strlen == LLONG_MIN || opt_min_strlen < 1)
+ {
+ fprintf(stderr, "strings: Option `-n %s` is too small\n", optarg);
+ usage();
+ return 1;
+ }
+ if(opt_min_strlen == LLONG_MAX || opt_min_strlen > 4096)
{
- fprintf(stderr, "strings: Minimal string length is %s: %s\n", errstr, optarg);
+ fprintf(stderr, "strings: Option `-n %s` is too large\n", optarg);
usage();
return 1;
}
diff --git a/configure b/configure
@@ -1,5 +1,5 @@
#!/bin/sh
-# SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+# SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
# SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
DEPS=""
@@ -194,25 +194,6 @@ do
pkg_config_check --exists "$dep" || exit 1
done
-# TODO: Check for strtonum and strtou existance
-if pkg_config_check libbsd-overlay
-then
- LIBBSD_CFLAGS="$("${PKGCONFIG}" --cflags libbsd-overlay) -DHAVE_LIBBSD"
- LIBBSD_LIBS="$("${PKGCONFIG}" --libs libbsd-overlay)"
-else
- cat >> target_filter <<EOF
-# strtou
-sleep.c
-sleep.1
-# strtonum
-strings.c
-strings.1
-humanize.c
-humanize.1
-EOF
- test $? -eq 0 || exit 1
-fi
-
echo
## Configuration write
diff --git a/test-cmd/humanize b/test-cmd/humanize
@@ -1,5 +1,5 @@
#!/usr/bin/env atf-sh
-# SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+# SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
# SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
atf_test_case one
@@ -62,19 +62,19 @@ noarg_body() {
atf_test_case badflag
badflag_body() {
- atf_check -s exit:1 -e "inline:Error: Unrecognised option: '-f'\n"'Usage: humanize [-bd] number\n' ../cmd/humanize -f
- atf_check -s exit:1 -e "inline:Error: Unrecognised option: '-f'\n"'Usage: humanize [-bd] number\n' ../cmd/humanize -f 123
+ atf_check -s exit:1 -e "inline:humanize: Error: Unrecognised option: '-f'\n"'Usage: humanize [-bd] number\n' ../cmd/humanize -f
+ atf_check -s exit:1 -e "inline:humanize: Error: Unrecognised option: '-f'\n"'Usage: humanize [-bd] number\n' ../cmd/humanize -f 123
}
atf_test_case limits
limits_body() {
- atf_check -o 'inline:9.22337 E\n' ../cmd/humanize 9223372036854775807
- atf_check -s exit:1 -e 'inline:humanize: strtonum: 9223372036854775808 is too large\n' ../cmd/humanize 9223372036854775808
+ atf_check -o 'inline:9.22337 E\n' ../cmd/humanize 9223372036854775806
+ atf_check -s exit:1 -e 'inline:humanize: 9223372036854775808 is too large\n' ../cmd/humanize 9223372036854775808
# Not as great as it should but at least not a lie
- atf_check -o 'inline:-9.22337e+18 \n' ../cmd/humanize -- -9223372036854775808
+ atf_check -o 'inline:-9.22337e+18 \n' ../cmd/humanize -- -9223372036854775807
- atf_check -s exit:1 -e 'inline:humanize: strtonum: -9223372036854775809 is too small\n' ../cmd/humanize -- -9223372036854775809
+ atf_check -s exit:1 -e 'inline:humanize: -9223372036854775809 is too small\n' ../cmd/humanize -- -9223372036854775809
}
atf_init_test_cases() {
diff --git a/test-cmd/strings b/test-cmd/strings
@@ -1,5 +1,5 @@
#!/usr/bin/env atf-sh
-# SPDX-FileCopyrightText: 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+# SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
# SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
atf_test_case allbytes
@@ -89,10 +89,10 @@ atf_test_case erange_n
erange_n_body() {
usage="strings: [-a] [-t format] [-n number] [file...]\n"
- atf_check -s exit:1 -e "inline:strings: Minimal string length is too small: 0\n${usage}" -- ../cmd/strings -n 0 inputs/all_bytes
- atf_check -s exit:1 -e "inline:strings: Minimal string length is too large: 4097\n${usage}" -- ../cmd/strings -n 4097 inputs/all_bytes
- atf_check -s exit:1 -e "inline:strings: Minimal string length is invalid: f\n${usage}" -- ../cmd/strings -n f inputs/all_bytes
- atf_check -s exit:1 -e "inline:strings: Minimal string length is invalid: 42f\n${usage}" -- ../cmd/strings -n 42f inputs/all_bytes
+ atf_check -s exit:1 -e "inline:strings: Option \`-n 0\` is too small\n${usage}" -- ../cmd/strings -n 0 inputs/all_bytes
+ atf_check -s exit:1 -e "inline:strings: Option \`-n 4097\` is too large\n${usage}" -- ../cmd/strings -n 4097 inputs/all_bytes
+ atf_check -s exit:1 -e "inline:strings: Option \`-n f\`: Invalid argument\n${usage}" -- ../cmd/strings -n f inputs/all_bytes
+ atf_check -s exit:1 -e "inline:strings: Option \`-n 42f\`: Invalid argument\n${usage}" -- ../cmd/strings -n 42f inputs/all_bytes
}
atf_test_case usage