[hackers] [sbase] strings: Add -n len support || sin

From: <git_AT_suckless.org>
Date: Tue, 17 Feb 2015 14:50:40 +0100 (CET)

commit 6116d07d35806108dd5d27f1a7d0f269ec379539
Author: sin <sin_AT_2f30.org>
Date: Tue Feb 17 13:46:48 2015 +0000

    strings: Add -n len support

diff --git a/README b/README
index 5203db1..49f681f 100644
--- a/README
+++ b/README
_AT_@ -67,7 +67,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
    sort no -m, -o, -d, -f, -i
 =* split yes none
 =* sponge non-posix none
- strings no -n, -t
+ strings no -t
 =* sync non-posix none
 =* tail yes none
 =* tar non-posix none
diff --git a/strings.1 b/strings.1
index ec534d3..3568a6a 100644
--- a/strings.1
+++ b/strings.1
_AT_@ -7,6 +7,7 @@
 .Sh SYNOPSIS
 .Nm
 .Op Fl a
+.Op Fl n Ar len
 .Op Ar file ...
 .Sh DESCRIPTION
 .Nm
_AT_@ -20,6 +21,10 @@ reads from stdin.
 .Bl -tag -width Ds
 .It Fl a
 Scan files in their entirety. This is the default.
+.It Fl n Ar len
+Only print sequences that are at least
+.Ar len
+characters. The default is 4 characters.
 .El
 .Sh STANDARDS
 .Nm
diff --git a/strings.c b/strings.c
index 59f3363..c380752 100644
--- a/strings.c
+++ b/strings.c
_AT_@ -1,15 +1,35 @@
 /* See LICENSE file for copyright and license details. */
 #include <ctype.h>
 #include <stdio.h>
+#include <stdlib.h>
 
 #include "util.h"
 
-static void dostrings(FILE *fp, const char *fname);
+static void
+strings(FILE *fp, const char *fname, int len)
+{
+ unsigned char buf[BUFSIZ];
+ int c, i = 0;
+ off_t offset = 0;
+
+ do {
+ offset++;
+ if (isprint(c = getc(fp)))
+ buf[i++] = c;
+ if ((!isprint(c) && i >= len) || i == sizeof(buf) - 1) {
+ buf[i] = '\0';
+ printf("%8ld: %s\n", (long)offset - i - 1, buf);
+ i = 0;
+ }
+ } while (c != EOF);
+ if (ferror(fp))
+ eprintf("%s: read error:", fname);
+}
 
 static void
 usage(void)
 {
- eprintf("usage: %s [-a] [file ...]\n", argv0);
+ eprintf("usage: %s [-a] [-n len] [file ...]\n", argv0);
 }
 
 int
_AT_@ -17,16 +37,20 @@ main(int argc, char *argv[])
 {
         FILE *fp;
         int ret = 0;
+ int len = 4;
 
         ARGBEGIN {
         case 'a':
                 break;
+ case 'n':
+ len = estrtonum(EARGF(usage()), 1, INT_MAX);
+ break;
         default:
                 usage();
         } ARGEND;
 
         if (argc == 0) {
- dostrings(stdin, "<stdin>");
+ strings(stdin, "<stdin>", len);
         } else {
                 for (; argc > 0; argc--, argv++) {
                         if (!(fp = fopen(argv[0], "r"))) {
_AT_@ -34,30 +58,9 @@ main(int argc, char *argv[])
                                 ret = 1;
                                 continue;
                         }
- dostrings(fp, argv[0]);
+ strings(fp, argv[0], len);
                         fclose(fp);
                 }
         }
         return ret;
 }
-
-static void
-dostrings(FILE *fp, const char *fname)
-{
- unsigned char buf[BUFSIZ];
- int c, i = 0;
- off_t offset = 0;
-
- do {
- offset++;
- if (isprint(c = getc(fp)))
- buf[i++] = c;
- if ((!isprint(c) && i >= 6) || i == sizeof(buf) - 1) {
- buf[i] = '\0';
- printf("%8ld: %s\n", (long)offset - i - 1, buf);
- i = 0;
- }
- } while (c != EOF);
- if (ferror(fp))
- eprintf("%s: read error:", fname);
-}
Received on Tue Feb 17 2015 - 14:50:40 CET

This archive was generated by hypermail 2.3.0 : Tue Feb 17 2015 - 15:00:16 CET