From 17d0dea87c80203aaf0199cb33dea0afc4a7f956 Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Fri, 30 May 2014 04:05:18 +0200 Subject: [PATCH] we look for strings.c, it is broken, this strings.c will make us go. --- sys/man/1/strings | 6 +-- sys/src/cmd/strings.c | 91 +++++++++++++++++++++---------------------- 2 files changed, 46 insertions(+), 51 deletions(-) diff --git a/sys/man/1/strings b/sys/man/1/strings index 9164194bd..e8f862e29 100644 --- a/sys/man/1/strings +++ b/sys/man/1/strings @@ -24,12 +24,10 @@ Printable characters are taken to be .SM ASCII characters from blank through tilde (hexadecimal 20 through 7E), inclusive, and -all other characters from value 00A0 to FFFF. +all other characters above A0. Strings reports the decimal offset within the file at which the string starts and the text -of the string. If the string is longer than 70 runes the line is -terminated by three dots and the printing is resumed on the next -line with the offset of the continuation line. +of the string. .SH SOURCE .B /sys/src/cmd/strings.c .SH SEE ALSO diff --git a/sys/src/cmd/strings.c b/sys/src/cmd/strings.c index 67cf464ff..1cba97e5a 100644 --- a/sys/src/cmd/strings.c +++ b/sys/src/cmd/strings.c @@ -2,16 +2,14 @@ #include #include -Biobuf *fin; +Biobuf fin; Biobuf fout; -#define MINSPAN 6 /* Min characters in string (default) */ -#define BUFSIZE 70 - -void stringit(char *); +void stringit(int); int isprint(Rune); -static int minspan = MINSPAN; +int minspan = 6; /* Min characters in string (default) */ +Rune *span; static void usage(void) @@ -23,75 +21,74 @@ usage(void) void main(int argc, char **argv) { - int i; + int i, fd; ARGBEGIN{ case 'm': minspan = atoi(EARGF(usage())); + if(minspan <= 0) + usage(); break; default: usage(); break; }ARGEND + + span = malloc(sizeof(Rune)*(minspan+1)); + if(span == nil) + sysfatal("out of memory"); + Binit(&fout, 1, OWRITE); if(argc < 1) { - stringit("/fd/0"); + stringit(0); exits(0); } for(i = 0; i < argc; i++) { - if(argc > 2) - print("%s:\n", argv[i]); - - stringit(argv[i]); + if(argc > 1){ + Bprint(&fout, "%s:\n", argv[i]); + Bflush(&fout); + } + if((fd = open(argv[i], OREAD)) < 0){ + perror("open"); + continue; + } + stringit(fd); + close(fd); } exits(0); } void -stringit(char *str) +stringit(int fd) { - long posn, start; - int cnt = 0; + Rune *sp; long c; - Rune buf[BUFSIZE]; - - if ((fin = Bopen(str, OREAD)) == 0) { - perror("open"); - return; - } - - start = 0; - posn = Boffset(fin); - while((c = Bgetrune(fin)) >= 0) { + Binit(&fin, fd, OREAD); + sp = span; + while((c = Bgetrune(&fin)) >= 0) { if(isprint(c)) { - if(start == 0) - start = posn; - buf[cnt++] = c; - if(cnt == BUFSIZE-1) { - buf[cnt] = 0; - Bprint(&fout, "%8ld: %S ...\n", start, buf); - start = 0; - cnt = 0; + if(sp == nil){ + Bputrune(&fout, c); + continue; } + *sp++ = c; + if((sp-span) < minspan) + continue; + *sp = 0; + Bprint(&fout, "%8lld: %S", Boffset(&fin)-minspan, span); + sp = nil; } else { - if(cnt >= minspan) { - buf[cnt] = 0; - Bprint(&fout, "%8ld: %S\n", start, buf); - } - start = 0; - cnt = 0; - } - posn = Boffset(fin); + if(sp == nil) + Bputrune(&fout, '\n'); + sp = span; + } } - - if(cnt >= minspan){ - buf[cnt] = 0; - Bprint(&fout, "%8ld: %S\n", start, buf); - } - Bterm(fin); + if(sp == nil) + Bputrune(&fout, '\n'); + Bterm(&fin); } int