2014-07-06 08:13:36 +00:00
|
|
|
#![crate_name = "tac"]
|
2015-02-06 12:54:58 +00:00
|
|
|
#![feature(collections, core, io, path, rustc_private)]
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern crate getopts;
|
2014-04-07 22:43:34 +00:00
|
|
|
extern crate libc;
|
2014-02-27 18:59:51 +00:00
|
|
|
|
2015-01-29 07:29:31 +00:00
|
|
|
use std::old_io as io;
|
2014-02-27 18:59:51 +00:00
|
|
|
|
|
|
|
#[path = "../common/util.rs"]
|
2015-01-08 12:54:22 +00:00
|
|
|
#[macro_use]
|
2014-02-27 18:59:51 +00:00
|
|
|
mod util;
|
|
|
|
|
|
|
|
static NAME: &'static str = "tac";
|
|
|
|
static VERSION: &'static str = "1.0.0";
|
|
|
|
|
2015-02-06 13:48:07 +00:00
|
|
|
pub fn uumain(args: Vec<String>) -> i32 {
|
2014-07-20 01:13:55 +00:00
|
|
|
let program = args[0].clone();
|
2014-02-27 18:59:51 +00:00
|
|
|
|
2014-05-30 08:35:54 +00:00
|
|
|
let opts = [
|
2014-02-27 18:59:51 +00:00
|
|
|
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")
|
|
|
|
];
|
2014-11-19 20:55:25 +00:00
|
|
|
let matches = match getopts::getopts(args.tail(), &opts) {
|
2014-02-27 18:59:51 +00:00
|
|
|
Ok(m) => m,
|
2014-06-15 10:50:40 +00:00
|
|
|
Err(f) => crash!(1, "{}", f)
|
2014-02-27 18:59:51 +00:00
|
|
|
};
|
|
|
|
if matches.opt_present("help") {
|
|
|
|
println!("tac {}", VERSION);
|
|
|
|
println!("");
|
|
|
|
println!("Usage:");
|
2014-11-21 09:09:43 +00:00
|
|
|
println!(" {0} [OPTION]... [FILE]...", program);
|
2014-02-27 18:59:51 +00:00
|
|
|
println!("");
|
2014-11-19 20:55:25 +00:00
|
|
|
print!("{}", getopts::usage("Write each file to standard output, last line first.", &opts));
|
2014-02-27 18:59:51 +00:00
|
|
|
} 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-28 06:33:39 +00:00
|
|
|
None => "\n".to_string()
|
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-28 06:33:39 +00:00
|
|
|
vec!("-".to_string())
|
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-06-08 07:56:37 +00:00
|
|
|
|
2014-06-12 04:41:53 +00:00
|
|
|
0
|
2014-02-27 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2014-05-25 09:20:52 +00:00
|
|
|
fn tac(filenames: Vec<String>, before: bool, _: bool, separator: &str) {
|
2014-09-17 15:11:39 +00:00
|
|
|
for filename in filenames.into_iter() {
|
2014-02-27 18:59:51 +00:00
|
|
|
let mut file = io::BufferedReader::new(
|
2014-05-17 10:32:14 +00:00
|
|
|
if filename.as_slice() == "-" {
|
2015-01-10 12:49:18 +00:00
|
|
|
Box::new(io::stdio::stdin_raw()) as Box<Reader>
|
2014-05-07 23:55:53 +00:00
|
|
|
} else {
|
2014-12-22 00:39:02 +00:00
|
|
|
let r = crash_if_err!(1, io::File::open(&Path::new(filename)));
|
2015-01-10 12:49:18 +00:00
|
|
|
Box::new(r) as Box<Reader>
|
2014-05-07 23:55:53 +00:00
|
|
|
}
|
|
|
|
);
|
2014-07-09 08:29:50 +00:00
|
|
|
let mut data = crash_if_err!(1, file.read_to_string());
|
2014-05-23 12:28:40 +00:00
|
|
|
if data.as_slice().ends_with("\n") {
|
2014-04-26 05:03:08 +00:00
|
|
|
// removes blank line that is inserted otherwise
|
2015-01-08 13:02:19 +00:00
|
|
|
let mut buf = data.to_string();
|
2014-04-26 05:03:08 +00:00
|
|
|
let len = buf.len();
|
|
|
|
buf.truncate(len - 1);
|
2015-01-08 13:02:19 +00:00
|
|
|
data = buf.to_string();
|
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();
|
2014-05-25 09:20:52 +00:00
|
|
|
let rev: String = split_vec.iter().rev().fold(String::new(), |mut a, &b| {
|
2014-05-23 12:28:40 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|