Hi,

Added a barebones strings(1) implementation.  Let me know
if you guys want this.

Thanks,
sin
>From 311992f992c040caf2347c21e35757d713ca7c8d Mon Sep 17 00:00:00 2001
From: sin <s...@2f30.org>
Date: Wed, 14 Aug 2013 10:41:55 +0100
Subject: [PATCH] Add strings(1)

---
 Makefile  |  1 +
 strings.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+)
 create mode 100644 strings.c

diff --git a/Makefile b/Makefile
index 33c78a0..6bc091a 100644
--- a/Makefile
+++ b/Makefile
@@ -70,6 +70,7 @@ SRC = \
        sort.c     \
        split.c    \
        sponge.c   \
+       strings.c  \
        sync.c     \
        tail.c     \
        tar.c      \
diff --git a/strings.c b/strings.c
new file mode 100644
index 0000000..a2a2a06
--- /dev/null
+++ b/strings.c
@@ -0,0 +1,70 @@
+/* See LICENSE file for copyright and license details. */
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include "util.h"
+
+static void dostrings(int fd, const char *fname);
+
+static void
+usage(void)
+{
+       eprintf("usage: %s file\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+       int fd;
+
+       ARGBEGIN {
+       default:
+               usage();
+       } ARGEND;
+
+       if (argc > 0) {
+               if ((fd = open(argv[0], O_RDONLY)) < 0)
+                       eprintf("open %s:", argv[0]);
+               dostrings(fd, argv[0]);
+               close(fd);
+       } else {
+               dostrings(STDIN_FILENO, "<stdin>");
+       }
+
+       return 0;
+}
+
+static void
+dostrings(int fd, const char *fname)
+{
+       ssize_t r;
+       unsigned char buf[BUFSIZ], c;
+       int span = 0, i = 0;
+       off_t offset = 0;
+
+       while ((r = read(fd, &c, 1) > 0)) {
+               offset++;
+               if (isprint(c)) {
+                       span++;
+                       buf[i++] = c;
+                       if (i == sizeof(buf) - 1) {
+                               printf("%8ld: %.*s\n",
+                                      (long)offset - i - 1,
+                                      i, buf);
+                               span = i = 0;
+                       }
+               } else {
+                       if (span >= 6) {
+                               printf("%8ld: %.*s\n",
+                                      (long)offset - i - 1,
+                                      i, buf);
+                       }
+                       span = i = 0;
+               }
+       }
+       if (r < 0)
+               eprintf("%s: read error:", fname);
+}
-- 
1.8.2.3

Reply via email to