shithub: 9ferno

Download patch

ref: 819fc1850e53f5094cc06431ed719759dd685885
parent: 7d3fb4d665372fe975403ff80f32500204454d3b
author: 9ferno <gophone2015@gmail.com>
date: Mon Jan 10 21:16:09 EST 2022

read takes a file argument

--- a/appl/cmd/read.b	Mon Jan 10 19:57:56 2022
+++ b/appl/cmd/read.b	Mon Jan 10 21:16:09 2022
@@ -9,7 +9,7 @@
 
 usage()
 {
-	sys->fprint(sys->fildes(2), "usage: read [-[ero] offset] count\n");
+	sys->fprint(sys->fildes(2), "usage: read [-[ero] offset] count [file]\n");
 	raise "fail:usage";
 }
 
@@ -20,6 +20,7 @@
 	count := Sys->ATOMICIO;
 	offset := big 0;
 	seeking := -1;
+	fd := sys->fildes(0);
 	if (argv != nil)
 		argv = tl argv;
 	if (argv != nil && hd argv != nil && (hd argv)[0] == '-') {
@@ -39,11 +40,16 @@
 		argv = tl tl argv;
 	}
 	if (argv != nil) {
-		if (tl argv != nil)
-			usage();
 		count = int hd argv;
+		if (tl argv != nil){
+			file := hd tl argv; # usage();
+			fd = sys->open(file, Sys->OREAD);
+			if(fd == nil){
+				sys->fprint(sys->fildes(2), "read: cannot open %s: %r\n", file);
+				raise "fail:bad open";
+			}
+		}
 	}
-	fd := sys->fildes(0);
 	if (seeking != -1)
 		sys->seek(fd, offset, seeking);
 	if (count == 0)