blob: 19ed8b7190da59ea821531bdb659bddb526a0602 [file] [log] [blame]
rscbc7cb1a2003-11-23 18:04:47 +00001#include <u.h>
2#include <libc.h>
3#include <bio.h>
4
5Biobuf *fin;
6Biobuf fout;
7
8#define MINSPAN 6 /* Min characters in string */
9
10#define BUFSIZE 70
11
12void stringit(char *);
rscb4a659b2004-04-19 23:03:46 +000013#undef isprint
14#define isprint risprint
rscbc7cb1a2003-11-23 18:04:47 +000015int isprint(Rune);
16
17void
18main(int argc, char **argv)
19{
20 int i;
21
22 Binit(&fout, 1, OWRITE);
23 if(argc < 2) {
24 stringit("/fd/0");
25 exits(0);
26 }
27
28 for(i = 1; i < argc; i++) {
29 if(argc > 2)
30 print("%s:\n", argv[i]);
31
32 stringit(argv[i]);
33 }
34
35 exits(0);
36}
37
38void
39stringit(char *str)
40{
41 long posn, start;
42 int cnt = 0;
43 long c;
44
45 Rune buf[BUFSIZE];
46
47 if ((fin = Bopen(str, OREAD)) == 0) {
48 perror("open");
49 return;
50 }
51
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 }
75
76 if(cnt >= MINSPAN){
77 buf[cnt] = 0;
78 Bprint(&fout, "%8ld: %S\n", start, buf);
79 }
80 Bterm(fin);
81}
82
83int
84isprint(Rune r)
85{
86 if ((r >= ' ' && r <0x7f) || r > 0xA0)
87 return 1;
88 else
89 return 0;
90}