coreutils/tac/tac.rs
Michael Gehring 30bba07f9c always build multicall binary
squashed:
	a2c6b27 - build: automatically generate main() files
	c942f0f - remove MULTICALL=1 build from travis
	cb7b35b - make: remove unnecessary shell command
	69bbb31 - update README
	03a3168 - all: move main() into separate file that links against util crate
	8276384 - make: always build multicall binary
	aa4edeb - make: avoid 'rustc --crate-file-name'
2014-06-26 10:26:16 +02:00

102 lines
3.3 KiB
Rust

#![crate_id(name = "tac", vers = "1.0.0", author = "Arcterus")]
/*
* 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.
*/
#![feature(macro_rules)]
extern crate getopts;
extern crate libc;
use std::io;
#[path = "../common/util.rs"]
mod util;
static NAME: &'static str = "tac";
static VERSION: &'static str = "1.0.0";
pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).clone();
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)
};
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") {
Some(m) => {
if m.len() == 0 {
crash!(1, "separator cannot be empty")
} else {
m
}
}
None => "\n".to_string()
};
let files = if matches.free.is_empty() {
vec!("-".to_string())
} else {
matches.free
};
tac(files, before, regex, separator.as_slice());
}
0
}
fn tac(filenames: Vec<String>, before: bool, _: bool, separator: &str) {
for filename in filenames.move_iter() {
let mut file = io::BufferedReader::new(
if filename.as_slice() == "-" {
box io::stdio::stdin_raw() as Box<Reader>
} else {
box crash_if_err!(1, io::File::open(&Path::new(filename))) as Box<Reader>
}
);
let mut data = crash_if_err!(1, file.read_to_str());
if data.as_slice().ends_with("\n") {
// removes blank line that is inserted otherwise
let mut buf = data.into_string();
let len = buf.len();
buf.truncate(len - 1);
data = buf.into_string();
}
let split_vec: Vec<&str> = data.as_slice().split_str(separator).collect();
let rev: String = split_vec.iter().rev().fold(String::new(), |mut a, &b| {
if before {
a.push_str(separator);
a.push_str(b);
} else {
a.push_str(b);
a.push_str(separator);
}
a
});
print!("{}", rev);
}
}