hautils

[hare] Set of POSIX utilities
Log | Files | Refs | README | LICENSE

cat.ha (798B)


      1 use fmt;
      2 use fs;
      3 use getopt;
      4 use io;
      5 use main;
      6 use os;
      7 
      8 export fn utilmain() (main::error | void) = {
      9 	const cmd = getopt::parse(os::args,
     10 		('u', "POSIX compatibility, ignored"),
     11 		"[file...]");
     12 	defer getopt::finish(&cmd);
     13 
     14 	if (len(cmd.args) == 0) {
     15 		io::copy(os::stdout, os::stdin)?;
     16 		return;
     17 	};
     18 
     19 	for (let i = 0z; i < len(cmd.args); i += 1z) {
     20 		const file = open(cmd.args[i]);
     21 		match (io::copy(os::stdout, file)) {
     22 		case size => void;
     23 		case let err: io::error =>
     24 			io::close(file): void;
     25 			return err;
     26 		};
     27 		io::close(file)?;
     28 	};
     29 };
     30 
     31 fn open(path: str) io::handle = {
     32 	if (path == "-") {
     33 		return os::stdin;
     34 	};
     35 
     36 	match (os::open(path)) {
     37 	case let err: fs::error =>
     38 		fmt::fatalf("Error opening '{}': {}", path, fs::strerror(err));
     39 	case let file: io::file =>
     40 		return file;
     41 	};
     42 };