mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 03:57:44 +00:00
Make show_error! not set the exit code. #211
This commit is contained in:
parent
962a6bbf2f
commit
8fbfe24176
14 changed files with 156 additions and 104 deletions
|
@ -111,7 +111,10 @@ pub fn uumain(args: Vec<String>) -> int {
|
||||||
if files.is_empty() {
|
if files.is_empty() {
|
||||||
match cksum("-") {
|
match cksum("-") {
|
||||||
Ok((crc, size)) => println!("{} {}", crc, size),
|
Ok((crc, size)) => println!("{} {}", crc, size),
|
||||||
Err(err) => show_error!(2, "{}", err),
|
Err(err) => {
|
||||||
|
show_errer!("{}", err);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -119,7 +122,10 @@ pub fn uumain(args: Vec<String>) -> int {
|
||||||
for fname in files.iter() {
|
for fname in files.iter() {
|
||||||
match cksum(fname.as_slice()) {
|
match cksum(fname.as_slice()) {
|
||||||
Ok((crc, size)) => println!("{} {} {}", crc, size, fname),
|
Ok((crc, size)) => println!("{} {} {}", crc, size, fname),
|
||||||
Err(err) => show_error!(2, "'{}' {}", fname, err),
|
Err(err) => {
|
||||||
|
show_errer!("'{}' {}", fname, err);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,18 +12,7 @@
|
||||||
extern crate libc;
|
extern crate libc;
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! show_error(
|
macro_rules! show_errer(
|
||||||
($exitcode:expr, $($args:expr),+) => ({
|
|
||||||
::std::os::set_exit_status($exitcode as int);
|
|
||||||
safe_write!(&mut ::std::io::stderr(), "{}: error: ", ::NAME);
|
|
||||||
safe_writeln!(&mut ::std::io::stderr(), $($args),+);
|
|
||||||
})
|
|
||||||
)
|
|
||||||
|
|
||||||
// FIXME #211: Transitional until there are no further users of `show_error!`.
|
|
||||||
// Then this can be renamed.
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! display_error(
|
|
||||||
($($args:expr),+) => ({
|
($($args:expr),+) => ({
|
||||||
safe_write!(&mut ::std::io::stderr(), "{}: error: ", ::NAME);
|
safe_write!(&mut ::std::io::stderr(), "{}: error: ", ::NAME);
|
||||||
safe_writeln!(&mut ::std::io::stderr(), $($args),+);
|
safe_writeln!(&mut ::std::io::stderr(), $($args),+);
|
||||||
|
@ -41,7 +30,8 @@ macro_rules! show_warning(
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! crash(
|
macro_rules! crash(
|
||||||
($exitcode:expr, $($args:expr),+) => ({
|
($exitcode:expr, $($args:expr),+) => ({
|
||||||
show_error!($exitcode, $($args),+);
|
safe_write!(&mut ::std::io::stderr(), "{}: error: ", ::NAME);
|
||||||
|
safe_writeln!(&mut ::std::io::stderr(), $($args),+);
|
||||||
unsafe { self::libc::exit($exitcode as self::libc::c_int); }
|
unsafe { self::libc::exit($exitcode as self::libc::c_int); }
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
4
du/du.rs
4
du/du.rs
|
@ -162,8 +162,8 @@ pub fn uumain(args: Vec<String>) -> int {
|
||||||
let matches = match getopts::getopts(args.tail(), opts) {
|
let matches = match getopts::getopts(args.tail(), opts) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(f) => {
|
Err(f) => {
|
||||||
show_error!(1, "Invalid options\n{}", f.to_err_msg());
|
show_errer!("Invalid options\n{}", f.to_err_msg());
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -36,8 +36,8 @@ pub fn uumain(args: Vec<String>) -> int {
|
||||||
let matches = match getopts(args.tail(), options) {
|
let matches = match getopts(args.tail(), options) {
|
||||||
Ok(m) => { m },
|
Ok(m) => { m },
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
show_error!(1, "{}", usage(NAME, options));
|
show_errer!("{}", usage(NAME, options));
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,7 @@ mod util;
|
||||||
static NAME: &'static str = "hostid";
|
static NAME: &'static str = "hostid";
|
||||||
static VERSION: &'static str = "0.0.1";
|
static VERSION: &'static str = "0.0.1";
|
||||||
|
|
||||||
static EXIT_ERR: i32 = 1;
|
static EXIT_ERR: int = 1;
|
||||||
|
|
||||||
pub enum Mode {
|
pub enum Mode {
|
||||||
HostId,
|
HostId,
|
||||||
|
@ -65,8 +65,8 @@ pub fn uumain(args: Vec<String>) -> int {
|
||||||
let matches = match getopts(args.tail(), opts) {
|
let matches = match getopts(args.tail(), opts) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
show_error!(EXIT_ERR, "{}\n{}", e.to_err_msg(), get_help_text(NAME, usage.as_slice()));
|
show_errer!("{}\n{}", e.to_err_msg(), get_help_text(NAME, usage.as_slice()));
|
||||||
return 0;
|
return EXIT_ERR;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
12
kill/kill.rs
12
kill/kill.rs
|
@ -41,8 +41,8 @@ mod util;
|
||||||
static NAME: &'static str = "kill";
|
static NAME: &'static str = "kill";
|
||||||
static VERSION: &'static str = "0.0.1";
|
static VERSION: &'static str = "0.0.1";
|
||||||
|
|
||||||
static EXIT_OK: i32 = 0;
|
static EXIT_OK: int = 0;
|
||||||
static EXIT_ERR: i32 = 1;
|
static EXIT_ERR: int = 1;
|
||||||
|
|
||||||
pub enum Mode {
|
pub enum Mode {
|
||||||
Kill,
|
Kill,
|
||||||
|
@ -71,8 +71,8 @@ pub fn uumain(args: Vec<String>) -> int {
|
||||||
let matches = match getopts(args.tail(), opts) {
|
let matches = match getopts(args.tail(), opts) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
show_error!(EXIT_ERR, "{}\n{}", e.to_err_msg(), get_help_text(NAME, usage.as_slice()));
|
show_errer!("{}\n{}", e.to_err_msg(), get_help_text(NAME, usage.as_slice()));
|
||||||
return 0
|
return EXIT_ERR;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -128,10 +128,10 @@ fn print_signal(signal_name_or_value: &str) {
|
||||||
for signal in ALL_SIGNALS.iter() {
|
for signal in ALL_SIGNALS.iter() {
|
||||||
if signal.name == signal_name_or_value || (format!("SIG{}", signal.name).as_slice()) == signal_name_or_value {
|
if signal.name == signal_name_or_value || (format!("SIG{}", signal.name).as_slice()) == signal_name_or_value {
|
||||||
println!("{}", signal.value)
|
println!("{}", signal.value)
|
||||||
exit!(EXIT_OK)
|
exit!(EXIT_OK as i32)
|
||||||
} else if signal_name_or_value == signal.value.to_str().as_slice() {
|
} else if signal_name_or_value == signal.value.to_str().as_slice() {
|
||||||
println!("{}", signal.name);
|
println!("{}", signal.name);
|
||||||
exit!(EXIT_OK)
|
exit!(EXIT_OK as i32)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
crash!(EXIT_ERR, "unknown signal name {}", signal_name_or_value)
|
crash!(EXIT_ERR, "unknown signal name {}", signal_name_or_value)
|
||||||
|
|
|
@ -144,7 +144,7 @@ fn exec(dirs: Vec<String>, mk_parents: bool, mode: FilePermission, verbose: bool
|
||||||
} else {
|
} else {
|
||||||
format!("directory '{}' already exists", *dir)
|
format!("directory '{}' already exists", *dir)
|
||||||
};
|
};
|
||||||
display_error!("{}", error_msg);
|
show_errer!("{}", error_msg);
|
||||||
return Err(1);
|
return Err(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
60
rm/rm.rs
60
rm/rm.rs
|
@ -79,8 +79,9 @@ pub fn uumain(args: Vec<String>) -> int {
|
||||||
} else if matches.opt_present("version") {
|
} else if matches.opt_present("version") {
|
||||||
println!("rm 1.0.0");
|
println!("rm 1.0.0");
|
||||||
} else if matches.free.is_empty() {
|
} else if matches.free.is_empty() {
|
||||||
show_error!(1, "missing an argument");
|
show_errer!("missing an argument");
|
||||||
show_error!(1, "for help, try '{0:s} --help'", program)
|
show_errer!("for help, try '{0:s} --help'", program);
|
||||||
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
let force = matches.opt_present("force");
|
let force = matches.opt_present("force");
|
||||||
let interactive =
|
let interactive =
|
||||||
|
@ -116,15 +117,18 @@ pub fn uumain(args: Vec<String>) -> int {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
remove(matches.free, force, interactive, one_fs, preserve_root,
|
match remove(matches.free, force, interactive, one_fs, preserve_root,
|
||||||
recursive, dir, verbose);
|
recursive, dir, verbose) {
|
||||||
|
Ok(()) => ( /* pass */ ),
|
||||||
|
Err(e) => return e
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: implement one-file-system
|
// TODO: implement one-file-system
|
||||||
fn remove(files: Vec<String>, force: bool, interactive: InteractiveMode, one_fs: bool, preserve_root: bool, recursive: bool, dir: bool, verbose: bool) {
|
fn remove(files: Vec<String>, force: bool, interactive: InteractiveMode, one_fs: bool, preserve_root: bool, recursive: bool, dir: bool, verbose: bool) -> Result<(), int> {
|
||||||
for filename in files.iter() {
|
for filename in files.iter() {
|
||||||
let filename = filename.as_slice();
|
let filename = filename.as_slice();
|
||||||
let file = Path::new(filename);
|
let file = Path::new(filename);
|
||||||
|
@ -137,30 +141,46 @@ fn remove(files: Vec<String>, force: bool, interactive: InteractiveMode, one_fs:
|
||||||
crash!(1, "{}", f.to_str());
|
crash!(1, "{}", f.to_str());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
remove(walk_dir.map(|x| x.as_str().unwrap().to_string()).collect(), force, interactive, one_fs, preserve_root, recursive, dir, verbose);
|
match remove(walk_dir.map(|x| x.as_str().unwrap().to_string()).collect(), force, interactive, one_fs, preserve_root, recursive, dir, verbose) {
|
||||||
remove_dir(&file, filename, interactive, verbose);
|
Ok(()) => ( /* pass */ ),
|
||||||
|
Err(e) => return Err(e)
|
||||||
|
}
|
||||||
|
match remove_dir(&file, filename, interactive, verbose) {
|
||||||
|
Ok(()) => ( /* pass */ ),
|
||||||
|
Err(e) => return Err(e)
|
||||||
|
}
|
||||||
} else if dir && (filename != "/" || !preserve_root) {
|
} else if dir && (filename != "/" || !preserve_root) {
|
||||||
remove_dir(&file, filename, interactive, verbose);
|
match remove_dir(&file, filename, interactive, verbose) {
|
||||||
|
Ok(()) => ( /* pass */ ),
|
||||||
|
Err(e) => return Err(e)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if recursive {
|
if recursive {
|
||||||
show_error!(1, "could not remove directory '{}'",
|
show_errer!("could not remove directory '{}'",
|
||||||
filename);
|
filename);
|
||||||
|
return Err(1);
|
||||||
} else {
|
} else {
|
||||||
show_error!(1,
|
show_errer!("could not remove directory '{}' (did you mean to pass '-r'?)",
|
||||||
"could not remove directory '{}' (did you mean to pass '-r'?)",
|
|
||||||
filename);
|
filename);
|
||||||
|
return Err(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
remove_file(&file, filename.as_slice(), interactive, verbose);
|
match remove_file(&file, filename.as_slice(), interactive, verbose) {
|
||||||
|
Ok(()) => ( /* pass */ ),
|
||||||
|
Err(e) => return Err(e)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if !force {
|
} else if !force {
|
||||||
show_error!(1, "no such file or directory '{}'", filename);
|
show_errer!("no such file or directory '{}'", filename);
|
||||||
|
return Err(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_dir(path: &Path, name: &str, interactive: InteractiveMode, verbose: bool) {
|
fn remove_dir(path: &Path, name: &str, interactive: InteractiveMode, verbose: bool) -> Result<(), int> {
|
||||||
let response =
|
let response =
|
||||||
if interactive == InteractiveAlways {
|
if interactive == InteractiveAlways {
|
||||||
prompt_file(path, name)
|
prompt_file(path, name)
|
||||||
|
@ -171,13 +191,16 @@ fn remove_dir(path: &Path, name: &str, interactive: InteractiveMode, verbose: bo
|
||||||
match fs::rmdir(path) {
|
match fs::rmdir(path) {
|
||||||
Ok(_) => if verbose { println!("Removed '{}'", name); },
|
Ok(_) => if verbose { println!("Removed '{}'", name); },
|
||||||
Err(f) => {
|
Err(f) => {
|
||||||
show_error!(1, "{}", f.to_str());
|
show_errer!("{}", f.to_str());
|
||||||
|
return Err(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_file(path: &Path, name: &str, interactive: InteractiveMode, verbose: bool) {
|
fn remove_file(path: &Path, name: &str, interactive: InteractiveMode, verbose: bool) -> Result<(), int> {
|
||||||
let response =
|
let response =
|
||||||
if interactive == InteractiveAlways {
|
if interactive == InteractiveAlways {
|
||||||
prompt_file(path, name)
|
prompt_file(path, name)
|
||||||
|
@ -188,10 +211,13 @@ fn remove_file(path: &Path, name: &str, interactive: InteractiveMode, verbose: b
|
||||||
match fs::unlink(path) {
|
match fs::unlink(path) {
|
||||||
Ok(_) => if verbose { println!("Removed '{}'", name); },
|
Ok(_) => if verbose { println!("Removed '{}'", name); },
|
||||||
Err(f) => {
|
Err(f) => {
|
||||||
show_error!(1, "{}", f.to_str());
|
show_errer!("{}", f.to_str());
|
||||||
|
return Err(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prompt_file(path: &Path, name: &str) -> bool {
|
fn prompt_file(path: &Path, name: &str) -> bool {
|
||||||
|
|
|
@ -38,8 +38,8 @@ pub fn uumain(args: Vec<String>) -> int {
|
||||||
let matches = match getopts::getopts(args.tail(), opts) {
|
let matches = match getopts::getopts(args.tail(), opts) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(f) => {
|
Err(f) => {
|
||||||
show_error!(1, "{}", f.to_err_msg());
|
show_errer!("{}", f.to_err_msg());
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -53,39 +53,50 @@ pub fn uumain(args: Vec<String>) -> int {
|
||||||
} else if matches.opt_present("version") {
|
} else if matches.opt_present("version") {
|
||||||
println!("rmdir 1.0.0");
|
println!("rmdir 1.0.0");
|
||||||
} else if matches.free.is_empty() {
|
} else if matches.free.is_empty() {
|
||||||
show_error!(1, "missing an argument");
|
show_errer!("missing an argument");
|
||||||
show_error!(1, "for help, try '{0:s} --help'", program);
|
show_errer!("for help, try '{0:s} --help'", program);
|
||||||
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
let ignore = matches.opt_present("ignore-fail-on-non-empty");
|
let ignore = matches.opt_present("ignore-fail-on-non-empty");
|
||||||
let parents = matches.opt_present("parents");
|
let parents = matches.opt_present("parents");
|
||||||
let verbose = matches.opt_present("verbose");
|
let verbose = matches.opt_present("verbose");
|
||||||
remove(matches.free, ignore, parents, verbose);
|
match remove(matches.free, ignore, parents, verbose) {
|
||||||
|
Ok(()) => ( /* pass */ ),
|
||||||
|
Err(e) => return e
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove(dirs: Vec<String>, ignore: bool, parents: bool, verbose: bool) {
|
fn remove(dirs: Vec<String>, ignore: bool, parents: bool, verbose: bool) -> Result<(), int>{
|
||||||
for dir in dirs.iter() {
|
for dir in dirs.iter() {
|
||||||
let path = Path::new(dir.as_slice());
|
let path = Path::new(dir.as_slice());
|
||||||
if path.exists() {
|
if path.exists() {
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
remove_dir(&path, dir.as_slice(), ignore, parents, verbose);
|
match remove_dir(&path, dir.as_slice(), ignore, parents, verbose) {
|
||||||
|
Ok(()) => ( /* pass */ ),
|
||||||
|
Err(e) => return Err(e)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
show_error!(1, "failed to remove '{}' (file)", *dir);
|
show_errer!("failed to remove '{}' (file)", *dir);
|
||||||
|
return Err(1);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
show_error!(1, "no such file or directory '{}'", *dir);
|
show_errer!("no such file or directory '{}'", *dir);
|
||||||
|
return Err(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
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) -> Result<(), int> {
|
||||||
let mut walk_dir = match fs::walk_dir(path) {
|
let mut walk_dir = match fs::walk_dir(path) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(f) => {
|
Err(f) => {
|
||||||
show_error!(1, "{}", f.to_str());
|
show_errer!("{}", f.to_str());
|
||||||
return;
|
return Err(1);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if walk_dir.next() == None {
|
if walk_dir.next() == None {
|
||||||
|
@ -97,16 +108,23 @@ fn remove_dir(path: &Path, dir: &str, ignore: bool, parents: bool, verbose: bool
|
||||||
if parents {
|
if parents {
|
||||||
let dirname = path.dirname_str().unwrap();
|
let dirname = path.dirname_str().unwrap();
|
||||||
if dirname != "." {
|
if dirname != "." {
|
||||||
remove_dir(&Path::new(dirname), dirname, ignore, parents, verbose);
|
match remove_dir(&Path::new(dirname), dirname, ignore, parents, verbose) {
|
||||||
|
Ok(()) => ( /* pass */ ),
|
||||||
|
Err(e) => return Err(e)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(f) => {
|
Err(f) => {
|
||||||
show_error!(1, "{}", f.to_str());
|
show_errer!("{}", f.to_str());
|
||||||
|
return Err(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if !ignore {
|
} else if !ignore {
|
||||||
show_error!(1, "Failed to remove directory '{}' (non-empty)", dir);
|
show_errer!("Failed to remove directory '{}' (non-empty)", dir);
|
||||||
|
return Err(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
seq/seq.rs
10
seq/seq.rs
|
@ -47,9 +47,9 @@ pub fn uumain(args: Vec<String>) -> int {
|
||||||
let matches = match getopts::getopts(args.tail(), opts) {
|
let matches = match getopts::getopts(args.tail(), opts) {
|
||||||
Ok(m) => { m }
|
Ok(m) => { m }
|
||||||
Err(f) => {
|
Err(f) => {
|
||||||
show_error!(1, "{:s}", f.to_err_msg());
|
show_errer!("{:s}", f.to_err_msg());
|
||||||
print_usage(opts);
|
print_usage(opts);
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if matches.opt_present("help") {
|
if matches.opt_present("help") {
|
||||||
|
@ -67,7 +67,7 @@ pub fn uumain(args: Vec<String>) -> int {
|
||||||
let first = if matches.free.len() > 1 {
|
let first = if matches.free.len() > 1 {
|
||||||
match parse_float(matches.free.get(0).as_slice()) {
|
match parse_float(matches.free.get(0).as_slice()) {
|
||||||
Ok(n) => n,
|
Ok(n) => n,
|
||||||
Err(s) => { show_error!(1, "{:s}", s); return 0; }
|
Err(s) => { show_errer!("{:s}", s); return 1; }
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
1.0
|
1.0
|
||||||
|
@ -75,14 +75,14 @@ pub fn uumain(args: Vec<String>) -> int {
|
||||||
let step = if matches.free.len() > 2 {
|
let step = if matches.free.len() > 2 {
|
||||||
match parse_float(matches.free.get(1).as_slice()) {
|
match parse_float(matches.free.get(1).as_slice()) {
|
||||||
Ok(n) => n,
|
Ok(n) => n,
|
||||||
Err(s) => { show_error!(1, "{:s}", s); return 0; }
|
Err(s) => { show_errer!("{:s}", s); return 1; }
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
1.0
|
1.0
|
||||||
};
|
};
|
||||||
let last = match parse_float(matches.free.get(matches.free.len()-1).as_slice()) {
|
let last = match parse_float(matches.free.get(matches.free.len()-1).as_slice()) {
|
||||||
Ok(n) => n,
|
Ok(n) => n,
|
||||||
Err(s) => { show_error!(1, "{:s}", s); return 0; }
|
Err(s) => { show_errer!("{:s}", s); return 1; }
|
||||||
};
|
};
|
||||||
let separator = escape_sequences(matches.opt_str("s").unwrap_or("\n".to_string()).as_slice());
|
let separator = escape_sequences(matches.opt_str("s").unwrap_or("\n".to_string()).as_slice());
|
||||||
let terminator = escape_sequences(matches.opt_str("t").unwrap_or(separator.to_string()).as_slice());
|
let terminator = escape_sequences(matches.opt_str("t").unwrap_or(separator.to_string()).as_slice());
|
||||||
|
|
|
@ -36,8 +36,8 @@ pub fn uumain(args: Vec<String>) -> int {
|
||||||
let matches = match getopts::getopts(args.tail(), opts) {
|
let matches = match getopts::getopts(args.tail(), opts) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(f) => {
|
Err(f) => {
|
||||||
show_error!(1, "{}", f.to_err_msg());
|
show_errer!("{}", f.to_err_msg());
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -57,8 +57,9 @@ specified by the sum of their values.", opts).as_slice());
|
||||||
} else if matches.opt_present("version") {
|
} else if matches.opt_present("version") {
|
||||||
println!("sleep 1.0.0");
|
println!("sleep 1.0.0");
|
||||||
} else if matches.free.is_empty() {
|
} else if matches.free.is_empty() {
|
||||||
show_error!(1, "missing an argument");
|
show_errer!("missing an argument");
|
||||||
show_error!(1, "for help, try '{0:s} --help'", program);
|
show_errer!("for help, try '{0:s} --help'", program);
|
||||||
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
sleep(matches.free);
|
sleep(matches.free);
|
||||||
}
|
}
|
||||||
|
|
8
tr/tr.rs
8
tr/tr.rs
|
@ -161,8 +161,8 @@ pub fn uumain(args: Vec<String>) -> int {
|
||||||
let matches = match getopts::getopts(args.tail(), opts) {
|
let matches = match getopts::getopts(args.tail(), opts) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
show_error!(1, "{}", err.to_err_msg());
|
show_errer!("{}", err.to_err_msg());
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -186,8 +186,8 @@ pub fn uumain(args: Vec<String>) -> int {
|
||||||
let sets = matches.free;
|
let sets = matches.free;
|
||||||
|
|
||||||
if cflag && !dflag {
|
if cflag && !dflag {
|
||||||
show_error!(1, "-c is only supported with -d");
|
show_errer!("-c is only supported with -d");
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if dflag {
|
if dflag {
|
||||||
|
|
|
@ -21,18 +21,6 @@ use std::u64;
|
||||||
#[path = "../common/util.rs"]
|
#[path = "../common/util.rs"]
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
macro_rules! get_file_size(
|
|
||||||
($file:ident, $action:expr) => ({
|
|
||||||
match fs::stat($file.path()) {
|
|
||||||
Ok(stat) => stat.size,
|
|
||||||
Err(f) => {
|
|
||||||
show_error!(1, "{}", f.to_str());
|
|
||||||
$action
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
)
|
|
||||||
|
|
||||||
#[deriving(Eq, PartialEq)]
|
#[deriving(Eq, PartialEq)]
|
||||||
enum TruncateMode {
|
enum TruncateMode {
|
||||||
Reference,
|
Reference,
|
||||||
|
@ -95,8 +83,8 @@ file based on its current size:
|
||||||
} else if matches.opt_present("version") {
|
} else if matches.opt_present("version") {
|
||||||
println!("truncate 1.0.0");
|
println!("truncate 1.0.0");
|
||||||
} else if matches.free.is_empty() {
|
} else if matches.free.is_empty() {
|
||||||
show_error!(1, "missing an argument");
|
show_errer!("missing an argument");
|
||||||
crash!(1, "for help, try '{0:s} --help'", program);
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
let no_create = matches.opt_present("no-create");
|
let no_create = matches.opt_present("no-create");
|
||||||
let io_blocks = matches.opt_present("io-blocks");
|
let io_blocks = matches.opt_present("io-blocks");
|
||||||
|
@ -105,14 +93,17 @@ file based on its current size:
|
||||||
if reference.is_none() && size.is_none() {
|
if reference.is_none() && size.is_none() {
|
||||||
crash!(1, "you must specify either --reference or --size");
|
crash!(1, "you must specify either --reference or --size");
|
||||||
} else {
|
} else {
|
||||||
truncate(no_create, io_blocks, reference, size, matches.free);
|
match truncate(no_create, io_blocks, reference, size, matches.free) {
|
||||||
|
Ok(()) => ( /* pass */ ),
|
||||||
|
Err(e) => return e
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn truncate(no_create: bool, _: bool, reference: Option<String>, size: Option<String>, filenames: Vec<String>) {
|
fn truncate(no_create: bool, _: bool, reference: Option<String>, size: Option<String>, filenames: Vec<String>) -> Result<(), int> {
|
||||||
let (refsize, mode) = match reference {
|
let (refsize, mode) = match reference {
|
||||||
Some(rfilename) => {
|
Some(rfilename) => {
|
||||||
let rfile = match File::open(&Path::new(rfilename.clone())) {
|
let rfile = match File::open(&Path::new(rfilename.clone())) {
|
||||||
|
@ -121,7 +112,13 @@ fn truncate(no_create: bool, _: bool, reference: Option<String>, size: Option<St
|
||||||
crash!(1, "{}", f.to_str())
|
crash!(1, "{}", f.to_str())
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
(get_file_size!(rfile, return), Reference)
|
match fs::stat(rfile.path()) {
|
||||||
|
Ok(stat) => (stat.size, Reference),
|
||||||
|
Err(f) => {
|
||||||
|
show_errer!("{}", f.to_str());
|
||||||
|
return Err(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
None => parse_size(size.unwrap().as_slice())
|
None => parse_size(size.unwrap().as_slice())
|
||||||
};
|
};
|
||||||
|
@ -131,7 +128,13 @@ fn truncate(no_create: bool, _: bool, reference: Option<String>, size: Option<St
|
||||||
if path.exists() || !no_create {
|
if path.exists() || !no_create {
|
||||||
match File::open_mode(&path, Open, ReadWrite) {
|
match File::open_mode(&path, Open, ReadWrite) {
|
||||||
Ok(mut file) => {
|
Ok(mut file) => {
|
||||||
let fsize = get_file_size!(file, continue);
|
let fsize = match fs::stat(file.path()) {
|
||||||
|
Ok(stat) => stat.size,
|
||||||
|
Err(f) => {
|
||||||
|
show_warning!("{}", f.to_str());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
let tsize = match mode {
|
let tsize = match mode {
|
||||||
Reference => refsize,
|
Reference => refsize,
|
||||||
Extend => fsize + refsize,
|
Extend => fsize + refsize,
|
||||||
|
@ -144,16 +147,19 @@ fn truncate(no_create: bool, _: bool, reference: Option<String>, size: Option<St
|
||||||
match file.truncate(tsize as i64) {
|
match file.truncate(tsize as i64) {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(f) => {
|
Err(f) => {
|
||||||
show_error!(1, "{}", f.to_str());
|
show_errer!("{}", f.to_str());
|
||||||
|
return Err(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(f) => {
|
Err(f) => {
|
||||||
show_error!(1, "{}", f.to_str());
|
show_errer!("{}", f.to_str());
|
||||||
|
return Err(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_size(size: &str) -> (u64, TruncateMode) {
|
fn parse_size(size: &str) -> (u64, TruncateMode) {
|
||||||
|
|
25
wc/wc.rs
25
wc/wc.rs
|
@ -17,6 +17,7 @@ extern crate libc;
|
||||||
use std::os;
|
use std::os;
|
||||||
use std::str::from_utf8;
|
use std::str::from_utf8;
|
||||||
use std::io::{print, stdin, File, BufferedReader};
|
use std::io::{print, stdin, File, BufferedReader};
|
||||||
|
use StdResult = std::result::Result;
|
||||||
use getopts::Matches;
|
use getopts::Matches;
|
||||||
|
|
||||||
#[path = "../common/util.rs"]
|
#[path = "../common/util.rs"]
|
||||||
|
@ -75,7 +76,10 @@ pub fn uumain(args: Vec<String>) -> int {
|
||||||
files = vec!("-".to_string());
|
files = vec!("-".to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
wc(files, &matches);
|
match wc(files, &matches) {
|
||||||
|
Ok(()) => ( /* pass */ ),
|
||||||
|
Err(e) => return e
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -91,7 +95,7 @@ fn is_word_seperator(byte: u8) -> bool {
|
||||||
byte == SPACE || byte == TAB || byte == CR || byte == SYN || byte == FF
|
byte == SPACE || byte == TAB || byte == CR || byte == SYN || byte == FF
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn wc(files: Vec<String>, matches: &Matches) {
|
pub fn wc(files: Vec<String>, matches: &Matches) -> StdResult<(), int> {
|
||||||
let mut total_line_count: uint = 0;
|
let mut total_line_count: uint = 0;
|
||||||
let mut total_word_count: uint = 0;
|
let mut total_word_count: uint = 0;
|
||||||
let mut total_char_count: uint = 0;
|
let mut total_char_count: uint = 0;
|
||||||
|
@ -103,8 +107,8 @@ pub fn wc(files: Vec<String>, matches: &Matches) {
|
||||||
|
|
||||||
for path in files.iter() {
|
for path in files.iter() {
|
||||||
let mut reader = match open(path.to_string()) {
|
let mut reader = match open(path.to_string()) {
|
||||||
Some(f) => f,
|
Ok(f) => f,
|
||||||
None => { continue }
|
Err(e) => { return Err(e); }
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut line_count: uint = 0;
|
let mut line_count: uint = 0;
|
||||||
|
@ -187,6 +191,8 @@ pub fn wc(files: Vec<String>, matches: &Matches) {
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
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,
|
||||||
|
@ -226,21 +232,20 @@ fn print_stats(filename: &str, line_count: uint, word_count: uint, char_count: u
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn open(path: String) -> Option<BufferedReader<Box<Reader>>> {
|
fn open(path: String) -> StdResult<BufferedReader<Box<Reader>>, int> {
|
||||||
if "-" == path.as_slice() {
|
if "-" == path.as_slice() {
|
||||||
let reader = box stdin() as Box<Reader>;
|
let reader = box stdin() as Box<Reader>;
|
||||||
return Some(BufferedReader::new(reader));
|
return Ok(BufferedReader::new(reader));
|
||||||
}
|
}
|
||||||
|
|
||||||
match File::open(&std::path::Path::new(path.as_slice())) {
|
match File::open(&std::path::Path::new(path.as_slice())) {
|
||||||
Ok(fd) => {
|
Ok(fd) => {
|
||||||
let reader = box fd as Box<Reader>;
|
let reader = box fd as Box<Reader>;
|
||||||
return Some(BufferedReader::new(reader));
|
return Ok(BufferedReader::new(reader));
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
show_error!(1, "wc: {0:s}: {1:s}", path, e.desc.to_str());
|
show_errer!("wc: {0:s}: {1:s}", path, e.desc.to_str());
|
||||||
|
return Err(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue