xref: /inferno-os/appl/cmd/echo.b (revision 37da2899f40661e3e9631e497da8dc59b971cbd0)
1implement Echo;
2
3include "sys.m";
4	sys: Sys;
5include "draw.m";
6
7Echo: module
8{
9	init:	fn(nil: ref Draw->Context, nil: list of string);
10};
11
12init(nil: ref Draw->Context, args: list of string)
13{
14	sys = load Sys Sys->PATH;
15	if(args != nil)
16		args = tl args;
17	addnl := 1;
18	if(args != nil && (hd args == "-n" || hd args == "--")) {
19		if(hd args == "-n")
20			addnl = 0;
21		args = tl args;
22	}
23	s := "";
24	if(args != nil) {
25		s = hd args;
26		while((args = tl args) != nil)
27			s += " " + hd args;
28	}
29	if(addnl)
30		s[len s] = '\n';
31	a := array of byte s;
32	if(sys->write(sys->fildes(1), a, len a) < 0){
33		sys->fprint(sys->fildes(2), "echo: write error: %r\n");
34		raise "fail:write error";
35	}
36}
37