1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Merge branch 'master' of git://github.com/Heather/coreutils into Heather-master

This commit is contained in:
Arcterus 2014-02-04 20:46:41 -08:00
commit 89ad4d6c1b
4 changed files with 64 additions and 63 deletions

View file

@ -11,7 +11,7 @@
extern mod extra; extern mod extra;
use std::io::{print, println, stderr}; use std::io::{print, println};
use std::os; use std::os;
use std::str; use std::str;
use std::str::StrSlice; use std::str::StrSlice;
@ -27,7 +27,6 @@ fn main() {
// //
// Argument parsing // Argument parsing
// //
let opts = ~[ let opts = ~[
groups::optflag("h", "help", "display this help and exit"), groups::optflag("h", "help", "display this help and exit"),
groups::optflag("V", "version", "output version information and exit"), groups::optflag("V", "version", "output version information and exit"),
@ -35,12 +34,7 @@ fn main() {
let matches = match groups::getopts(args.tail(), opts) { let matches = match groups::getopts(args.tail(), opts) {
Ok(m) => m, Ok(m) => m,
Err(f) => { Err(f) => fail!("Invalid options\n{}", f.to_err_msg())
writeln!(&mut stderr() as &mut Writer,
"Invalid options\n{}", f.to_err_msg());
os::set_exit_status(1);
return;
}
}; };
if matches.opt_present("help") { if matches.opt_present("help") {
@ -95,7 +89,6 @@ fn strip_dir(fullname :&~str) -> ~str {
if c == '/' || c == '\\' { if c == '/' || c == '\\' {
return name; return name;
} }
name = str::from_char(c) + name; name = str::from_char(c) + name;
} }

View file

@ -15,7 +15,7 @@
extern mod extra; extern mod extra;
use std::os; use std::os;
use std::io::{print, stdin, stderr, stdout, File, result}; use std::io::{print, stdin, stdout, File};
use extra::getopts::groups; use extra::getopts::groups;
fn main() { fn main() {
@ -36,12 +36,7 @@ fn main() {
]; ];
let matches = match groups::getopts(args.tail(), opts) { let matches = match groups::getopts(args.tail(), opts) {
Ok(m) => m, Ok(m) => m,
Err(f) => { Err(f) => fail!("Invalid options\n{}", f.to_err_msg())
writeln!(&mut stderr() as &mut Writer,
"Invalid options\n{}", f.to_err_msg());
os::set_exit_status(1);
return
}
}; };
if matches.opt_present("help") { if matches.opt_present("help") {
println!("cat 1.0.0"); println!("cat 1.0.0");
@ -118,11 +113,13 @@ pub fn exec(files: ~[~str], number: NumberingMode, show_nonprint: bool, show_end
loop { loop {
// reading from a TTY seems to raise a condition on // reading from a TTY seems to raise a condition on
// EOF, rather than return Some(0) like a file. // EOF, rather than return Some(0) like a file.
match result(|| reader.read(buf)) { match reader.read(buf) {
Ok(Some(n)) if n != 0 => { Ok(n) if n != 0 => {
for byte in buf.slice_to(n).iter() { for byte in buf.slice_to(n).iter() {
if at_line_start && (number == NumberAll || (number == NumberNonEmpty && !is_newline_char(*byte))) { if at_line_start && (number == NumberAll || (number == NumberNonEmpty && !is_newline_char(*byte))) {
write!(&mut writer as &mut Writer, "{0:6u}\t", counter); match write!(&mut writer as &mut Writer, "{0:6u}\t", counter) {
Ok(_) => (), Err(err) => fail!("{}", err)
};
counter += 1; counter += 1;
at_line_start = false; at_line_start = false;
} }
@ -130,32 +127,45 @@ pub fn exec(files: ~[~str], number: NumberingMode, show_nonprint: bool, show_end
at_line_start = true; at_line_start = true;
} }
if show_tabs && *byte == TAB { if show_tabs && *byte == TAB {
writer.write(bytes!("^I")); match writer.write(bytes!("^I")) {
Ok(_) => (), Err(err) => fail!("{}", err)
};
} else if show_ends && *byte == LF { } else if show_ends && *byte == LF {
writer.write(bytes!("$\n")); match writer.write(bytes!("$\n")) {
Ok(_) => (), Err(err) => fail!("{}", err)
};
} else if show_nonprint && (*byte < 32 || *byte >= 127) && !is_newline_char(*byte) { } else if show_nonprint && (*byte < 32 || *byte >= 127) && !is_newline_char(*byte) {
let mut byte = *byte; let mut byte = *byte;
if byte >= 128 { if byte >= 128 {
writer.write(bytes!("M-")); match writer.write(bytes!("M-")) {
Ok(_) => (), Err(err) => fail!("{}", err)
};
byte = byte - 128; byte = byte - 128;
} }
if byte < 32 { if byte < 32 {
writer.write(['^' as u8, byte + 64]); match writer.write(['^' as u8, byte + 64]) {
Ok(_) => (), Err(err) => fail!("{}", err)
};
} else if byte == 127 { } else if byte == 127 {
writer.write(['^' as u8, byte - 64]); match writer.write(['^' as u8, byte - 64]) {
Ok(_) => (), Err(err) => fail!("{}", err)
};
} else { } else {
writer.write([byte]); match writer.write([byte]) {
Ok(_) => (), Err(err) => fail!("{}", err)
};
} }
} else { } else {
writer.write([*byte]); match writer.write([*byte]) {
} Ok(_) => (), Err(err) => fail!("{}", err)
};
} }
} }
},
_ => break _ => break
} }
} }
} }
return; return;
} }
@ -170,9 +180,12 @@ pub fn exec(files: ~[~str], number: NumberingMode, show_nonprint: bool, show_end
loop { loop {
// reading from a TTY seems to raise a condition on EOF, // reading from a TTY seems to raise a condition on EOF,
// rather than return Some(0) like a file. // rather than return Some(0) like a file.
match result(|| reader.read(buf)) { match reader.read(buf) {
Ok(Some(n)) if n != 0 => writer.write(buf.slice_to(n)), Ok(n) if n != 0 => {
_ => break match writer.write(buf.slice_to(n)) {
Ok(_) => (), Err(err) => fail!("{}", err)
}
}, _ => break
} }
} }
} }
@ -183,14 +196,8 @@ fn open(path: ~str) -> Option<~Reader> {
return Some(~stdin() as ~Reader); return Some(~stdin() as ~Reader);
} }
match result(|| File::open(&std::path::Path::new(path.as_slice()))) { match File::open(&std::path::Path::new(path.as_slice())) {
Ok(fd) => return Some(~fd as ~Reader), Ok(fd) => return Some(~fd as ~Reader),
Err(e) => { Err(e) => fail!("cat: {0:s}: {1:s}", path, e.to_str())
writeln!(&mut stderr() as &mut Writer,
"cat: {0:s}: {1:s}", path, e.to_str());
os::set_exit_status(1);
} }
} }
None
}

View file

@ -12,7 +12,7 @@
extern mod extra; extern mod extra;
use std::os; use std::os;
use std::io::{print, stderr}; use std::io::print;
use extra::getopts::groups; use extra::getopts::groups;
static VERSION: &'static str = "1.0.0"; static VERSION: &'static str = "1.0.0";
@ -28,12 +28,7 @@ fn main() {
let matches = match groups::getopts(args.tail(), opts) { let matches = match groups::getopts(args.tail(), opts) {
Ok(m) => m, Ok(m) => m,
Err(f) => { Err(f) => fail!("Invalid options\n{}", f.to_err_msg())
writeln!(&mut stderr() as &mut Writer,
"Invalid options\n{}", f.to_err_msg());
os::set_exit_status(1);
return
}
}; };
if matches.opt_present("help") { if matches.opt_present("help") {
@ -60,7 +55,10 @@ directory).", opts));
if !matches.free.is_empty() { if !matches.free.is_empty() {
for path in matches.free.iter() { for path in matches.free.iter() {
let p = std::path::Path::new(path.clone()); let p = std::path::Path::new(path.clone());
print(std::str::from_utf8(p.dirname())); let d = std::str::from_utf8(p.dirname());
if d.is_some() {
print(d.unwrap());
}
print(separator); print(separator);
} }
} else { } else {

View file

@ -13,7 +13,7 @@ extern mod extra;
use std::os; use std::os;
use std::io::{print, stdin, stderr, File, result, BufferedReader}; use std::io::{print, stdin, stderr, File, result, BufferedReader};
use std::str::from_utf8_opt; use std::str::from_utf8;
use extra::getopts::{groups, Matches}; use extra::getopts::{groups, Matches};
struct Result { struct Result {
@ -58,7 +58,7 @@ fn main() {
return; return;
} }
if (matches.opt_present("version")) { if matches.opt_present("version") {
println!("wc 1.0.0"); println!("wc 1.0.0");
return; return;
} }
@ -111,14 +111,14 @@ pub fn wc(files: ~[~str], matches: &Matches) {
match result(| | reader.read_until(LF)) { match result(| | reader.read_until(LF)) {
Ok(Some(raw_line)) => { Ok(Some(raw_line)) => {
// GNU 'wc' only counts lines that end in LF as lines // GNU 'wc' only counts lines that end in LF as lines
if (raw_line.iter().last().unwrap() == &LF) { if raw_line.iter().last().unwrap() == &LF {
line_count += 1; line_count += 1;
} }
byte_count += raw_line.iter().len(); byte_count += raw_line.iter().len();
// try and convert the bytes to UTF-8 first // try and convert the bytes to UTF-8 first
match from_utf8_opt(raw_line) { match from_utf8(raw_line) {
Some(line) => { Some(line) => {
word_count += line.words().len(); word_count += line.words().len();
current_char_count = line.chars().len(); current_char_count = line.chars().len();
@ -138,7 +138,7 @@ pub fn wc(files: ~[~str], matches: &Matches) {
} }
} }
if (current_char_count > longest_line_length) { if current_char_count > longest_line_length {
// we subtract one here because `line.iter().len()` includes the LF // we subtract one here because `line.iter().len()` includes the LF
// matches GNU 'wc' behaviour // matches GNU 'wc' behaviour
longest_line_length = current_char_count - 1; longest_line_length = current_char_count - 1;
@ -163,7 +163,7 @@ pub fn wc(files: ~[~str], matches: &Matches) {
total_char_count += char_count; total_char_count += char_count;
total_byte_count += byte_count; total_byte_count += byte_count;
if (longest_line_length > total_longest_line_length) { if longest_line_length > total_longest_line_length {
total_longest_line_length = longest_line_length; total_longest_line_length = longest_line_length;
} }
@ -175,38 +175,41 @@ pub fn wc(files: ~[~str], matches: &Matches) {
print_stats(&result.filename, result.lines, result.words, result.chars, result.bytes, result.max_line_length, matches, max_str_len); print_stats(&result.filename, result.lines, result.words, result.chars, result.bytes, result.max_line_length, matches, max_str_len);
} }
if (files.len() > 1) { if files.len() > 1 {
print_stats(&~"total", total_line_count, total_word_count, total_char_count, total_byte_count, total_longest_line_length, matches, max_str_len); print_stats(&~"total", total_line_count, total_word_count, total_char_count, total_byte_count, total_longest_line_length, matches, max_str_len);
} }
} }
fn print_stats(filename: &~str, line_count: uint, word_count: uint, char_count: uint, fn print_stats(filename: &~str, line_count: uint, word_count: uint, char_count: uint,
byte_count: uint, longest_line_length: uint, matches: &Matches, max_str_len: uint) { byte_count: uint, longest_line_length: uint, matches: &Matches, max_str_len: uint) {
if (matches.opt_present("lines")) { if matches.opt_present("lines") {
print!("{:1$}", line_count, max_str_len); print!("{:1$}", line_count, max_str_len);
} }
if (matches.opt_present("words")) { if matches.opt_present("words") {
print!("{:1$}", word_count, max_str_len); print!("{:1$}", word_count, max_str_len);
} }
if (matches.opt_present("bytes")) { if matches.opt_present("bytes") {
print!("{:1$}", byte_count, max_str_len + 1); print!("{:1$}", byte_count, max_str_len + 1);
} }
if (matches.opt_present("chars")) { if matches.opt_present("chars") {
print!("{:1$}", char_count, max_str_len); print!("{:1$}", char_count, max_str_len);
} }
if (matches.opt_present("max-line-length")) { if matches.opt_present("max-line-length") {
print!("{:1$}", longest_line_length, max_str_len); print!("{:1$}", longest_line_length, max_str_len);
} }
// defaults // defaults
if (!matches.opt_present("bytes") && !matches.opt_present("chars") && !matches.opt_present("lines") if !matches.opt_present("bytes")
&& !matches.opt_present("words") && !matches.opt_present("max-line-length")) { && !matches.opt_present("chars")
&& !matches.opt_present("lines")
&& !matches.opt_present("words")
&& !matches.opt_present("max-line-length") {
print!("{:1$}", line_count, max_str_len); print!("{:1$}", line_count, max_str_len);
print!("{:1$}", word_count, max_str_len + 1); print!("{:1$}", word_count, max_str_len + 1);
print!("{:1$}", byte_count, max_str_len + 1); print!("{:1$}", byte_count, max_str_len + 1);
} }
if (*filename != ~"-") { if *filename != ~"-" {
println!(" {}", *filename); println!(" {}", *filename);
} }
else { else {