aboutsummaryrefslogtreecommitdiff
path: root/toys/posix/strings.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2014-06-11 08:11:38 -0500
committerRob Landley <rob@landley.net>2014-06-11 08:11:38 -0500
commitff2bc8551c697a909118f580b26b6cb1e0e47f61 (patch)
treed0e66c563345d5852fe6adf6c2d87e8ec75e9adf /toys/posix/strings.c
parent11c3924f8c29a36a80233326937ad21f504ae658 (diff)
downloadtoybox-ff2bc8551c697a909118f580b26b6cb1e0e47f61.tar.gz
Promote strings.
Diffstat (limited to 'toys/posix/strings.c')
-rw-r--r--toys/posix/strings.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/toys/posix/strings.c b/toys/posix/strings.c
new file mode 100644
index 00000000..a872cf6a
--- /dev/null
+++ b/toys/posix/strings.c
@@ -0,0 +1,67 @@
+/*strings.c - print the strings of printable characters in files.
+ *
+ * Copyright 2014 Kyung-su Kim <kaspyx@gmail.com>
+ * Copyright 2014 Kyungwan Han <asura321@gmail.com>
+ *
+ * No Standard
+ * TODO: utf8 strings
+ * TODO: posix -t
+
+USE_STRINGS(NEWTOY(strings, "an#=4<1fo", TOYFLAG_USR|TOYFLAG_BIN))
+
+config STRINGS
+ bool "strings"
+ default y
+ help
+ usage: strings [-fo] [-n LEN] [FILE...]
+
+ Display printable strings in a binary file
+
+ -f Precede strings with filenames
+ -n At least LEN characters form a string (default 4)
+ -o Precede strings with decimal offsets
+*/
+
+#define FOR_strings
+#include "toys.h"
+
+GLOBALS(
+ long num;
+)
+
+void do_strings(int fd, char *filename)
+{
+ int nread, i, wlen = TT.num, count = 0;
+ off_t offset = 0;
+ char *string = xzalloc(wlen + 1);
+
+ for (;;) {
+ nread = read(fd, toybuf, sizeof(toybuf));
+ if (nread < 0) perror_msg("%s", filename);
+ if (nread < 1) 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);
+ else {
+ string[count++] = toybuf[i];
+ if (count == wlen) {
+ if (toys.optflags & FLAG_f) printf("%s: ", filename);
+ if (toys.optflags & FLAG_o)
+ printf("%7lld ",(long long)(offset - wlen));
+ printf("%s", string);
+ }
+ }
+ } else {
+ if (count == wlen) xputc('\n');
+ count = 0;
+ }
+ }
+ }
+ xclose(fd);
+ free(string);
+}
+
+void strings_main(void)
+{
+ loopfiles(toys.optargs, do_strings);
+}