diff options
| author | forsyth <forsyth@vitanuova.com> | 2010-08-02 14:49:50 +0100 |
|---|---|---|
| committer | forsyth <forsyth@vitanuova.com> | 2010-08-02 14:49:50 +0100 |
| commit | 66f5808b81b1df84bc57c4f7b9d487201bc162fb (patch) | |
| tree | fe09448075dcf50ecca78a673e16ad84a666d3da /appl/cmd/cat.b | |
| parent | 7781741266783e4df3b35d42a55e8e504838898b (diff) | |
20100802-1449
Diffstat (limited to 'appl/cmd/cat.b')
| -rw-r--r-- | appl/cmd/cat.b | 51 |
1 files changed, 21 insertions, 30 deletions
diff --git a/appl/cmd/cat.b b/appl/cmd/cat.b index 24d62372..5b9e3b5b 100644 --- a/appl/cmd/cat.b +++ b/appl/cmd/cat.b @@ -1,6 +1,8 @@ implement Cat; include "sys.m"; + sys: Sys; + include "draw.m"; Cat: module @@ -8,50 +10,39 @@ Cat: module init: fn(ctxt: ref Draw->Context, argv: list of string); }; -sys: Sys; stdout: ref Sys->FD; -init(nil: ref Draw->Context, argl: list of string) +init(nil: ref Draw->Context, args: list of string) { sys = load Sys Sys->PATH; - stdout = sys->fildes(1); - - argl = tl argl; - if(argl == nil) - argl = "-" :: nil; - while(argl != nil) { - cat(hd argl); - argl = tl argl; + args = tl args; + if(args == nil) + args = "-" :: nil; + for(; args != nil; args = tl args){ + file := hd args; + if(file != "-"){ + fd := sys->open(file, Sys->OREAD); + if(fd == nil){ + sys->fprint(sys->fildes(2), "cat: cannot open %s: %r\n", file); + raise "fail:bad open"; + } + cat(fd, file); + }else + cat(sys->fildes(0), "<stdin>"); } } -cat(file: string) +cat(fd: ref Sys->FD, file: string) { - n: int; - fd: ref Sys->FD; - buf := array[8192] of byte; - - if(file == "-") - fd = sys->fildes(0); - else { - fd = sys->open(file, sys->OREAD); - if(fd == nil) { - sys->fprint(sys->fildes(2), "cat: cannot open %s: %r\n", file); - raise "fail:bad open"; - } - } - for(;;) { - n = sys->read(fd, buf, len buf); - if(n <= 0) - break; + buf := array[Sys->ATOMICIO] of byte; + while((n := sys->read(fd, buf, len buf)) > 0) if(sys->write(stdout, buf, n) < n) { sys->fprint(sys->fildes(2), "cat: write error: %r\n"); raise "fail:write error"; } - } if(n < 0) { - sys->fprint(sys->fildes(2), "cat: read error: %r\n"); + sys->fprint(sys->fildes(2), "cat: error reading %s: %r\n", file); raise "fail:read error"; } } |
