Skip to content

Instantly share code, notes, and snippets.

@dradtke
Created November 13, 2013 23:16
Show Gist options
  • Save dradtke/7458284 to your computer and use it in GitHub Desktop.
Save dradtke/7458284 to your computer and use it in GitHub Desktop.
Rustic Cat
/*
* Rustic cat. If any command-line arguments are supplied, this
* program will print out the contents of each of those files.
* Otherwise, it prints out whatever is read from stdin.
*
* As of Rust 0.8, this failed to compile with this error:
*
* error: internal compiler error: unexpected failure
* note: the compiler hit an unexpected failure path. this is a bug
* note: try running with RUST_LOG=rustc=1 to get further details and report the results to github.com/mozilla/rust/issues
*
* And with RUST_LOG=rustc=1:
*
* task <unnamed> failed at 'assertion failed: rp.is_none()', /opt/rust/0.8/src/librustc/middle/typeck/collect.rs:1108
* error: internal compiler error: unexpected failure
* note: the compiler hit an unexpected failure path. this is a bug
* note: try running with RUST_LOG=rustc=1 to get further details and report the results to github.com/mozilla/rust/issues
* task <unnamed> failed at 'explicit failure', /opt/rust/0.8/src/librustc/rustc.rs:391
*/
use std::io;
use std::iter;
use std::os;
condition! {
pub file_not_found : ~str -> ~str;
}
fn cat<T: io::Reader>(reader: &T) {
loop {
let line = reader.read_line();
if reader.eof() {
break;
}
io::println(line);
}
}
fn cat_files<T: Iterator<&'self ~str>>(files: &T) {
let stderr = io::stderr();
do file_not_found::cond.trap(|filename| "cat: " + filename + ": No such file or directory").inside {
for file in files {
let filename = file.to_str();
let p = &Path(filename);
if !p.exists() {
stderr.write_str(file_not_found::cond.raise(filename) + "\n");
} else {
let r = io::file_reader(p);
if r.is_ok() {
cat(&r.unwrap());
} else {
fail!(r.unwrap_err());
}
}
}
}
}
fn main() {
let args: ~[~str] = os::args();
if args.len() > 1 {
cat_files(&args.iter().skip(1));
} else {
cat(&io::stdin());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment