aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIlya Kuzmich <ilya.kuzmich@gmail.com>2017-05-29 07:05:16 +0300
committerGabriele M <moto.falcon.git@gmail.com>2018-01-27 17:37:04 +0000
commit3125af0e06f45e03d6411c182fb1b1318733e259 (patch)
treef75814d739d23444861db306cc341868920f9004
parentc766ad3d95eebf05f3890081592d30ce4c2025ac (diff)
downloadandroid_external_toybox-cm-14.1.tar.gz
android_external_toybox-cm-14.1.tar.bz2
android_external_toybox-cm-14.1.zip
strings tests and bugfixescm-14.1
Fixes missing newline in output if last byte of the input is string. Fixes one-off offset bug. Adds strings tests. Change-Id: I40c8c8308d86ab9c46bc02745bf6d4cb17fcfc41 Signed-off-by: Ilya Kuzmich <ilya.kuzmich@gmail.com>
-rw-r--r--tests/strings.test20
-rw-r--r--toys/posix/strings.c7
2 files changed, 25 insertions, 2 deletions
diff --git a/tests/strings.test b/tests/strings.test
new file mode 100644
index 00000000..8becc2f5
--- /dev/null
+++ b/tests/strings.test
@@ -0,0 +1,20 @@
+#!/bin/bash
+
+[ -f testing.sh ] && . testing.sh
+
+#testing "name" "command" "result" "infile" "stdin"
+
+testing "stdin" "strings" "foobar\n" "" "foobar\n"
+testing "file" "strings input" "foobar\n" "foobar\n" ""
+testing "string to the end" "strings input" "foobar\n" "foobar" ""
+testing "short strings" "strings input" "" "foo\nbar\n" ""
+testing "-n6" "strings -n6 input" "foobar\n" "foobar\nbaz\n" ""
+testing "string and nulls" "strings input" "foobar\nbazfoo\n" \
+ "\0foobar\0\0bazfoo\0foo" ""
+testing "-f" "strings -f input" "input: foobar\n" "foobar\n" ""
+testing "-o" "strings -o input | sed 's/^ *//'" "6 foobar\n" \
+ "\0\0\0\0\0\0foobar\n" ""
+testing "-o, multiple strings" "strings -n3 -o input | sed 's/^ *//'" \
+ "1 foo\n7 bar\n" "\0foo\0b\0bar\n" ""
+testing "-fo" "strings -fo input | sed 's/: */: /'" "input: 6 foobar\n" \
+ "\0\0\0\0\0\0foobar\n" ""
diff --git a/toys/posix/strings.c b/toys/posix/strings.c
index f3ce70ce..d2154e79 100644
--- a/toys/posix/strings.c
+++ b/toys/posix/strings.c
@@ -38,7 +38,10 @@ static void do_strings(int fd, char *filename)
for (;;) {
nread = read(fd, toybuf, sizeof(toybuf));
if (nread < 0) perror_msg_raw(filename);
- if (nread < 1) break;
+ if (nread < 1) {
+ if (count == wlen) xputc('\n');
+ break;
+ }
for (i = 0; i < nread; i++, offset++) {
if (((toybuf[i] >= 32) && (toybuf[i] <= 126)) || (toybuf[i] == '\t')) {
if (count == wlen) fputc(toybuf[i], stdout);
@@ -47,7 +50,7 @@ static void do_strings(int fd, char *filename)
if (count == wlen) {
if (toys.optflags & FLAG_f) printf("%s: ", filename);
if (toys.optflags & FLAG_o)
- printf("%7lld ",(long long)(offset - wlen));
+ printf("%7lld ",(long long)(offset + 1 - wlen));
printf("%s", string);
}
}