Task

Find the name of the host on which the routine is running.




implement Hostname;
 
include "sys.m"; sys: Sys;
include "draw.m";
 
Hostname: module {
	init: fn(nil: ref Draw->Context, nil: list of string);
};
 
init(nil: ref Draw->Context, nil: list of string)
{
	sys = load Sys Sys->PATH;
	buf := array[Sys->ATOMICIO] of byte;
 
	fd := sys->open("/dev/sysname", Sys->OREAD);
	if(fd == nil)
		die("Couldn't open /dev/sysname");
 
	n := sys->read(fd, buf, len buf - 1);
	if(n < 1)
		die("Couldn't read /dev/sysname");
 
	buf[n++] = byte '\n';
	sys->write(sys->fildes(1), buf, n);
}
 
die(s: string)
{
	sys->fprint(sys->fildes(2), "hostname: %s: %r", s);
	raise "fail:errors";
}
 