Blob
1 #include <u.h>2 #include <libc.h>3 #include <bio.h>5 Biobuf *fin;6 Biobuf fout;8 #define MINSPAN 6 /* Min characters in string */10 #define BUFSIZE 7012 void stringit(char *);13 int isprint(Rune);15 void16 main(int argc, char **argv)17 {18 int i;20 Binit(&fout, 1, OWRITE);21 if(argc < 2) {22 stringit("/fd/0");23 exits(0);24 }26 for(i = 1; i < argc; i++) {27 if(argc > 2)28 print("%s:\n", argv[i]);30 stringit(argv[i]);31 }33 exits(0);34 }36 void37 stringit(char *str)38 {39 long posn, start;40 int cnt = 0;41 long c;43 Rune buf[BUFSIZE];45 if ((fin = Bopen(str, OREAD)) == 0) {46 perror("open");47 return;48 }50 start = 0;51 posn = Boffset(fin);52 while((c = Bgetrune(fin)) >= 0) {53 if(isprint(c)) {54 if(start == 0)55 start = posn;56 buf[cnt++] = c;57 if(cnt == BUFSIZE-1) {58 buf[cnt] = 0;59 Bprint(&fout, "%8ld: %S ...\n", start, buf);60 start = 0;61 cnt = 0;62 }63 } else {64 if(cnt >= MINSPAN) {65 buf[cnt] = 0;66 Bprint(&fout, "%8ld: %S\n", start, buf);67 }68 start = 0;69 cnt = 0;70 }71 posn = Boffset(fin);72 }74 if(cnt >= MINSPAN){75 buf[cnt] = 0;76 Bprint(&fout, "%8ld: %S\n", start, buf);77 }78 Bterm(fin);79 }81 int82 isprint(Rune r)83 {84 if ((r >= ' ' && r <0x7f) || r > 0xA0)85 return 1;86 else87 return 0;88 }