aboutsummaryrefslogtreecommitdiff
path: root/toys/pending/strings.c
diff options
context:
space:
mode:
authorAshwini Sharma <ak.ashwini1981@gmail.com>2014-05-29 08:18:50 -0500
committerAshwini Sharma <ak.ashwini1981@gmail.com>2014-05-29 08:18:50 -0500
commit877884d2bf8f9d4b48afcfd1a88680fb363c27ad (patch)
treea92b28469a5bcda7faf8dd07281f8a4c8262f5aa /toys/pending/strings.c
parent414c0cf9f5e91f7d755ba935595bbd7f3fd7c29e (diff)
downloadtoybox-877884d2bf8f9d4b48afcfd1a88680fb363c27ad.tar.gz
strings - print the strings in the file.
Diffstat (limited to 'toys/pending/strings.c')
-rw-r--r--toys/pending/strings.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/toys/pending/strings.c b/toys/pending/strings.c
new file mode 100644
index 00000000..1b53d8df
--- /dev/null
+++ b/toys/pending/strings.c
@@ -0,0 +1,66 @@
+/*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
+
+USE_STRINGS(NEWTOY(strings, "n#=4<1fo", TOYFLAG_USR|TOYFLAG_BIN))
+
+config STRINGS
+ bool "strings"
+ default n
+ help
+ usage: strings [-fo] [-n count] [FILE] ...
+
+ Display printable strings in a binary file
+
+ -f Precede strings with filenames
+ -n [LEN] 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, count = 0;
+ off_t offset = 0;
+ char *string;
+
+ string = xzalloc(TT.num + 1);
+ wlen = TT.num - 1;
+
+ while ((nread = read(fd,toybuf,sizeof(toybuf)))>0 ) {
+ for (i = 0; i < nread; i++, offset++) {
+ if (((toybuf[i] >= 32) && (toybuf[i] <= 126)) || (toybuf[i] == '\t')) {
+ if (count > wlen) xputc(toybuf[i]);
+ else {
+ string[count] = toybuf[i];
+ if (count == wlen) {
+ if (toys.optflags & FLAG_f) xprintf("%s: ", filename);
+ if (toys.optflags & FLAG_o)
+ xprintf("%7lld ",(long long)(offset - wlen));
+ xprintf("%s",string);
+ }
+ count++;
+ }
+ } else {
+ if (count > wlen) xputc('\n');
+ count = 0;
+ }
+ }
+ }
+ xclose(fd);
+ free(string);
+}
+
+void strings_main(void)
+{
+ loopfiles(toys.optargs, do_strings);
+}
+