commit: f406e24396ed344dd535f44540ceb0004ce61e52
parent 36767da66e4aa63bd7795f20ed7a907fb0207e13
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Mon, 9 Sep 2024 04:05:34 +0200
cmd/strings: add support for -z option
Diffstat:
3 files changed, 32 insertions(+), 11 deletions(-)
diff --git a/cmd/strings.1 b/cmd/strings.1
@@ -1,7 +1,7 @@
.\" utils-std: Collection of commonly available Unix tools
.\" Copyright 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
.\" SPDX-License-Identifier: MPL-2.0
-.Dd 2023-08-11
+.Dd 2024-09-09
.Dt STRINGS 1
.Os
.Sh NAME
@@ -9,7 +9,7 @@
.Nd find printable strings
.Sh SYNOPSIS
.Nm
-.Op Fl a
+.Op Fl az
.Op Fl t Ar format
.Op Fl n Ar number
.Op Ar file...
@@ -46,6 +46,8 @@ Octal
.It x
Hexadecimal
.El
+.It Fl z
+Separate each string with NULL instead of newline.
.El
.Sh EXIT STATUS
.Ex -std
@@ -57,5 +59,8 @@ Hexadecimal
should be compliant with the
IEEE Std 1003.1-2024 (“POSIX.1”)
specification.
+The
+.Fl z
+option is an extension.
.Sh AUTHORS
.An Haelwenn (lanodan) Monnier Aq Mt contact+utils@hacktivis.me
diff --git a/cmd/strings.c b/cmd/strings.c
@@ -15,6 +15,7 @@
size_t opt_min_strlen = 4;
const char *opt_offset_format = NULL;
+int delim = '\n';
static int
print_string(char *buffer, size_t offset)
@@ -23,11 +24,11 @@ print_string(char *buffer, size_t offset)
if(opt_offset_format == NULL)
{
- ret = printf("%s\n", buffer);
+ ret = printf("%s%c", buffer, delim);
}
else
{
- ret = printf(opt_offset_format, offset, buffer);
+ ret = printf(opt_offset_format, offset, buffer, delim);
}
if(ret < 0)
@@ -99,14 +100,14 @@ concat(int fd, const char *fdname)
static void
usage(void)
{
- fprintf(stderr, "strings: [-a] [-t format] [-n number] [file...]\n");
+ fprintf(stderr, "strings: [-az] [-t format] [-n number] [file...]\n");
}
int
main(int argc, char *argv[])
{
int c;
- while((c = getopt(argc, argv, ":an:t:")) != -1)
+ while((c = getopt(argc, argv, ":an:t:z")) != -1)
{
switch(c)
{
@@ -151,13 +152,13 @@ main(int argc, char *argv[])
switch(optarg[0])
{
case 'o':
- opt_offset_format = "%zo %s\n";
+ opt_offset_format = "%zo %s%c";
break;
case 'x':
- opt_offset_format = "%zx %s\n";
+ opt_offset_format = "%zx %s%c";
break;
case 'd':
- opt_offset_format = "%zd %s\n";
+ opt_offset_format = "%zd %s%c";
break;
default:
fprintf(stderr, "strings: Unknown format: %s\n", optarg);
@@ -165,6 +166,19 @@ main(int argc, char *argv[])
return 1;
}
break;
+ case 'z':
+ delim = '\0';
+ break;
+ case ':':
+ fprintf(stderr, "strings: Error: Missing operand for option: '-%c'\n", optopt);
+ usage();
+ return 1;
+ case '?':
+ fprintf(stderr, "strings: Error: Unrecognised option: '-%c'\n", optopt);
+ usage();
+ return 1;
+ default:
+ abort();
}
}
diff --git a/test-cmd/strings.sh b/test-cmd/strings.sh
@@ -4,10 +4,10 @@
WD="$(dirname "$0")"
target="${WD}/../cmd/strings"
-plans=25
+plans=26
. "$(dirname "$0")/tap.sh"
-usage="strings: [-a] [-t format] [-n number] [file...]
+usage="strings: [-az] [-t format] [-n number] [file...]
"
t --exit=1 usage "-t aa $WD/inputs/all_bytes" "${usage}"
@@ -28,6 +28,8 @@ t n8 "-n 8 ${WD}/inputs/strings/length" '_______8
________9
'
+t z8 "-z -n 8 ${WD}/inputs/strings/length" "$(printf '_______8\0________9\0')"
+
#atf_test_case noperm cleanup
#noperm_body() {
# touch inputs/chmod_000 || atf_fail "touching chmod_000"