coreutils/tac/tac.rs

103 lines
3.3 KiB
Rust
Raw Normal View History

2014-03-31 16:40:21 +00:00
#![crate_id(name = "tac", vers = "1.0.0", author = "Arcterus")]
2014-02-27 18:59:51 +00:00
/*
* This file is part of the uutils coreutils package.
*
* (c) Arcterus <arcterus@mail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2014-03-31 16:40:21 +00:00
#![feature(macro_rules)]
2014-02-27 18:59:51 +00:00
extern crate getopts;
extern crate libc;
2014-02-27 18:59:51 +00:00
use std::io;
use std::os;
#[path = "../common/util.rs"]
mod util;
static NAME: &'static str = "tac";
static VERSION: &'static str = "1.0.0";
fn main() {
2014-05-17 10:32:14 +00:00
let args: Vec<StrBuf> = os::args().iter().map(|x| x.to_strbuf()).collect();
2014-05-16 08:32:58 +00:00
let program = args.get(0).clone();
2014-02-27 18:59:51 +00:00
let opts = ~[
getopts::optflag("b", "before", "attach the separator before instead of after"),
getopts::optflag("r", "regex", "interpret the sequence as a regular expression (NOT IMPLEMENTED)"),
getopts::optopt("s", "separator", "use STRING as the separator instead of newline", "STRING"),
getopts::optflag("h", "help", "display this help and exit"),
getopts::optflag("V", "version", "output version information and exit")
];
let matches = match getopts::getopts(args.tail(), opts) {
Ok(m) => m,
Err(f) => crash!(1, "{}", f.to_err_msg())
};
if matches.opt_present("help") {
println!("tac {}", VERSION);
println!("");
println!("Usage:");
println!(" {0:s} [OPTION]... [FILE]...", program);
println!("");
print!("{}", getopts::usage("Write each file to standard output, last line first.", opts));
} else if matches.opt_present("version") {
println!("tac {}", VERSION);
} else {
let before = matches.opt_present("b");
let regex = matches.opt_present("r");
let separator = match matches.opt_str("s") {
2014-02-28 05:34:43 +00:00
Some(m) => {
if m.len() == 0 {
crash!(1, "separator cannot be empty")
} else {
m
}
}
2014-05-17 10:32:14 +00:00
None => "\n".to_strbuf()
2014-02-27 18:59:51 +00:00
};
2014-05-07 23:55:53 +00:00
let files = if matches.free.is_empty() {
2014-05-17 10:32:14 +00:00
vec!("-".to_strbuf())
2014-05-07 23:55:53 +00:00
} else {
matches.free
};
2014-05-17 10:32:14 +00:00
tac(files, before, regex, separator.as_slice());
2014-02-27 18:59:51 +00:00
}
}
2014-05-17 10:32:14 +00:00
fn tac(filenames: Vec<StrBuf>, before: bool, _: bool, separator: &str) {
2014-02-27 18:59:51 +00:00
for filename in filenames.move_iter() {
let mut file = io::BufferedReader::new(
2014-05-17 10:32:14 +00:00
if filename.as_slice() == "-" {
2014-05-09 00:12:57 +00:00
box io::stdio::stdin_raw() as Box<Reader>
2014-05-07 23:55:53 +00:00
} else {
2014-05-09 00:12:57 +00:00
box crash_if_err!(1, io::File::open(&Path::new(filename))) as Box<Reader>
2014-05-07 23:55:53 +00:00
}
);
2014-02-28 05:34:43 +00:00
let mut data = crash_if_err!(1, file.read_to_str());
2014-05-23 12:28:40 +00:00
if data.as_slice().ends_with("\n") {
// removes blank line that is inserted otherwise
let mut buf = data.into_strbuf();
let len = buf.len();
buf.truncate(len - 1);
data = buf.into_owned();
2014-02-28 05:34:43 +00:00
}
2014-05-23 12:28:40 +00:00
let split_vec: Vec<&str> = data.as_slice().split_str(separator).collect();
let rev: StrBuf = split_vec.iter().rev().fold(StrBuf::new(), |mut a, &b| {
if before {
a.push_str(separator);
a.push_str(b);
2014-02-27 18:59:51 +00:00
} else {
2014-05-23 12:28:40 +00:00
a.push_str(b);
a.push_str(separator);
2014-02-27 18:59:51 +00:00
}
2014-05-23 12:28:40 +00:00
a
});
2014-02-27 18:59:51 +00:00
print!("{}", rev);
}
}