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;
}
}
}
|