summaryrefslogtreecommitdiff
path: root/appl/cmd/cat.b
blob: 5b9e3b5ba83eef30ed578db01a0a2dd7bf197ce2 (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
implement Cat;

include "sys.m";
	sys: Sys;

include "draw.m";

Cat: module
{
	init:	fn(ctxt: ref Draw->Context, argv: list of string);
};

stdout: ref Sys->FD;

init(nil: ref Draw->Context, args: list of string)
{
	sys = load Sys Sys->PATH;
	stdout = sys->fildes(1);
	args = tl args;
	if(args == nil)
		args = "-" :: nil;
	for(; args != nil; args = tl args){
		file := hd args;
		if(file != "-"){
			fd := sys->open(file, Sys->OREAD);
			if(fd == nil){
				sys->fprint(sys->fildes(2), "cat: cannot open %s: %r\n", file);
				raise "fail:bad open";
			}
			cat(fd, file);
		}else
			cat(sys->fildes(0), "<stdin>");
	}
}

cat(fd: ref Sys->FD, file: string)
{
	buf := array[Sys->ATOMICIO] of byte;
	while((n := sys->read(fd, buf, len buf)) > 0)
		if(sys->write(stdout, buf, n) < n) {
			sys->fprint(sys->fildes(2), "cat: write error: %r\n");
			raise "fail:write error";
		}
	if(n < 0) {
		sys->fprint(sys->fildes(2), "cat: error reading %s: %r\n", file);
		raise "fail:read error";
	}
}