aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/strings.c
blob: dbf57ff85f1a9c0da284456aa5eca6dc8b38939b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include	<u.h>
#include 	<libc.h>
#include	<bio.h>

Biobuf	*fin;
Biobuf	fout;

#define	MINSPAN		6		/* Min characters in string */

#define BUFSIZE		70

void stringit(char *);
int isprint(Rune);

void
main(int argc, char **argv)
{
	int i;

	Binit(&fout, 1, OWRITE);
	if(argc < 2) {
		stringit("/fd/0");
		exits(0);
	}

	for(i = 1; i < argc; i++) {
		if(argc > 2)
			print("%s:\n", argv[i]);

		stringit(argv[i]);
	}

	exits(0);
}

void
stringit(char *str)
{
	long posn, start;
	int cnt = 0;
	long c;

	Rune buf[BUFSIZE];

	if ((fin = Bopen(str, OREAD)) == 0) {
		perror("open");
		return;
	}

	start = 0;
	posn = Boffset(fin);
	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;
			}
		} 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);
	}
	Bterm(fin);
}

int
isprint(Rune r)
{
	if ((r >= ' ' && r <0x7f) || r > 0xA0)
		return 1;
	else
		return 0;
}