RosettaCodeData/Task/Unix-ls/Rust/unix-ls.rust

25 lines
677 B
Text
Raw Permalink Normal View History

2015-11-18 06:14:39 +00:00
use std::{env, fmt, fs, process};
use std::io::{self, Write};
use std::path::Path;
2015-02-20 09:02:09 -05:00
fn main() {
2015-11-18 06:14:39 +00:00
let cur = env::current_dir().unwrap_or_else(|e| exit_err(e, 1));
let arg = env::args().nth(1);
print_files(arg.as_ref().map_or(cur.as_path(), |p| Path::new(p)))
.unwrap_or_else(|e| exit_err(e, 2));
}
2015-02-20 09:02:09 -05:00
2015-11-18 06:14:39 +00:00
#[inline]
fn print_files(path: &Path) -> io::Result<()> {
for x in try!(fs::read_dir(path)) {
println!("{}", try!(x).file_name().to_string_lossy());
}
Ok(())
}
2015-02-20 09:02:09 -05:00
2015-11-18 06:14:39 +00:00
#[inline]
fn exit_err<T>(msg: T, code: i32) -> ! where T: fmt::Display {
writeln!(&mut io::stderr(), "{}", msg).expect("Could not write to stderr");
process::exit(code)
2015-02-20 09:02:09 -05:00
}