Plan 9 from Bell Labs’s /usr/web/sources/contrib/nemo/octopus/port/lib/io.b

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


implement Io;
include "sys.m";
	sys: Sys;
include "io.m";

init()
{
	if (sys != nil)
		return;
	sys = load Sys Sys->PATH;
}

readn(fd: ref Sys->FD, buf: array of byte, nb: int): int
{
	init();
	for(nr := 0; nr < nb;){
		n := sys->read(fd, buf[nr:], nb-nr);
		if(n <= 0){
			if(nr == 0)
				return n;
			break;
		}
		nr += n;
	}
	return nr;
}

readfile(fd: ref Sys->FD): array of byte
{
	init();
	buf := array[1024] of byte;
	tot := 0;
	for(;;){
		nr := sys->read(fd, buf[tot:], len buf - tot);
		if (nr < 0)
			return nil;
		if (nr == 0)
			return buf[0:tot];
		tot += nr;
		if (tot > 64 * 1024 * 1024){
			sys->fprint(sys->fildes(2), "Io: file too large. fix me.\n");
			return nil;
		}
		if (tot == len buf){
			nbuf := array[2 * len buf] of byte;
			nbuf[0:] = buf;
			buf = nbuf;
		}
	}
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].