summaryrefslogtreecommitdiff
path: root/appl/cmd/cat.b
diff options
context:
space:
mode:
Diffstat (limited to 'appl/cmd/cat.b')
-rw-r--r--appl/cmd/cat.b51
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";
}
}