coreutils/src/tac/tac.rs

148 lines
4.5 KiB
Rust
Raw Normal View History

2014-07-06 08:13:36 +00:00
#![crate_name = "tac"]
2015-04-30 22:44:31 +00:00
#![feature(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;
extern crate libc;
2014-02-27 18:59:51 +00:00
2015-04-30 22:44:31 +00:00
use std::fs::File;
use std::io::{stdin, stdout, BufReader, Read, Stdout, Write};
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";
pub fn uumain(args: Vec<String>) -> i32 {
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")
];
2015-04-30 22:44:31 +00:00
let matches = match getopts::getopts(&args[1..], &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:");
2015-04-30 22:44:31 +00:00
println!(" {0} [OPTION]... [FILE]...", &args[0][..]);
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
};
2015-04-30 22:44:31 +00:00
tac(files, before, regex, &separator[..]);
2014-02-27 18:59:51 +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();
for filename in filenames.iter() {
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
if offsets.len() == 0 || *offsets.last().unwrap() < data.len() - slen {
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
}
}