mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Merge pull request #69 from Heather/master
Important changes! [ Mostly due language Syntax changes ]
This commit is contained in:
commit
2fa4f23d86
12 changed files with 154 additions and 115 deletions
2
Makefile
2
Makefile
|
@ -3,7 +3,7 @@ RUSTC ?= rustc
|
||||||
RM := rm
|
RM := rm
|
||||||
|
|
||||||
# Flags
|
# Flags
|
||||||
RUSTCFLAGS := --opt-level=3
|
RUSTCFLAGS := --opt-level=3 -A unused_must_use
|
||||||
RMFLAGS :=
|
RMFLAGS :=
|
||||||
|
|
||||||
# Possible programs
|
# Possible programs
|
||||||
|
|
|
@ -82,7 +82,10 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decode(input: &mut Reader, ignore_garbage: bool) {
|
fn decode(input: &mut Reader, ignore_garbage: bool) {
|
||||||
let mut to_decode = str::from_utf8_owned(input.read_to_end()).unwrap();
|
let mut to_decode = str::from_utf8_owned(match input.read_to_end() {
|
||||||
|
Ok(m) => m,
|
||||||
|
Err(f) => fail!(f.to_str())
|
||||||
|
}).unwrap();
|
||||||
|
|
||||||
to_decode = str::replace(to_decode, "\n", "");
|
to_decode = str::replace(to_decode, "\n", "");
|
||||||
|
|
||||||
|
@ -120,7 +123,10 @@ fn encode(input: &mut Reader, line_wrap: uint) {
|
||||||
_ => Some(line_wrap)
|
_ => Some(line_wrap)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let to_encode = input.read_to_end();
|
let to_encode = match input.read_to_end() {
|
||||||
|
Ok(m) => m,
|
||||||
|
Err(f) => fail!(f.to_str())
|
||||||
|
};
|
||||||
let mut encoded = to_encode.to_base64(b64_conf);
|
let mut encoded = to_encode.to_base64(b64_conf);
|
||||||
|
|
||||||
// To my knowledge, RFC 3548 does not specify which line endings to use. It
|
// To my knowledge, RFC 3548 does not specify which line endings to use. It
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#[feature(macro_rules)];
|
||||||
#[crate_id(name="basename", vers="1.0.0", author="Jimmy Lu")];
|
#[crate_id(name="basename", vers="1.0.0", author="Jimmy Lu")];
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -11,7 +12,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;
|
||||||
|
@ -19,28 +20,31 @@ use extra::getopts::groups;
|
||||||
|
|
||||||
static VERSION: &'static str = "1.0.0";
|
static VERSION: &'static str = "1.0.0";
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! crash(
|
||||||
|
($exitcode:expr, $($args:expr),+) => (
|
||||||
|
{ ::std::os::set_exit_status($exitcode);
|
||||||
|
let _unused = write!(&mut ::std::io::stderr(), $($args),+);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args = os::args();
|
let args = os::args();
|
||||||
|
|
||||||
let program = strip_dir(&args[ 0 ].clone());
|
let program = strip_dir(&args[ 0 ].clone());
|
||||||
|
|
||||||
//
|
//
|
||||||
// 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"),
|
||||||
];
|
];
|
||||||
|
|
||||||
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) => crash!(1, "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 +99,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
67
cat/cat.rs
67
cat/cat.rs
|
@ -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
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
20
echo/echo.rs
20
echo/echo.rs
|
@ -1,3 +1,4 @@
|
||||||
|
#[feature(macro_rules)];
|
||||||
#[crate_id(name="echo", vers="1.0.0", author="Derek Chiang")];
|
#[crate_id(name="echo", vers="1.0.0", author="Derek Chiang")];
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -12,12 +13,22 @@
|
||||||
extern mod extra;
|
extern mod extra;
|
||||||
|
|
||||||
use std::os;
|
use std::os;
|
||||||
use std::io::{print, println, stderr};
|
use std::io::{print, println};
|
||||||
use std::uint;
|
use std::uint;
|
||||||
use extra::getopts::groups;
|
use extra::getopts::groups;
|
||||||
|
|
||||||
static VERSION: &'static str = "1.0.0";
|
static VERSION: &'static str = "1.0.0";
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! crash(
|
||||||
|
($exitcode:expr, $($args:expr),+) => (
|
||||||
|
{ ::std::os::set_exit_status($exitcode);
|
||||||
|
let _unused = write!(&mut ::std::io::stderr(), $($args),+);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
fn print_char(c: char) {
|
fn print_char(c: char) {
|
||||||
print!("{}", c);
|
print!("{}", c);
|
||||||
}
|
}
|
||||||
|
@ -77,12 +88,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) => crash!(1, "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") {
|
||||||
|
|
4
env/env.rs
vendored
4
env/env.rs
vendored
|
@ -191,12 +191,12 @@ fn main() {
|
||||||
match opts.program {
|
match opts.program {
|
||||||
[ref prog, ..args] => {
|
[ref prog, ..args] => {
|
||||||
match std::run::process_status(prog.as_slice(), args.as_slice()) {
|
match std::run::process_status(prog.as_slice(), args.as_slice()) {
|
||||||
Some(exit) =>
|
Ok(exit) =>
|
||||||
std::os::set_exit_status(match exit {
|
std::os::set_exit_status(match exit {
|
||||||
std::io::process::ExitStatus(s) => s,
|
std::io::process::ExitStatus(s) => s,
|
||||||
_ => 1
|
_ => 1
|
||||||
}),
|
}),
|
||||||
None => std::os::set_exit_status(1)
|
Err(_) => std::os::set_exit_status(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
extern mod extra;
|
extern mod extra;
|
||||||
|
|
||||||
use std::os;
|
use std::os;
|
||||||
use std::io::{fs, result, stderr};
|
use std::io::{fs, stderr};
|
||||||
use std::num::strconv;
|
use std::num::strconv;
|
||||||
use extra::getopts::groups;
|
use extra::getopts::groups;
|
||||||
|
|
||||||
|
@ -149,7 +149,7 @@ fn exec(dirs: ~[~str], mk_parents: bool, mode: u32, verbose: bool) {
|
||||||
* Wrapper to catch errors, return false if failed
|
* Wrapper to catch errors, return false if failed
|
||||||
*/
|
*/
|
||||||
fn mkdir(path: &Path, mode: u32) -> bool {
|
fn mkdir(path: &Path, mode: u32) -> bool {
|
||||||
match result(|| fs::mkdir(path, mode)) {
|
match fs::mkdir(path, mode) {
|
||||||
Ok(_) => true,
|
Ok(_) => true,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
writeln!(&mut stderr() as &mut Writer,
|
writeln!(&mut stderr() as &mut Writer,
|
||||||
|
|
49
rm/rm.rs
49
rm/rm.rs
|
@ -12,7 +12,7 @@
|
||||||
extern mod extra;
|
extern mod extra;
|
||||||
|
|
||||||
use std::os;
|
use std::os;
|
||||||
use std::io::{print,stdin,stderr,stdio,fs,BufferedReader,io_error};
|
use std::io::{print,stdin,stderr,stdio,fs,BufferedReader};
|
||||||
use extra::getopts::groups;
|
use extra::getopts::groups;
|
||||||
|
|
||||||
#[deriving(Eq)]
|
#[deriving(Eq)]
|
||||||
|
@ -127,7 +127,16 @@ fn remove(files: &[~str], force: bool, interactive: InteractiveMode, one_fs: boo
|
||||||
if file.exists() {
|
if file.exists() {
|
||||||
if file.is_dir() {
|
if file.is_dir() {
|
||||||
if recursive && (*filename != ~"/" || !preserve_root) {
|
if recursive && (*filename != ~"/" || !preserve_root) {
|
||||||
remove(fs::walk_dir(&file).map(|x| x.as_str().unwrap().to_owned()).to_owned_vec(), force, interactive, one_fs, preserve_root, recursive, dir, verbose);
|
let walk_dir = match fs::walk_dir(&file) {
|
||||||
|
Ok(m) => m,
|
||||||
|
Err(f) => {
|
||||||
|
writeln!(&mut stderr() as &mut Writer,
|
||||||
|
"{}", f.to_str());
|
||||||
|
os::set_exit_status(1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
remove(walk_dir.map(|x| x.as_str().unwrap().to_owned()).to_owned_vec(), force, interactive, one_fs, preserve_root, recursive, dir, verbose);
|
||||||
remove_dir(&file, *filename, interactive, verbose);
|
remove_dir(&file, *filename, interactive, verbose);
|
||||||
} else if dir && (*filename != ~"/" || !preserve_root) {
|
} else if dir && (*filename != ~"/" || !preserve_root) {
|
||||||
remove_dir(&file, *filename, interactive, verbose);
|
remove_dir(&file, *filename, interactive, verbose);
|
||||||
|
@ -162,16 +171,14 @@ fn remove_dir(path: &Path, name: &str, interactive: InteractiveMode, verbose: bo
|
||||||
true
|
true
|
||||||
};
|
};
|
||||||
if response {
|
if response {
|
||||||
io_error::cond.trap(|_| {
|
match fs::rmdir(path) {
|
||||||
writeln!(&mut stderr() as &mut Writer,
|
Ok(_) => if verbose { println!("Removed '{}'", name); },
|
||||||
"Could not remove directory '{}'", name);
|
Err(f) => {
|
||||||
os::set_exit_status(1);
|
writeln!(&mut stderr() as &mut Writer,
|
||||||
}).inside(|| {
|
"{}", f.to_str());
|
||||||
fs::rmdir(path);
|
os::set_exit_status(1);
|
||||||
if verbose {
|
|
||||||
println!("Removed '{}'", name);
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -183,16 +190,14 @@ fn remove_file(path: &Path, name: &str, interactive: InteractiveMode, verbose: b
|
||||||
true
|
true
|
||||||
};
|
};
|
||||||
if response {
|
if response {
|
||||||
io_error::cond.trap(|_| {
|
match fs::unlink(path) {
|
||||||
writeln!(&mut stderr() as &mut Writer,
|
Ok(_) => if verbose { println!("Removed '{}'", name); },
|
||||||
"Could not remove file '{}'", name);
|
Err(f) => {
|
||||||
os::set_exit_status(1);
|
writeln!(&mut stderr() as &mut Writer,
|
||||||
}).inside(|| {
|
"{}", f.to_str());
|
||||||
fs::unlink(path);
|
os::set_exit_status(1);
|
||||||
if verbose {
|
|
||||||
println!("Removed '{}'", name);
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -212,7 +217,7 @@ fn prompt(msg: &str) -> bool {
|
||||||
fn read_prompt() -> bool {
|
fn read_prompt() -> bool {
|
||||||
stdio::flush();
|
stdio::flush();
|
||||||
match BufferedReader::new(stdin()).read_line() {
|
match BufferedReader::new(stdin()).read_line() {
|
||||||
Some(line) => {
|
Ok(line) => {
|
||||||
match line.char_at(0) {
|
match line.char_at(0) {
|
||||||
'y' | 'Y' => true,
|
'y' | 'Y' => true,
|
||||||
'n' | 'N' => false,
|
'n' | 'N' => false,
|
||||||
|
@ -222,7 +227,7 @@ fn read_prompt() -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => true
|
Err(_) => true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
extern mod extra;
|
extern mod extra;
|
||||||
|
|
||||||
use std::os;
|
use std::os;
|
||||||
use std::io::{print, stderr, io_error, fs};
|
use std::io::{print, stderr, fs};
|
||||||
use extra::getopts::groups;
|
use extra::getopts::groups;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -77,23 +77,34 @@ fn remove(dirs: &[~str], ignore: bool, parents: bool, verbose: bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_dir(path: &Path, dir: &~str, ignore: bool, parents: bool, verbose: bool) {
|
fn remove_dir(path: &Path, dir: &~str, ignore: bool, parents: bool, verbose: bool) {
|
||||||
if fs::walk_dir(path).next() == None {
|
let mut walk_dir = match fs::walk_dir(path) {
|
||||||
io_error::cond.trap(|_| {
|
Ok(m) => m,
|
||||||
|
Err(f) => {
|
||||||
writeln!(&mut stderr() as &mut Writer,
|
writeln!(&mut stderr() as &mut Writer,
|
||||||
"Failed to remove directory '{}'", *dir);
|
"{}", f.to_str());
|
||||||
os::set_exit_status(1);
|
os::set_exit_status(1);
|
||||||
}).inside(|| {
|
return;
|
||||||
fs::rmdir(path);
|
}
|
||||||
if verbose {
|
};
|
||||||
println!("Removed directory '{}'", *dir);
|
if walk_dir.next() == None {
|
||||||
}
|
match fs::rmdir(path) {
|
||||||
if parents {
|
Ok(_) => {
|
||||||
let dirname = path.dirname_str().unwrap();
|
if verbose {
|
||||||
if dirname != "." {
|
println!("Removed directory '{}'", *dir);
|
||||||
remove_dir(&Path::new(dirname), &dirname.to_owned(), ignore, parents, verbose);
|
}
|
||||||
|
if parents {
|
||||||
|
let dirname = path.dirname_str().unwrap();
|
||||||
|
if dirname != "." {
|
||||||
|
remove_dir(&Path::new(dirname), &dirname.to_owned(), ignore, parents, verbose);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
Err(f) => {
|
||||||
|
writeln!(&mut stderr() as &mut Writer,
|
||||||
|
"{}", f.to_str());
|
||||||
|
os::set_exit_status(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
} else if !ignore {
|
} else if !ignore {
|
||||||
writeln!(&mut stderr() as &mut Writer,
|
writeln!(&mut stderr() as &mut Writer,
|
||||||
"Failed to remove directory '{}' (non-empty)", *dir);
|
"Failed to remove directory '{}' (non-empty)", *dir);
|
||||||
|
|
|
@ -28,7 +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) => {
|
||||||
writeln!(&mut stderr() as &mut Writer, "{}", f.to_err_msg())
|
writeln!(&mut stderr() as &mut Writer, "{}", f.to_err_msg());
|
||||||
os::set_exit_status(1);
|
os::set_exit_status(1);
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
33
wc/wc.rs
33
wc/wc.rs
|
@ -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 {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue