diff options
author | cinap_lenrek <cinap_lenrek@felloff.net> | 2014-05-30 04:05:18 +0200 |
---|---|---|
committer | cinap_lenrek <cinap_lenrek@felloff.net> | 2014-05-30 04:05:18 +0200 |
commit | 17d0dea87c80203aaf0199cb33dea0afc4a7f956 (patch) | |
tree | 32a4bd2bf3230b33094666c1254a8faa13f17a88 /sys/src | |
parent | 52464166216095a5747f7541eff179ecf8d94e1f (diff) |
we look for strings.c, it is broken, this strings.c will make us go.
Diffstat (limited to 'sys/src')
-rw-r--r-- | sys/src/cmd/strings.c | 91 |
1 files changed, 44 insertions, 47 deletions
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 <libc.h> #include <bio.h> -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(cnt >= minspan){ - buf[cnt] = 0; - Bprint(&fout, "%8ld: %S\n", start, buf); + if(sp == nil) + Bputrune(&fout, '\n'); + sp = span; + } } - Bterm(fin); + if(sp == nil) + Bputrune(&fout, '\n'); + Bterm(&fin); } int |