diff options
| author | Charles.Forsyth <devnull@localhost> | 2006-12-22 17:07:39 +0000 |
|---|---|---|
| committer | Charles.Forsyth <devnull@localhost> | 2006-12-22 17:07:39 +0000 |
| commit | 37da2899f40661e3e9631e497da8dc59b971cbd0 (patch) | |
| tree | cbc6d4680e347d906f5fa7fca73214418741df72 /appl/cmd/read.b | |
| parent | 54bc8ff236ac10b3eaa928fd6bcfc0cdb2ba46ae (diff) | |
20060303a
Diffstat (limited to 'appl/cmd/read.b')
| -rw-r--r-- | appl/cmd/read.b | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/appl/cmd/read.b b/appl/cmd/read.b new file mode 100644 index 00000000..a4a008a9 --- /dev/null +++ b/appl/cmd/read.b @@ -0,0 +1,62 @@ +implement Read; +include "sys.m"; + sys: Sys; +include "draw.m"; + +Read: module { + init: fn(nil: ref Draw->Context, argv: list of string); +}; + +usage() +{ + sys->fprint(sys->fildes(2), "usage: read [-[ero] offset] count\n"); + raise "fail:usage"; +} + +init(nil: ref Draw->Context, argv: list of string) +{ + sys = load Sys Sys->PATH; + # usage: read [-[ero] offset] count + count := Sys->ATOMICIO; + offset := big 0; + seeking := -1; + if (argv != nil) + argv = tl argv; + if (argv != nil && hd argv != nil && (hd argv)[0] == '-') { + if (tl argv == nil) + usage(); + case hd argv { + "-o" => + seeking = Sys->SEEKSTART; + "-e" => + seeking = Sys->SEEKEND; + "-r" => + seeking = Sys->SEEKRELA; + * => + usage(); + } + offset = big hd tl argv; + argv = tl tl argv; + } + if (argv != nil) { + if (tl argv != nil) + usage(); + count = int hd argv; + } + fd := sys->fildes(0); + if (seeking != -1) + sys->seek(fd, offset, seeking); + if (count == 0) + return; + buf := array[count] of byte; + n := sys->read(fd, buf, len buf); + if (n > 0) + sys->write(sys->fildes(1), buf, n); + else { + if (n == -1) { + sys->fprint(sys->fildes(2), "read: read error: %r\n"); + raise "fail:error"; + } + raise "fail:eof"; + } +} |
