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 70
12 void stringit(char *);
13 #undef isprint
14 #define isprint risprint
15 int isprint(Rune);
17 void
18 main(int argc, char **argv)
19 {
20 int i;
22 Binit(&fout, 1, OWRITE);
23 if(argc < 2) {
24 stringit("/dev/stdin");
25 exits(0);
26 }
28 for(i = 1; i < argc; i++) {
29 if(argc > 2)
30 print("%s:\n", argv[i]);
32 stringit(argv[i]);
33 }
35 exits(0);
36 }
38 void
39 stringit(char *str)
40 {
41 long posn, start;
42 int cnt = 0;
43 long c;
45 Rune buf[BUFSIZE];
47 if ((fin = Bopen(str, OREAD)) == 0) {
48 perror("open");
49 return;
50 }
52 start = 0;
53 posn = Boffset(fin);
54 while((c = Bgetrune(fin)) >= 0) {
55 if(isprint(c)) {
56 if(start == 0)
57 start = posn;
58 buf[cnt++] = c;
59 if(cnt == BUFSIZE-1) {
60 buf[cnt] = 0;
61 Bprint(&fout, "%8ld: %S ...\n", start, buf);
62 start = 0;
63 cnt = 0;
64 }
65 } else {
66 if(cnt >= MINSPAN) {
67 buf[cnt] = 0;
68 Bprint(&fout, "%8ld: %S\n", start, buf);
69 }
70 start = 0;
71 cnt = 0;
72 }
73 posn = Boffset(fin);
74 }
76 if(cnt >= MINSPAN){
77 buf[cnt] = 0;
78 Bprint(&fout, "%8ld: %S\n", start, buf);
79 }
80 Bterm(fin);
81 }
83 int
84 isprint(Rune r)
85 {
86 if ((r >= ' ' && r <0x7f) || r > 0xA0)
87 return 1;
88 else
89 return 0;
90 }