From aec09c0f4579f7677e0c34fe55dc49b352b5aeab Mon Sep 17 00:00:00 2001 From: Niels Provos Date: Wed, 23 Feb 2000 19:44:09 +0000 Subject: new -m number parameter to specify a maximum length of strings to match, ok millert@ deraadt@ --- usr.bin/strings/strings.1 | 7 ++++++- usr.bin/strings/strings.c | 47 +++++++++++++++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/usr.bin/strings/strings.1 b/usr.bin/strings/strings.1 index 1ca6a5d40fa..d598dd403e4 100644 --- a/usr.bin/strings/strings.1 +++ b/usr.bin/strings/strings.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: strings.1,v 1.4 1999/06/05 01:21:41 aaron Exp $ +.\" $OpenBSD: strings.1,v 1.5 2000/02/23 19:44:08 provos Exp $ .\" $NetBSD: strings.1,v 1.4 1994/12/10 11:54:28 jtc Exp $ .\" .\" Copyright (c) 1980, 1990, 1993 @@ -44,6 +44,7 @@ .Nm strings .Op Fl afo .Op Fl n Ar number +.Op Fl m Ar number .Op Fl t Ar radix .Op Ar file ... .Sh DESCRIPTION @@ -71,6 +72,10 @@ in which it was found. Specifies the minimum number of characters in a sequence to be .Ar number , instead of four. +.It Fl m Ar number +Specifies the maximum number of characters in a sequence to be +.Ar number , +instead of unlimited. .It Fl o Each string is preceded by its octal offset in the file. .It Fl t Ar radix diff --git a/usr.bin/strings/strings.c b/usr.bin/strings/strings.c index a730c078b7b..992ee946fa7 100644 --- a/usr.bin/strings/strings.c +++ b/usr.bin/strings/strings.c @@ -1,4 +1,4 @@ -/* $OpenBSD: strings.c,v 1.4 1997/09/11 11:21:54 deraadt Exp $ */ +/* $OpenBSD: strings.c,v 1.5 2000/02/23 19:44:08 provos Exp $ */ /* $NetBSD: strings.c,v 1.7 1995/02/15 15:49:19 jtc Exp $ */ /* @@ -44,7 +44,7 @@ static char copyright[] = #if 0 static char sccsid[] = "@(#)strings.c 8.2 (Berkeley) 1/28/94"; #endif -static char rcsid[] = "$OpenBSD: strings.c,v 1.4 1997/09/11 11:21:54 deraadt Exp $"; +static char rcsid[] = "$OpenBSD: strings.c,v 1.5 2000/02/23 19:44:08 provos Exp $"; #endif /* not lint */ #include @@ -87,7 +87,7 @@ main(argc, argv) register int ch, cnt; register u_char *C; EXEC *head; - int exitcode, minlen; + int exitcode, minlen, maxlen, bfrlen; short asdata, fflg; u_char *bfr; char *file, *p; @@ -102,7 +102,8 @@ main(argc, argv) asdata = exitcode = fflg = 0; offset_format = NULL; minlen = -1; - while ((ch = getopt(argc, argv, "-0123456789an:oft:")) != -1) + maxlen = -1; + while ((ch = getopt(argc, argv, "-0123456789an:m:oft:")) != -1) switch((char)ch) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': @@ -128,6 +129,9 @@ main(argc, argv) case 'n': minlen = atoi(optarg); break; + case 'm': + maxlen = atoi(optarg); + break; case 'o': offset_format = FORMAT_OCT; break; @@ -160,12 +164,17 @@ main(argc, argv) (void)fprintf(stderr, "strings: length less than 1\n"); exit (1); } - - if (!(bfr = malloc(minlen + 1))) { + if (maxlen != -1 && maxlen < minlen) { + (void)fprintf(stderr, "strings: max length less than min\n"); + exit (1); + } + bfrlen = maxlen == -1 ? minlen : maxlen; + bfr = malloc(bfrlen + 1); + if (!bfr) { (void)fprintf(stderr, "strings: %s\n", strerror(errno)); exit(1); } - bfr[minlen] = '\0'; + bfr[bfrlen] = '\0'; file = "stdin"; do { if (*argv) { @@ -205,6 +214,21 @@ start: *C++ = ch; if (++cnt < minlen) continue; + if (maxlen != -1) { + while ((ch = getch()) != EOF && + ISSTR(ch) && cnt++ < maxlen) + *C++ = ch; + if (ch == EOF || + (ch != 0 && ch != '\n')) { + /* get all of too big string */ + while ((ch = getch()) != EOF && + ISSTR(ch)) + ; + ungetc(ch, stdin); + goto out; + } + *C = 0; + } if (fflg) printf("%s:", file); @@ -213,10 +237,13 @@ start: printf(offset_format, foff - minlen); printf("%s", bfr); - - while ((ch = getch()) != EOF && ISSTR(ch)) - putchar((char)ch); + + if (maxlen == -1) + while ((ch = getch()) != EOF && + ISSTR(ch)) + putchar((char)ch); putchar('\n'); + out: } cnt = 0; } -- cgit v1.2.3