commit 8aab07cc3ae68b6c8c4b464cf6100725622d8d35
parent 3c8a71297f7d74eb18f05aae75d9cfb06e79df0a
Author: Drew DeVault <sir@cmpwn.com>
Date: Fri, 11 Feb 2022 14:31:02 +0100
cat: fix - and -u
Signed-off-by: Drew DeVault <sir@cmpwn.com>
Diffstat:
M | cat.ha | | | 25 | +++++++++++++++++-------- |
1 file changed, 17 insertions(+), 8 deletions(-)
diff --git a/cat.ha b/cat.ha
@@ -6,7 +6,9 @@ use main;
use os;
export fn utilmain() (main::error | void) = {
- const cmd = getopt::parse(os::args);
+ const cmd = getopt::parse(os::args,
+ ('u', "POSIX compatibility, ignored"),
+ "[file...]");
defer getopt::finish(&cmd);
if (len(cmd.args) == 0) {
@@ -15,14 +17,21 @@ export fn utilmain() (main::error | void) = {
};
for (let i = 0z; i < len(cmd.args); i += 1z) {
- const file = match (os::open(cmd.args[i])) {
- case let err: fs::error =>
- fmt::fatal("Error opening '{}': {}",
- cmd.args[i], fs::strerror(err));
- case let file: io::file =>
- yield file;
- };
+ const file = open(cmd.args[i]);
defer io::close(file);
io::copy(os::stdout, file)?;
};
};
+
+fn open(path: str) io::handle = {
+ if (path == "-") {
+ return os::stdin;
+ };
+
+ match (os::open(path)) {
+ case let err: fs::error =>
+ fmt::fatal("Error opening '{}': {}", path, fs::strerror(err));
+ case let file: io::file =>
+ return file;
+ };
+};