2015-12-08 02:42:08 +00:00
|
|
|
#![crate_name = "uu_tac"]
|
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;
|
|
|
|
|
2015-11-24 01:00:51 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate uucore;
|
|
|
|
|
2015-04-30 22:44:31 +00:00
|
|
|
use std::fs::File;
|
2015-05-21 18:42:42 +00:00
|
|
|
use std::io::{BufReader, Read, stdin, stdout, Stdout, Write};
|
2014-02-27 18:59:51 +00:00
|
|
|
|
|
|
|
static NAME: &'static str = "tac";
|
2015-11-25 09:52:10 +00:00
|
|
|
static VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
2014-02-27 18:59:51 +00:00
|
|
|
|
2015-02-06 13:48:07 +00:00
|
|
|
pub fn uumain(args: Vec<String>) -> i32 {
|
2015-05-21 18:42:42 +00:00
|
|
|
let mut opts = getopts::Options::new();
|
|
|
|
|
|
|
|
opts.optflag("b", "before", "attach the separator before instead of after");
|
|
|
|
opts.optflag("r", "regex", "interpret the sequence as a regular expression (NOT IMPLEMENTED)");
|
|
|
|
opts.optopt("s", "separator", "use STRING as the separator instead of newline", "STRING");
|
|
|
|
opts.optflag("h", "help", "display this help and exit");
|
|
|
|
opts.optflag("V", "version", "output version information and exit");
|
|
|
|
|
|
|
|
let matches = match opts.parse(&args[1..]) {
|
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") {
|
2015-05-21 18:42:42 +00:00
|
|
|
let msg = format!("{0} {1}
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
{0} [OPTION]... [FILE]...
|
|
|
|
|
|
|
|
Write each file to standard output, last line first.", NAME, VERSION);
|
|
|
|
|
|
|
|
print!("{}", opts.usage(&msg));
|
2014-02-27 18:59:51 +00:00
|
|
|
} else if matches.opt_present("version") {
|
2015-05-21 18:42:42 +00:00
|
|
|
println!("{} {}", NAME, VERSION);
|
2014-02-27 18:59:51 +00:00
|
|
|
} 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) => {
|
2016-01-05 19:42:52 +00:00
|
|
|
if m.is_empty() {
|
2014-02-28 05:34:43 +00:00
|
|
|
crash!(1, "separator cannot be empty")
|
|
|
|
} else {
|
|
|
|
m
|
|
|
|
}
|
|
|
|
}
|
2016-01-05 19:42:52 +00:00
|
|
|
None => "\n".to_owned()
|
2014-02-27 18:59:51 +00:00
|
|
|
};
|
2014-05-07 23:55:53 +00:00
|
|
|
let files = if matches.free.is_empty() {
|
2016-01-05 19:42:52 +00:00
|
|
|
vec!("-".to_owned())
|
2014-05-07 23:55:53 +00:00
|
|
|
} else {
|
|
|
|
matches.free
|
|
|
|
};
|
2015-04-30 22:44:31 +00:00
|
|
|
tac(files, before, regex, &separator[..]);
|
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) {
|
2015-04-30 22:44:31 +00:00
|
|
|
let mut out = stdout();
|
|
|
|
let sbytes = separator.as_bytes();
|
|
|
|
let slen = sbytes.len();
|
|
|
|
|
2016-01-05 19:42:52 +00:00
|
|
|
for filename in &filenames {
|
2015-04-30 22:44:31 +00:00
|
|
|
let mut file = BufReader::new(
|
|
|
|
if filename == "-" {
|
|
|
|
Box::new(stdin()) as Box<Read>
|
|
|
|
} else {
|
|
|
|
match File::open(filename) {
|
|
|
|
Ok(f) => Box::new(f) as Box<Read>,
|
|
|
|
Err(e) => {
|
|
|
|
show_warning!("failed to open '{}' for reading: {}", filename, e);
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut data = Vec::new();
|
|
|
|
match file.read_to_end(&mut data) {
|
|
|
|
Err(e) => {
|
|
|
|
show_warning!("failed to read '{}': {}", filename, e);
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
Ok(_) => (),
|
|
|
|
};
|
|
|
|
|
|
|
|
// find offsets in string of all separators
|
|
|
|
let mut offsets = Vec::new();
|
|
|
|
let mut i = 0;
|
|
|
|
loop {
|
|
|
|
if i + slen > data.len() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if &data[i..i+slen] == sbytes {
|
|
|
|
offsets.push(i);
|
|
|
|
i += slen;
|
2014-05-07 23:55:53 +00:00
|
|
|
} else {
|
2015-04-30 22:44:31 +00:00
|
|
|
i += 1;
|
2014-05-07 23:55:53 +00:00
|
|
|
}
|
2014-02-28 05:34:43 +00:00
|
|
|
}
|
2015-04-30 22:44:31 +00:00
|
|
|
drop(i);
|
|
|
|
|
|
|
|
// if there isn't a separator at the end of the file, fake it
|
2016-01-05 19:42:52 +00:00
|
|
|
if offsets.is_empty() || *offsets.last().unwrap() < data.len() - slen {
|
2015-04-30 22:44:31 +00:00
|
|
|
offsets.push(data.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut prev = *offsets.last().unwrap();
|
|
|
|
let mut start = true;
|
|
|
|
for off in offsets.iter().rev().skip(1) {
|
|
|
|
// correctly handle case of no final separator in file
|
|
|
|
if start && prev == data.len() {
|
|
|
|
show_line(&mut out, &[], &data[*off+slen..prev], before);
|
|
|
|
start = false;
|
2014-02-27 18:59:51 +00:00
|
|
|
} else {
|
2015-04-30 22:44:31 +00:00
|
|
|
show_line(&mut out, sbytes, &data[*off+slen..prev], before);
|
2014-02-27 18:59:51 +00:00
|
|
|
}
|
2015-04-30 22:44:31 +00:00
|
|
|
prev = *off;
|
|
|
|
}
|
|
|
|
show_line(&mut out, sbytes, &data[0..prev], before);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn show_line(out: &mut Stdout, sep: &[u8], dat: &[u8], before: bool) {
|
|
|
|
if before {
|
|
|
|
out.write_all(sep).unwrap_or_else(|e| crash!(1, "failed to write to stdout: {}", e));
|
|
|
|
}
|
|
|
|
|
|
|
|
out.write_all(dat).unwrap_or_else(|e| crash!(1, "failed to write to stdout: {}", e));
|
|
|
|
|
|
|
|
if !before {
|
|
|
|
out.write_all(sep).unwrap_or_else(|e| crash!(1, "failed to write to stdout: {}", e));
|
2014-02-27 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|