we look for strings.c, it is broken, this strings.c will make us go.

This commit is contained in:
cinap_lenrek 2014-05-30 04:05:18 +02:00
parent 5246416621
commit 17d0dea87c
2 changed files with 46 additions and 51 deletions

View file

@ -24,12 +24,10 @@ Printable characters are taken to be
.SM ASCII .SM ASCII
characters from blank through tilde (hexadecimal 20 through 7E), inclusive, characters from blank through tilde (hexadecimal 20 through 7E), inclusive,
and and
all other characters from value 00A0 to FFFF. all other characters above A0.
Strings reports Strings reports
the decimal offset within the file at which the string starts and the text 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 of the string.
terminated by three dots and the printing is resumed on the next
line with the offset of the continuation line.
.SH SOURCE .SH SOURCE
.B /sys/src/cmd/strings.c .B /sys/src/cmd/strings.c
.SH SEE ALSO .SH SEE ALSO

View file

@ -2,16 +2,14 @@
#include <libc.h> #include <libc.h>
#include <bio.h> #include <bio.h>
Biobuf *fin; Biobuf fin;
Biobuf fout; Biobuf fout;
#define MINSPAN 6 /* Min characters in string (default) */ void stringit(int);
#define BUFSIZE 70
void stringit(char *);
int isprint(Rune); int isprint(Rune);
static int minspan = MINSPAN; int minspan = 6; /* Min characters in string (default) */
Rune *span;
static void static void
usage(void) usage(void)
@ -23,75 +21,74 @@ usage(void)
void void
main(int argc, char **argv) main(int argc, char **argv)
{ {
int i; int i, fd;
ARGBEGIN{ ARGBEGIN{
case 'm': case 'm':
minspan = atoi(EARGF(usage())); minspan = atoi(EARGF(usage()));
if(minspan <= 0)
usage();
break; break;
default: default:
usage(); usage();
break; break;
}ARGEND }ARGEND
span = malloc(sizeof(Rune)*(minspan+1));
if(span == nil)
sysfatal("out of memory");
Binit(&fout, 1, OWRITE); Binit(&fout, 1, OWRITE);
if(argc < 1) { if(argc < 1) {
stringit("/fd/0"); stringit(0);
exits(0); exits(0);
} }
for(i = 0; i < argc; i++) { for(i = 0; i < argc; i++) {
if(argc > 2) if(argc > 1){
print("%s:\n", argv[i]); Bprint(&fout, "%s:\n", argv[i]);
Bflush(&fout);
stringit(argv[i]); }
if((fd = open(argv[i], OREAD)) < 0){
perror("open");
continue;
}
stringit(fd);
close(fd);
} }
exits(0); exits(0);
} }
void void
stringit(char *str) stringit(int fd)
{ {
long posn, start; Rune *sp;
int cnt = 0;
long c; long c;
Rune buf[BUFSIZE]; Binit(&fin, fd, OREAD);
sp = span;
if ((fin = Bopen(str, OREAD)) == 0) { while((c = Bgetrune(&fin)) >= 0) {
perror("open");
return;
}
start = 0;
posn = Boffset(fin);
while((c = Bgetrune(fin)) >= 0) {
if(isprint(c)) { if(isprint(c)) {
if(start == 0) if(sp == nil){
start = posn; Bputrune(&fout, c);
buf[cnt++] = c; continue;
if(cnt == BUFSIZE-1) {
buf[cnt] = 0;
Bprint(&fout, "%8ld: %S ...\n", start, buf);
start = 0;
cnt = 0;
} }
*sp++ = c;
if((sp-span) < minspan)
continue;
*sp = 0;
Bprint(&fout, "%8lld: %S", Boffset(&fin)-minspan, span);
sp = nil;
} else { } else {
if(cnt >= minspan) { if(sp == nil)
buf[cnt] = 0; Bputrune(&fout, '\n');
Bprint(&fout, "%8ld: %S\n", start, buf); sp = span;
} }
start = 0;
cnt = 0;
}
posn = Boffset(fin);
} }
if(sp == nil)
if(cnt >= minspan){ Bputrune(&fout, '\n');
buf[cnt] = 0; Bterm(&fin);
Bprint(&fout, "%8ld: %S\n", start, buf);
}
Bterm(fin);
} }
int int