summaryrefslogtreecommitdiff
path: root/sys/src/cmd/read.c
blob: e71a36d4f10730fc2633f44799af8fb101a9cd4a (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <u.h>
#include <libc.h>

int	multi;
vlong	count;
char	*status = nil;

int
line(int fd, char *file)
{
	char c;
	int m, n, nalloc;
	char *buf;

	nalloc = 0;
	buf = nil;
	for(m=0; ; ){
		n = read(fd, &c, 1);
		if(n < 0){
			fprint(2, "read: error reading %s: %r\n", file);
			exits("read error");
		}
		if(n == 0){
			if(m == 0)
				status = "eof";
			break;
		}
		if(m == nalloc){
			nalloc += 1024;
			buf = realloc(buf, nalloc);
			if(buf == nil){
				fprint(2, "read: malloc error: %r\n");
				exits("malloc");
			}
		}
		buf[m++] = c;
		if(c == '\n')
			break;
	}
	if(m > 0)
		write(1, buf, m);
	free(buf);
	return m;
}

void
lines(int fd, char *file)
{
	do{
		if(line(fd, file) == 0)
			break;
	}while(multi || --count > 0);
}

void
chars(int fd, char *file)
{
	char buf[8*1024];
	vlong m;
	int n;

	for(m = 0; m < count; m += n){
		n = sizeof(buf);
		if(n > (count - m))
			n = count - m;
		if((n = read(fd, buf, n)) < 0){
			fprint(2, "read: error reading %s: %r\n", file);
			exits("read error");
		}
		if(n == 0){
			if(m == 0)
				status = "eof";
			break;
		}
		write(1, buf, n);
	}
}

void
runes(int fd, char *file)
{
	char buf[8*1024], *s, *e;
	Rune r;

	while(count > 0){
		e = buf + read(fd, buf, count + UTFmax < sizeof buf ? count : sizeof buf - UTFmax);
		if(e < buf){
			fprint(2, "read: error reading %s: %r\n", file);
			exits("read error");
		}
		if(e == buf)
			break;
		for(s = buf; s < e && fullrune(s, e - s); s += chartorune(&r, s))
			count--;
		if(s < e){
			while(!fullrune(s, e - s))
				switch(read(fd, e, 1)){
				case -1:
					fprint(2, "read: error reading %s: %r\n", file);
					exits("read error");
				case 0:
					fprint(2, "warning: partial rune at end of %s: %r\n", file);
					write(1, buf, e - buf);
					return;
				case 1:
					e++;
					break;
				}
			count--;
		}
		write(1, buf, e - buf);
	}
}

void
usage(void)
{
	fprint(2, "usage: read [ -m | -n nlines | -c nbytes | -r nrunes ] [ file ... ]\n");
	exits("usage");
}

void
main(int argc, char *argv[])
{
	void (*proc)(int, char*);
	int i, fd;

	proc = lines;
	ARGBEGIN{
	case 'c':
		count = atoll(EARGF(usage()));
		proc = chars;
		break;
	case 'r':
		count = atoll(EARGF(usage()));
		proc = runes;
		break;
	case 'n':
		count = atoi(EARGF(usage()));
		break;
	case 'm':
		multi = 1;
		break;
	default:
		usage();
	}ARGEND

	if(argc == 0)
		(*proc)(0, "<stdin>");
	else
		for(i=0; i<argc; i++){
			fd = open(argv[i], OREAD);
			if(fd < 0){
				fprint(2, "read: can't open %s: %r\n", argv[i]);
				exits("open");
			}
			(*proc)(fd, argv[i]);
			close(fd);
		}

	exits(status);
}