1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

Merge pull request #212 from brson/exitcodes

Change the signature of `uumain` to return `int`
This commit is contained in:
Arcterus 2014-06-12 12:37:51 -07:00
commit 4dbd224e4c
44 changed files with 452 additions and 298 deletions

View file

@ -145,6 +145,11 @@ build/busybox: build/uutils
rm -f build/busybox rm -f build/busybox
ln -s $(SRC_DIR)/build/uutils build/busybox ln -s $(SRC_DIR)/build/uutils build/busybox
# This is a busybox-specific config file their test suite wants to parse.
# For now it's blank.
build/.config: build/uutils
touch $@
ifeq ($(BUSYBOX_SRC),) ifeq ($(BUSYBOX_SRC),)
busytest: busytest:
@echo @echo
@ -153,7 +158,7 @@ busytest:
@echo @echo
@false @false
else else
busytest: build/busybox busytest: build/busybox build/.config
(cd $(BUSYBOX_SRC)/testsuite && bindir=$(SRC_DIR)/build tstdir=$(BUSYBOX_SRC)/testsuite $(BUSYBOX_SRC)/testsuite/runtest $(RUNTEST_ARGS)) (cd $(BUSYBOX_SRC)/testsuite && bindir=$(SRC_DIR)/build tstdir=$(BUSYBOX_SRC)/testsuite $(BUSYBOX_SRC)/testsuite/runtest $(RUNTEST_ARGS))
endif endif
endif endif

View file

@ -15,7 +15,7 @@
extern crate serialize; extern crate serialize;
extern crate getopts; extern crate getopts;
extern crate libc; extern crate libc;
#[phase(syntax, link)] extern crate log; #[phase(plugin, link)] extern crate log;
use std::io::{println, File, stdin, stdout}; use std::io::{println, File, stdin, stdout};
use std::os; use std::os;
@ -35,7 +35,7 @@ mod util;
static NAME: &'static str = "base64"; static NAME: &'static str = "base64";
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let opts = [ let opts = [
optflag("d", "decode", "decode data"), optflag("d", "decode", "decode data"),
optflag("i", "ignore-garbage", "when decoding, ignore non-alphabetic characters"), optflag("i", "ignore-garbage", "when decoding, ignore non-alphabetic characters"),
@ -88,10 +88,12 @@ pub fn uumain(args: Vec<String>) {
Help => help(progname.as_slice(), usage.as_slice()), Help => help(progname.as_slice(), usage.as_slice()),
Version => version() Version => version()
} }
0
} }
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
fn decode(input: &mut Reader, ignore_garbage: bool) { fn decode(input: &mut Reader, ignore_garbage: bool) {
let mut to_decode = match input.read_to_str() { let mut to_decode = match input.read_to_str() {

View file

@ -24,9 +24,9 @@ static NAME: &'static str = "basename";
static VERSION: &'static str = "1.0.0"; static VERSION: &'static str = "1.0.0";
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let program = strip_dir(args.get(0).as_slice()); let program = strip_dir(args.get(0).as_slice());
// //
@ -50,25 +50,25 @@ pub fn uumain(args: Vec<String>) {
print(getopts::usage("", opts).as_slice()); print(getopts::usage("", opts).as_slice());
return; return 0;
} }
if matches.opt_present("version") { if matches.opt_present("version") {
println!("{} {}", program, VERSION); println!("{} {}", program, VERSION);
return; return 0;
} }
// too few arguments // too few arguments
if args.len() < 2 { if args.len() < 2 {
println!("{}: {}", program, "missing operand"); println!("{}: {}", program, "missing operand");
println!("Try '{} --help' for more information.", program); println!("Try '{} --help' for more information.", program);
return; return 0;
} }
// too many arguments // too many arguments
else if args.len() > 3 { else if args.len() > 3 {
println!("{}: extra operand '{}'", program, args.get(3)); println!("{}: extra operand '{}'", program, args.get(3));
println!("Try '{} --help' for more information.", program); println!("Try '{} --help' for more information.", program);
return; return 0;
} }
// //
@ -85,6 +85,8 @@ pub fn uumain(args: Vec<String>) {
} }
println(name.as_slice()); println(name.as_slice());
0
} }
fn strip_dir(fullname: &str) -> String { fn strip_dir(fullname: &str) -> String {
@ -97,7 +99,7 @@ fn strip_dir(fullname: &str) -> String {
name.push_char(c); name.push_char(c);
} }
return name.as_slice().chars().rev().collect(); name.as_slice().chars().rev().collect()
} }
fn strip_suffix(name: &str, suffix: &str) -> String { fn strip_suffix(name: &str, suffix: &str) -> String {
@ -109,5 +111,5 @@ fn strip_suffix(name: &str, suffix: &str) -> String {
return name.slice_to(name.len() - suffix.len()).into_string(); return name.slice_to(name.len() - suffix.len()).into_string();
} }
return name.into_string(); name.into_string()
} }

View file

@ -20,9 +20,9 @@ use std::io::{IoResult};
use std::ptr::{copy_nonoverlapping_memory}; use std::ptr::{copy_nonoverlapping_memory};
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).as_slice(); let program = args.get(0).as_slice();
let opts = [ let opts = [
getopts::optflag("A", "show-all", "equivalent to -vET"), getopts::optflag("A", "show-all", "equivalent to -vET"),
@ -53,11 +53,11 @@ pub fn uumain(args: Vec<String>) {
standard output.", opts).as_slice()); standard output.", opts).as_slice());
println!(""); println!("");
println!("With no FILE, or when FILE is -, read standard input."); println!("With no FILE, or when FILE is -, read standard input.");
return; return 0;
} }
if matches.opt_present("version") { if matches.opt_present("version") {
println!("cat 1.0.0"); println!("cat 1.0.0");
return; return 0;
} }
let mut number_mode = NumberNone; let mut number_mode = NumberNone;
@ -80,6 +80,8 @@ pub fn uumain(args: Vec<String>) {
} }
exec(files, number_mode, show_nonprint, show_ends, show_tabs, squeeze_blank); exec(files, number_mode, show_nonprint, show_ends, show_tabs, squeeze_blank);
0
} }
#[deriving(Eq, PartialEq)] #[deriving(Eq, PartialEq)]
@ -283,12 +285,12 @@ fn open(path: &str) -> Option<(Box<Reader>, bool)> {
} }
match File::open(&std::path::Path::new(path)) { match File::open(&std::path::Path::new(path)) {
Ok(f) => return Some((box f as Box<Reader>, false)), Ok(f) => Some((box f as Box<Reader>, false)),
Err(e) => { Err(e) => {
(writeln!(stderr(), "cat: {0:s}: {1:s}", path, e.to_str())).unwrap(); (writeln!(stderr(), "cat: {0:s}: {1:s}", path, e.to_str())).unwrap();
return None; None
}, },
}; }
} }
struct UnsafeWriter<'a, W> { struct UnsafeWriter<'a, W> {

View file

@ -78,9 +78,9 @@ fn open_file(name: &str) -> IoResult<Box<Reader>> {
} }
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let opts = [ let opts = [
getopts::optflag("h", "help", "display this help and exit"), getopts::optflag("h", "help", "display this help and exit"),
getopts::optflag("V", "version", "output version information and exit"), getopts::optflag("V", "version", "output version information and exit"),
@ -98,12 +98,12 @@ pub fn uumain(args: Vec<String>) {
println!(" {} [OPTIONS] [FILE]...", NAME); println!(" {} [OPTIONS] [FILE]...", NAME);
println!(""); println!("");
print(getopts::usage("Print CRC and size for each file.", opts.as_slice()).as_slice()); print(getopts::usage("Print CRC and size for each file.", opts.as_slice()).as_slice());
return; return 0;
} }
if matches.opt_present("version") { if matches.opt_present("version") {
println!("{} {}", NAME, VERSION); println!("{} {}", NAME, VERSION);
return; return 0;
} }
let files = matches.free; let files = matches.free;
@ -111,15 +111,24 @@ pub fn uumain(args: Vec<String>) {
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_error!("{}", err);
return 2;
} }
return }
return 0;
} }
let mut exit_code = 0;
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_error!("'{}' {}", fname, err);
exit_code = 2;
} }
} }
}
exit_code
} }

View file

@ -95,9 +95,9 @@ fn open_file(name: &str) -> IoResult<Box<Buffer>> {
} }
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let opts = [ let opts = [
getopts::optflag("1", "", "suppress column 1 (lines uniq to FILE1)"), getopts::optflag("1", "", "suppress column 1 (lines uniq to FILE1)"),
getopts::optflag("2", "", "suppress column 2 (lines uniq to FILE2)"), getopts::optflag("2", "", "suppress column 2 (lines uniq to FILE2)"),
@ -114,7 +114,7 @@ pub fn uumain(args: Vec<String>) {
if matches.opt_present("version") { if matches.opt_present("version") {
println!("{} {}", NAME, VERSION); println!("{} {}", NAME, VERSION);
return; return 0;
} }
if matches.opt_present("help") || matches.free.len() != 2 { if matches.opt_present("help") || matches.free.len() != 2 {
@ -125,14 +125,16 @@ pub fn uumain(args: Vec<String>) {
println!(""); println!("");
print(getopts::usage("Compare sorted files line by line.", opts.as_slice()).as_slice()); print(getopts::usage("Compare sorted files line by line.", opts.as_slice()).as_slice());
if matches.free.len() != 2 { if matches.free.len() != 2 {
os::set_exit_status(1); return 0;
} }
return; return 0;
} }
let mut f1 = open_file(matches.free.get(0).as_slice()).unwrap(); let mut f1 = open_file(matches.free.get(0).as_slice()).unwrap();
let mut f2 = open_file(matches.free.get(1).as_slice()).unwrap(); let mut f2 = open_file(matches.free.get(1).as_slice()).unwrap();
comm(&mut f1, &mut f2, &matches) comm(&mut f1, &mut f2, &matches);
0
} }

View file

@ -13,8 +13,7 @@ extern crate libc;
#[macro_export] #[macro_export]
macro_rules! show_error( macro_rules! show_error(
($exitcode:expr, $($args:expr),+) => ({ ($($args:expr),+) => ({
::std::os::set_exit_status($exitcode as int);
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),+);
}) })
@ -31,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); }
}) })
) )

View file

@ -12,7 +12,7 @@
*/ */
extern crate getopts; extern crate getopts;
#[phase(syntax, link)] extern crate log; #[phase(plugin, link)] extern crate log;
use std::os; use std::os;
use std::io; use std::io;
@ -32,9 +32,9 @@ pub enum Mode {
} }
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let opts = [ let opts = [
optflag("h", "help", "display this help and exit"), optflag("h", "help", "display this help and exit"),
optflag("", "version", "output version information and exit"), optflag("", "version", "output version information and exit"),
@ -62,6 +62,8 @@ pub fn uumain(args: Vec<String>) {
Help => help(progname.as_slice(), usage.as_slice()), Help => help(progname.as_slice(), usage.as_slice()),
Version => version(), Version => version(),
} }
0
} }
fn version() { fn version() {

View file

@ -17,9 +17,9 @@ use std::io::print;
static VERSION: &'static str = "1.0.0"; static VERSION: &'static str = "1.0.0";
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).clone(); let program = args.get(0).clone();
let opts = [ let opts = [
getopts::optflag("z", "zero", "separate output with NUL rather than newline"), getopts::optflag("z", "zero", "separate output with NUL rather than newline"),
@ -41,11 +41,12 @@ pub fn uumain(args: Vec<String>) {
print(getopts::usage("Output each NAME with its last non-slash component and trailing slashes print(getopts::usage("Output each NAME with its last non-slash component and trailing slashes
removed; if NAME contains no /'s, output '.' (meaning the current removed; if NAME contains no /'s, output '.' (meaning the current
directory).", opts).as_slice()); directory).", opts).as_slice());
return; return 0;
} }
if matches.opt_present("version") { if matches.opt_present("version") {
return println!("dirname version: {:s}", VERSION); println!("dirname version: {:s}", VERSION);
return 0;
} }
let separator = match matches.opt_present("zero") { let separator = match matches.opt_present("zero") {
@ -66,4 +67,6 @@ directory).", opts).as_slice());
println!("{0:s}: missing operand", program); println!("{0:s}: missing operand", program);
println!("Try '{0:s} --help' for more information.", program); println!("Try '{0:s} --help' for more information.", program);
} }
0
} }

View file

@ -14,15 +14,14 @@
extern crate getopts; extern crate getopts;
extern crate libc; extern crate libc;
extern crate sync;
extern crate time; extern crate time;
use std::os; use std::os;
use std::io::{stderr, fs, FileStat, TypeDirectory}; use std::io::{stderr, fs, FileStat, TypeDirectory};
use std::option::Option; use std::option::Option;
use std::path::Path; use std::path::Path;
use std::sync::{Arc, Future};
use time::Timespec; use time::Timespec;
use sync::{Arc, Future};
#[path = "../common/util.rs"] #[path = "../common/util.rs"]
mod util; mod util;
@ -87,13 +86,13 @@ fn du(path: &Path, mut my_stat: Stat,
stats.push(Arc::new(my_stat)); stats.push(Arc::new(my_stat));
return stats; stats
} }
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).as_slice(); let program = args.get(0).as_slice();
let opts = [ let opts = [
// In task // In task
@ -163,8 +162,8 @@ pub fn uumain(args: Vec<String>) {
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_error!("Invalid options\n{}", f.to_err_msg());
return return 1;
} }
}; };
@ -188,10 +187,10 @@ ers of 1000).",
program = program, program = program,
version = VERSION, version = VERSION,
usage = getopts::usage("Summarize disk usage of each FILE, recursively for directories.", opts)); usage = getopts::usage("Summarize disk usage of each FILE, recursively for directories.", opts));
return return 0;
} else if matches.opt_present("version") { } else if matches.opt_present("version") {
println!("{} version: {}", program, VERSION); println!("{} version: {}", program, VERSION);
return return 0;
} }
let summarize = matches.opt_present("summarize"); let summarize = matches.opt_present("summarize");
@ -201,11 +200,11 @@ ers of 1000).",
match (max_depth_str, max_depth) { match (max_depth_str, max_depth) {
(Some(ref s), _) if summarize => { (Some(ref s), _) if summarize => {
println!("{}: warning: summarizing conflicts with --max-depth={:s}", program, *s); println!("{}: warning: summarizing conflicts with --max-depth={:s}", program, *s);
return return 0;
} }
(Some(ref s), None) => { (Some(ref s), None) => {
println!("{}: invalid maximum depth '{:s}'", program, *s); println!("{}: invalid maximum depth '{:s}'", program, *s);
return return 0;
} }
(Some(_), Some(_)) | (None, _) => { /* valid */ } (Some(_), Some(_)) | (None, _) => { /* valid */ }
} }
@ -240,7 +239,7 @@ ers of 1000).",
for c in s.as_slice().chars() { for c in s.as_slice().chars() {
if found_letter && c.is_digit() || !found_number && !c.is_digit() { if found_letter && c.is_digit() || !found_number && !c.is_digit() {
println!("{}: invalid --block-size argument '{}'", program, s); println!("{}: invalid --block-size argument '{}'", program, s);
return return 0;
} else if c.is_digit() { } else if c.is_digit() {
found_number = true; found_number = true;
numbers.push(c as u8); numbers.push(c as u8);
@ -262,7 +261,7 @@ ers of 1000).",
"ZB" => 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000, "ZB" => 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
"YB" => 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000, "YB" => 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
_ => { _ => {
println!("{}: invalid --block-size argument '{}'", program, s); return println!("{}: invalid --block-size argument '{}'", program, s); return 0;
} }
}; };
number * multiple number * multiple
@ -301,7 +300,7 @@ Valid arguments are:
- 'long-iso' - 'long-iso'
- 'iso' - 'iso'
Try '{program} --help' for more information.", s, program = program); Try '{program} --help' for more information.", s, program = program);
return return 0;
} }
} }
}, },
@ -340,7 +339,7 @@ Try '{program} --help' for more information.", s, program = program);
Valid arguments are: Valid arguments are:
- 'accessed', 'created', 'modified' - 'accessed', 'created', 'modified'
Try '{program} --help' for more information.", program = program); Try '{program} --help' for more information.", program = program);
return return 0;
} }
}, },
None => stat.fstat.modified None => stat.fstat.modified
@ -367,4 +366,6 @@ Try '{program} --help' for more information.", s, program = program);
print!("{:<10} total", convert_size(grand_total)); print!("{:<10} total", convert_size(grand_total));
print!("{}", line_separator); print!("{}", line_separator);
} }
0
} }

View file

@ -66,13 +66,14 @@ fn convert_str(string: &str, index: uint, base: uint) -> (char, int) {
} }
} }
} }
return (to_char(&bytes, base), max_digits)
(to_char(&bytes, base), max_digits)
} }
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).clone(); let program = args.get(0).clone();
let opts = [ let opts = [
getopts::optflag("n", "", "do not output the trailing newline"), getopts::optflag("n", "", "do not output the trailing newline"),
@ -109,11 +110,12 @@ pub fn uumain(args: Vec<String>) {
\\v vertical tab \\v vertical tab
\\0NNN byte with octal value NNN (1 to 3 digits) \\0NNN byte with octal value NNN (1 to 3 digits)
\\xHH byte with hexadecimal value HH (1 to 2 digits)"); \\xHH byte with hexadecimal value HH (1 to 2 digits)");
return; return 0;
} }
if matches.opt_present("version") { if matches.opt_present("version") {
return println!("echo version: {:s}", VERSION); println!("echo version: {:s}", VERSION);
return 0;
} }
if !matches.free.is_empty() { if !matches.free.is_empty() {
@ -185,4 +187,6 @@ pub fn uumain(args: Vec<String>) {
if !matches.opt_present("n") { if !matches.opt_present("n") {
println!("") println!("")
} }
0
} }

24
env/env.rs vendored
View file

@ -54,9 +54,9 @@ fn print_env(null: bool) {
} }
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let prog = args.get(0).as_slice(); let prog = args.get(0).as_slice();
// to handle arguments the same way than GNU env, we can't use getopts // to handle arguments the same way than GNU env, we can't use getopts
@ -97,8 +97,8 @@ pub fn uumain(args: Vec<String>) {
} }
} else if opt.as_slice().starts_with("--") { } else if opt.as_slice().starts_with("--") {
match opt.as_slice() { match opt.as_slice() {
"--help" => { usage(prog); return } "--help" => { usage(prog); return 0; }
"--version" => { version(); return } "--version" => { version(); return 0; }
"--ignore-environment" => opts.ignore_env = true, "--ignore-environment" => opts.ignore_env = true,
"--null" => opts.null = true, "--null" => opts.null = true,
@ -114,7 +114,7 @@ pub fn uumain(args: Vec<String>) {
_ => { _ => {
println!("{:s}: invalid option \"{:s}\"", prog, *opt); println!("{:s}: invalid option \"{:s}\"", prog, *opt);
println!("Type \"{:s} --help\" for detailed informations", prog); println!("Type \"{:s} --help\" for detailed informations", prog);
return return 0;
} }
} }
} else if opt.as_slice().starts_with("-") { } else if opt.as_slice().starts_with("-") {
@ -131,8 +131,8 @@ pub fn uumain(args: Vec<String>) {
for c in chars { for c in chars {
// short versions of options // short versions of options
match c { match c {
'h' => { usage(prog); return } 'h' => { usage(prog); return 0; }
'V' => { version(); return } 'V' => { version(); return 0; }
'i' => opts.ignore_env = true, 'i' => opts.ignore_env = true,
'0' => opts.null = true, '0' => opts.null = true,
'u' => { 'u' => {
@ -146,7 +146,7 @@ pub fn uumain(args: Vec<String>) {
_ => { _ => {
println!("{:s}: illegal option -- {:c}", prog, c); println!("{:s}: illegal option -- {:c}", prog, c);
println!("Type \"{:s} --help\" for detailed informations", prog); println!("Type \"{:s} --help\" for detailed informations", prog);
return return 0;
} }
} }
} }
@ -200,14 +200,16 @@ pub fn uumain(args: Vec<String>) {
let args = opts.program.slice_from(1); let args = opts.program.slice_from(1);
match Command::new(prog).args(args).stdin(InheritFd(0)).stdout(InheritFd(1)).stderr(InheritFd(2)).status() { match Command::new(prog).args(args).stdin(InheritFd(0)).stdout(InheritFd(1)).stderr(InheritFd(2)).status() {
Ok(exit) => Ok(exit) =>
std::os::set_exit_status(match exit { return match exit {
std::io::process::ExitStatus(s) => s, std::io::process::ExitStatus(s) => s,
_ => 1 _ => 1
}), },
Err(_) => std::os::set_exit_status(1) Err(_) => return 1
} }
} else { } else {
// no program provided // no program provided
print_env(opts.null); print_env(opts.null);
} }
0
} }

View file

@ -27,9 +27,9 @@ static NAME: &'static str = "fold";
static VERSION: &'static str = "1.0.0"; static VERSION: &'static str = "1.0.0";
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let (args, obs_width) = handle_obsolete(args.as_slice()); let (args, obs_width) = handle_obsolete(args.as_slice());
let program = args.get(0).clone(); let program = args.get(0).clone();
@ -82,6 +82,8 @@ pub fn uumain(args: Vec<String>) {
}; };
fold(files, bytes, spaces, width); fold(files, bytes, spaces, width);
} }
0
} }
fn handle_obsolete(args: &[String]) -> (Vec<String>, Option<String>) { fn handle_obsolete(args: &[String]) -> (Vec<String>, Option<String>) {

View file

@ -26,9 +26,9 @@ use c_types::{get_pw_from_args, group};
static NAME: &'static str = "groups"; static NAME: &'static str = "groups";
#[allow(dead_code)] #[allow(dead_code)]
fn main () { uumain(os::args()); } fn main () { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let options = [ let options = [
optflag("h", "", "Help") optflag("h", "", "Help")
]; ];
@ -36,10 +36,12 @@ pub fn uumain(args: Vec<String>) {
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_error!("{}", usage(NAME, options));
return; return 1;
} }
}; };
group(get_pw_from_args(&matches.free), true); group(get_pw_from_args(&matches.free), true);
0
} }

View file

@ -23,9 +23,9 @@ use getopts::{optopt, optflag, getopts, usage};
static PROGRAM: &'static str = "head"; static PROGRAM: &'static str = "head";
#[allow(dead_code)] #[allow(dead_code)]
fn main () { uumain(os::args()); } fn main () { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let mut line_count = 10u; let mut line_count = 10u;
// handle obsolete -number syntax // handle obsolete -number syntax
@ -46,15 +46,15 @@ pub fn uumain(args: Vec<String>) {
Ok (m) => { m } Ok (m) => { m }
Err(_) => { Err(_) => {
println!("{:s}", usage(PROGRAM, possible_options)); println!("{:s}", usage(PROGRAM, possible_options));
return return 0;
} }
}; };
if given_options.opt_present("h") { if given_options.opt_present("h") {
println!("{:s}", usage(PROGRAM, possible_options)); println!("{:s}", usage(PROGRAM, possible_options));
return; return 0;
} }
if given_options.opt_present("V") { version(); return } if given_options.opt_present("V") { version(); return 0 }
match given_options.opt_str("n") { match given_options.opt_str("n") {
Some(n) => { Some(n) => {
@ -93,6 +93,8 @@ pub fn uumain(args: Vec<String>) {
head(&mut buffer, line_count); head(&mut buffer, line_count);
} }
} }
0
} }
// It searches for an option in the form of -123123 // It searches for an option in the form of -123123

View file

@ -18,7 +18,7 @@ extern crate serialize;
extern crate libc; extern crate libc;
#[phase(syntax, link)] extern crate log; #[phase(plugin, link)] extern crate log;
use std::os; use std::os;
@ -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,
@ -50,9 +50,9 @@ extern {
} }
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let opts = [ let opts = [
optflag("", "help", "display this help and exit"), optflag("", "help", "display this help and exit"),
@ -65,8 +65,8 @@ pub fn uumain(args: Vec<String>) {
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_error!("{}\n{}", e.to_err_msg(), get_help_text(NAME, usage.as_slice()));
return return EXIT_ERR;
}, },
}; };
@ -83,6 +83,8 @@ pub fn uumain(args: Vec<String>) {
Help => help(NAME, usage.as_slice()), Help => help(NAME, usage.as_slice()),
Version => version(), Version => version(),
} }
0
} }
fn version() { fn version() {

View file

@ -24,9 +24,9 @@ extern {
} }
#[allow(dead_code)] #[allow(dead_code)]
fn main () { uumain(os::args()); } fn main () { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0); let program = args.get(0);
let options = [ let options = [
@ -38,14 +38,14 @@ pub fn uumain(args: Vec<String>) {
let matches = match getopts(args.tail(), options) { let matches = match getopts(args.tail(), options) {
Ok(m) => { m } Ok(m) => { m }
_ => { help_menu(program.as_slice(), options); return; } _ => { help_menu(program.as_slice(), options); return 0; }
}; };
if matches.opt_present("h") { if matches.opt_present("h") {
help_menu(program.as_slice(), options); help_menu(program.as_slice(), options);
return return 0
} }
if matches.opt_present("V") { version(); return } if matches.opt_present("V") { version(); return 0 }
match matches.free.len() { match matches.free.len() {
0 => { 0 => {
@ -55,7 +55,7 @@ pub fn uumain(args: Vec<String>) {
let pos = hostname.as_slice().find_str("."); let pos = hostname.as_slice().find_str(".");
if pos.is_some() { if pos.is_some() {
println!("{:s}", hostname.as_slice().slice_to(pos.unwrap())); println!("{:s}", hostname.as_slice().slice_to(pos.unwrap()));
return; return 0;
} }
} }
@ -64,6 +64,8 @@ pub fn uumain(args: Vec<String>) {
1 => { xsethostname( matches.free.last().unwrap().as_slice() ) } 1 => { xsethostname( matches.free.last().unwrap().as_slice() ) }
_ => { help_menu(program.as_slice(), options); } _ => { help_menu(program.as_slice(), options); }
}; };
0
} }
fn version() { fn version() {

View file

@ -88,9 +88,9 @@ extern {
static NAME: &'static str = "id"; static NAME: &'static str = "id";
#[allow(dead_code)] #[allow(dead_code)]
fn main () { uumain(os::args()); } fn main () { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let args_t = args.tail(); let args_t = args.tail();
let options = [ let options = [
@ -109,18 +109,18 @@ pub fn uumain(args: Vec<String>) {
Ok(m) => { m }, Ok(m) => { m },
Err(_) => { Err(_) => {
println!("{:s}", usage(NAME, options)); println!("{:s}", usage(NAME, options));
return; return 0;
} }
}; };
if matches.opt_present("h") { if matches.opt_present("h") {
println!("{:s}", usage(NAME, options)); println!("{:s}", usage(NAME, options));
return; return 0;
} }
if matches.opt_present("A") { if matches.opt_present("A") {
auditid(); auditid();
return; return 0;
} }
@ -149,7 +149,7 @@ pub fn uumain(args: Vec<String>) {
} else { } else {
println!("{:u}", id); println!("{:u}", id);
} }
return; return 0;
} }
if uflag { if uflag {
@ -171,22 +171,22 @@ pub fn uumain(args: Vec<String>) {
println!("{:d}", id); println!("{:d}", id);
} }
return; return 0;
} }
if matches.opt_present("G") { if matches.opt_present("G") {
group(possible_pw, nflag); group(possible_pw, nflag);
return; return 0;
} }
if matches.opt_present("P") { if matches.opt_present("P") {
pline(possible_pw); pline(possible_pw);
return; return 0;
}; };
if matches.opt_present("p") { if matches.opt_present("p") {
pretty(possible_pw); pretty(possible_pw);
return; return 0;
} }
if possible_pw.is_some() { if possible_pw.is_some() {
@ -194,6 +194,8 @@ pub fn uumain(args: Vec<String>) {
} else { } else {
id_print(possible_pw, false, true, true) id_print(possible_pw, false, true, true)
} }
0
} }
fn pretty(possible_pw: Option<c_passwd>) { fn pretty(possible_pw: Option<c_passwd>) {

View file

@ -17,7 +17,7 @@ extern crate libc;
extern crate collections; extern crate collections;
extern crate serialize; extern crate serialize;
#[phase(syntax, link)] extern crate log; #[phase(plugin, link)] extern crate log;
use std::os; use std::os;
use std::from_str::from_str; use std::from_str::from_str;
@ -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,
@ -53,9 +53,9 @@ pub enum Mode {
} }
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let opts = [ let opts = [
optflag("h", "help", "display this help and exit"), optflag("h", "help", "display this help and exit"),
@ -71,8 +71,8 @@ pub fn uumain(args: Vec<String>) {
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_error!("{}\n{}", e.to_err_msg(), get_help_text(NAME, usage.as_slice()));
return return EXIT_ERR;
}, },
}; };
@ -96,6 +96,8 @@ pub fn uumain(args: Vec<String>) {
Help => help(NAME, usage.as_slice()), Help => help(NAME, usage.as_slice()),
Version => version(), Version => version(),
} }
0
} }
fn version() { fn version() {
@ -126,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)
@ -175,7 +177,7 @@ fn signal_by_name_or_value(signal_name_or_value: &str) -> Option<uint> {
return Some(signal.value); return Some(signal.value);
} }
} }
return None; None
} }
fn kill(signalname: &str, pids: std::vec::Vec<String>) { fn kill(signalname: &str, pids: std::vec::Vec<String>) {

View file

@ -44,9 +44,9 @@ fn version() {
} }
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).clone(); let program = args.get(0).clone();
// //
@ -69,14 +69,16 @@ pub fn uumain(args: Vec<String>) {
println!(" {:s}", program); println!(" {:s}", program);
println!(""); println!("");
print(getopts::usage("print user's login name", opts).as_slice()); print(getopts::usage("print user's login name", opts).as_slice());
return; return 0;
} }
if matches.opt_present("version") { if matches.opt_present("version") {
version(); version();
return; return 0;
} }
exec(); exec();
0
} }
fn exec() { fn exec() {

View file

@ -27,8 +27,10 @@ mod util;
static NAME: &'static str = "md5sum"; static NAME: &'static str = "md5sum";
static VERSION: &'static str = "1.0.0"; static VERSION: &'static str = "1.0.0";
fn main() { #[allow(dead_code)]
let args = os::args(); fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).clone(); let program = args.get(0).clone();
@ -72,11 +74,16 @@ fn main() {
} else { } else {
matches.free matches.free
}; };
md5sum(files, binary, check, tag, status, quiet, strict, warn); match md5sum(files, binary, check, tag, status, quiet, strict, warn) {
Ok(()) => return 0,
Err(e) => return e
} }
}
0
} }
fn md5sum(files: Vec<String>, binary: bool, check: bool, tag: bool, status: bool, quiet: bool, strict: bool, warn: bool) { fn md5sum(files: Vec<String>, binary: bool, check: bool, tag: bool, status: bool, quiet: bool, strict: bool, warn: bool) -> Result<(), int> {
let mut md5 = crypto::md5::Md5::new(); let mut md5 = crypto::md5::Md5::new();
let bytes = md5.output_bits() / 4; let bytes = md5.output_bits() / 4;
let mut bad_format = 0; let mut bad_format = 0;
@ -102,7 +109,7 @@ fn md5sum(files: Vec<String>, binary: bool, check: bool, tag: bool, status: bool
None => { None => {
bad_format += 1; bad_format += 1;
if strict { if strict {
os::set_exit_status(1); return Err(1);
} }
if warn { if warn {
show_warning!("{}: {}: improperly formatted MD5 checksum line", filename, i + 1); show_warning!("{}: {}: improperly formatted MD5 checksum line", filename, i + 1);
@ -121,7 +128,6 @@ fn md5sum(files: Vec<String>, binary: bool, check: bool, tag: bool, status: bool
println!("{}: FAILED", ck_filename); println!("{}: FAILED", ck_filename);
} }
failed += 1; failed += 1;
os::set_exit_status(1);
} }
} }
} else { } else {
@ -143,6 +149,8 @@ fn md5sum(files: Vec<String>, binary: bool, check: bool, tag: bool, status: bool
show_warning!("{} computed checksum did NOT match", failed); show_warning!("{} computed checksum did NOT match", failed);
} }
} }
Ok(())
} }
fn calc_sum(md5: &mut crypto::md5::Md5, file: &mut Reader, binary: bool) -> String { fn calc_sum(md5: &mut crypto::md5::Md5, file: &mut Reader, binary: bool) -> String {

View file

@ -29,9 +29,9 @@ static VERSION: &'static str = "1.0.0";
* Handles option parsing * Handles option parsing
*/ */
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let opts = [ let opts = [
// Linux-specific options, not implemented // Linux-specific options, not implemented
@ -54,11 +54,11 @@ pub fn uumain(args: Vec<String>) {
if args.len() == 1 || matches.opt_present("help") { if args.len() == 1 || matches.opt_present("help") {
print_help(opts); print_help(opts);
return; return 0;
} }
if matches.opt_present("version") { if matches.opt_present("version") {
println!("mkdir v{}", VERSION); println!("mkdir v{}", VERSION);
return; return 0;
} }
let verbose_flag = matches.opt_present("verbose"); let verbose_flag = matches.opt_present("verbose");
let mk_parents = matches.opt_present("parents"); let mk_parents = matches.opt_present("parents");
@ -84,7 +84,10 @@ pub fn uumain(args: Vec<String>) {
if dirs.is_empty() { if dirs.is_empty() {
crash!(1, "missing operand"); crash!(1, "missing operand");
} }
exec(dirs, mk_parents, mode, verbose_flag); match exec(dirs, mk_parents, mode, verbose_flag) {
Ok(()) => 0,
Err(e) => e
}
} }
fn print_help(opts: &[getopts::OptGroup]) { fn print_help(opts: &[getopts::OptGroup]) {
@ -97,7 +100,9 @@ fn print_help(opts: &[getopts::OptGroup]) {
/** /**
* Create the list of new directories * Create the list of new directories
*/ */
fn exec(dirs: Vec<String>, mk_parents: bool, mode: FilePermission, verbose: bool) { fn exec(dirs: Vec<String>, mk_parents: bool, mode: FilePermission, verbose: bool) -> Result<(), int> {
let mut result = Ok(());
let mut parent_dirs = Vec::new(); let mut parent_dirs = Vec::new();
if mk_parents { if mk_parents {
for dir in dirs.iter() { for dir in dirs.iter() {
@ -116,7 +121,10 @@ fn exec(dirs: Vec<String>, mk_parents: bool, mode: FilePermission, verbose: bool
} }
// Recursively build parent dirs that are needed // Recursively build parent dirs that are needed
if !parent_dirs.is_empty() { if !parent_dirs.is_empty() {
exec(parent_dirs, mk_parents, mode, verbose); match exec(parent_dirs, mk_parents, mode, verbose) {
Ok(()) => ( /* keep going */ ),
Err(e) => result = Err(e)
}
} }
for dir in dirs.iter() { for dir in dirs.iter() {
@ -138,9 +146,12 @@ fn exec(dirs: Vec<String>, mk_parents: bool, mode: FilePermission, verbose: bool
} else { } else {
format!("directory '{}' already exists", *dir) format!("directory '{}' already exists", *dir)
}; };
show_error!(1, "{}", error_msg); show_error!("{}", error_msg);
result = Err(1)
} }
} }
result
} }
/** /**

View file

@ -24,9 +24,9 @@ static NAME: &'static str = "paste";
static VERSION: &'static str = "1.0.0"; static VERSION: &'static str = "1.0.0";
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).clone(); let program = args.get(0).clone();
let opts = [ let opts = [
@ -56,6 +56,8 @@ pub fn uumain(args: Vec<String>) {
}; };
paste(matches.free, serial, delimiters.as_slice()); paste(matches.free, serial, delimiters.as_slice());
} }
0
} }
fn paste(filenames: Vec<String>, serial: bool, delimiters: &str) { fn paste(filenames: Vec<String>, serial: bool, delimiters: &str) {

View file

@ -25,9 +25,9 @@ mod util;
static NAME: &'static str = "printenv"; static NAME: &'static str = "printenv";
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).clone(); let program = args.get(0).clone();
let opts = [ let opts = [
getopts::optflag("0", "null", "end each output line with 0 byte rather than newline"), getopts::optflag("0", "null", "end each output line with 0 byte rather than newline"),
@ -47,11 +47,11 @@ pub fn uumain(args: Vec<String>) {
println!(" {0:s} [VARIABLE]... [OPTION]...", program); println!(" {0:s} [VARIABLE]... [OPTION]...", program);
println!(""); println!("");
print(getopts::usage("Prints the given environment VARIABLE(s), otherwise prints them all.", opts).as_slice()); print(getopts::usage("Prints the given environment VARIABLE(s), otherwise prints them all.", opts).as_slice());
return; return 0;
} }
if matches.opt_present("version") { if matches.opt_present("version") {
println!("printenv 1.0.0"); println!("printenv 1.0.0");
return; return 0;
} }
let mut separator = "\n"; let mut separator = "\n";
if matches.opt_present("null") { if matches.opt_present("null") {
@ -59,6 +59,8 @@ pub fn uumain(args: Vec<String>) {
}; };
exec(matches.free, separator); exec(matches.free, separator);
0
} }
pub fn exec(args: Vec<String>, separator: &str) { pub fn exec(args: Vec<String>, separator: &str) {

View file

@ -24,9 +24,9 @@ static NAME: &'static str = "pwd";
static VERSION: &'static str = "1.0.0"; static VERSION: &'static str = "1.0.0";
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).clone(); let program = args.get(0).clone();
let opts = [ let opts = [
getopts::optflag("", "help", "display this help and exit"), getopts::optflag("", "help", "display this help and exit"),
@ -48,9 +48,15 @@ pub fn uumain(args: Vec<String>) {
println!(""); println!("");
print(getopts::usage("Print the full filename of the current working directory.", opts).as_slice()); print(getopts::usage("Print the full filename of the current working directory.", opts).as_slice());
} else if matches.opt_present("version") { } else if matches.opt_present("version") {
return println!("pwd version: {}", VERSION); println!("pwd version: {}", VERSION);
return 0;
} else { } else {
let cwd = std::os::getcwd(); let cwd = std::os::getcwd();
println!("{}", cwd.display()); println!("{}", cwd.display());
return 0;
} }
0
} }

View file

@ -30,9 +30,9 @@ enum InteractiveMode {
static NAME: &'static str = "rm"; static NAME: &'static str = "rm";
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).clone(); let program = args.get(0).clone();
// TODO: make getopts support -R in addition to -r // TODO: make getopts support -R in addition to -r
@ -79,8 +79,9 @@ pub fn uumain(args: Vec<String>) {
} 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_error!("missing an argument");
show_error!(1, "for help, try '{0:s} --help'", program) show_error!("for help, try '{0:s} --help'", program);
return 1;
} else { } else {
let force = matches.opt_present("force"); let force = matches.opt_present("force");
let interactive = let interactive =
@ -113,16 +114,23 @@ pub fn uumain(args: Vec<String>) {
"Remove all arguments? " "Remove all arguments? "
}; };
if !prompt(msg) { if !prompt(msg) {
return; 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
} }
}
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> {
let mut r = Ok(());
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);
@ -135,30 +143,34 @@ 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); r = remove(walk_dir.map(|x| x.as_str().unwrap().to_string()).collect(), force, interactive, one_fs, preserve_root, recursive, dir, verbose).and(r);
remove_dir(&file, filename, interactive, verbose); r = remove_dir(&file, filename, interactive, verbose).and(r);
} else if dir && (filename != "/" || !preserve_root) { } else if dir && (filename != "/" || !preserve_root) {
remove_dir(&file, filename, interactive, verbose); r = remove_dir(&file, filename, interactive, verbose).and(r);
} else { } else {
if recursive { if recursive {
show_error!(1, "could not remove directory '{}'", show_error!("could not remove directory '{}'",
filename); filename);
r = Err(1);
} else { } else {
show_error!(1, show_error!("could not remove directory '{}' (did you mean to pass '-r'?)",
"could not remove directory '{}' (did you mean to pass '-r'?)",
filename); filename);
r = Err(1);
} }
} }
} else { } else {
remove_file(&file, filename.as_slice(), interactive, verbose); r = remove_file(&file, filename.as_slice(), interactive, verbose).and(r);
} }
} else if !force { } else if !force {
show_error!(1, "no such file or directory '{}'", filename); show_error!("no such file or directory '{}'", filename);
r = Err(1);
} }
} }
r
} }
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)
@ -169,13 +181,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_error!("{}", f.to_str());
return Err(1);
} }
} }
} }
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)
@ -186,10 +201,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_error!("{}", f.to_str());
return Err(1);
} }
} }
} }
Ok(())
} }
fn prompt_file(path: &Path, name: &str) -> bool { fn prompt_file(path: &Path, name: &str) -> bool {

View file

@ -23,9 +23,9 @@ mod util;
static NAME: &'static str = "rmdir"; static NAME: &'static str = "rmdir";
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).clone(); let program = args.get(0).clone();
let opts = [ let opts = [
@ -38,8 +38,8 @@ pub fn uumain(args: Vec<String>) {
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_error!("{}", f.to_err_msg());
return; return 1;
} }
}; };
@ -53,39 +53,54 @@ pub fn uumain(args: Vec<String>) {
} 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_error!("missing an argument");
show_error!(1, "for help, try '{0:s} --help'", program); show_error!("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
} }
}
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>{
let mut r = Ok(());
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); r = remove_dir(&path, dir.as_slice(), ignore, parents, verbose).and(r);
} else { } else {
show_error!(1, "failed to remove '{}' (file)", *dir); show_error!("failed to remove '{}' (file)", *dir);
r = Err(1);
} }
} else { } else {
show_error!(1, "no such file or directory '{}'", *dir); show_error!("no such file or directory '{}'", *dir);
r = Err(1);
} }
} }
r
} }
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_error!("{}", f.to_str());
return; return Err(1);
} }
}; };
let mut r = Ok(());
if walk_dir.next() == None { if walk_dir.next() == None {
match fs::rmdir(path) { match fs::rmdir(path) {
Ok(_) => { Ok(_) => {
@ -95,16 +110,20 @@ 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); r = remove_dir(&Path::new(dirname), dirname, ignore, parents, verbose).and(r);
} }
} }
} }
Err(f) => { Err(f) => {
show_error!(1, "{}", f.to_str()); show_error!("{}", f.to_str());
r = Err(1);
} }
} }
} else if !ignore { } else if !ignore {
show_error!(1, "Failed to remove directory '{}' (non-empty)", dir); show_error!("Failed to remove directory '{}' (non-empty)", dir);
r = Err(1);
} }
r
} }

View file

@ -34,9 +34,9 @@ fn escape_sequences(s: &str) -> String {
} }
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let opts = [ let opts = [
getopts::optopt("s", "separator", "Separator character (defaults to \\n)", ""), getopts::optopt("s", "separator", "Separator character (defaults to \\n)", ""),
getopts::optopt("t", "terminator", "Terminator character (defaults to separator)", ""), getopts::optopt("t", "terminator", "Terminator character (defaults to separator)", ""),
@ -47,28 +47,27 @@ pub fn uumain(args: Vec<String>) {
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_error!("{:s}", f.to_err_msg());
print_usage(opts); print_usage(opts);
return; return 1;
} }
}; };
if matches.opt_present("help") { if matches.opt_present("help") {
print_usage(opts); print_usage(opts);
return; return 0;
} }
if matches.opt_present("version") { if matches.opt_present("version") {
println!("seq 1.0.0"); println!("seq 1.0.0");
return; return 0;
} }
if matches.free.len() < 1 || matches.free.len() > 3 { if matches.free.len() < 1 || matches.free.len() > 3 {
os::set_exit_status(1);
print_usage(opts); print_usage(opts);
return; return 1;
} }
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; } Err(s) => { show_error!("{:s}", s); return 1; }
} }
} else { } else {
1.0 1.0
@ -76,18 +75,20 @@ pub fn uumain(args: Vec<String>) {
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; } Err(s) => { show_error!("{: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; } Err(s) => { show_error!("{: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());
print_seq(first, step, last, separator, terminator, matches.opt_present("w")); print_seq(first, step, last, separator, terminator, matches.opt_present("w"));
0
} }
fn done_printing(next: f32, step: f32, last: f32) -> bool { fn done_printing(next: f32, step: f32, last: f32) -> bool {

View file

@ -24,9 +24,9 @@ mod util;
static NAME: &'static str = "sleep"; static NAME: &'static str = "sleep";
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).clone(); let program = args.get(0).clone();
let opts = [ let opts = [
@ -36,8 +36,8 @@ pub fn uumain(args: Vec<String>) {
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_error!("{}", f.to_err_msg());
return return 1;
} }
}; };
@ -57,11 +57,14 @@ 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_error!("missing an argument");
show_error!(1, "for help, try '{0:s} --help'", program); show_error!("for help, try '{0:s} --help'", program);
return 1;
} else { } else {
sleep(matches.free); sleep(matches.free);
} }
0
} }
fn sleep(args: Vec<String>) { fn sleep(args: Vec<String>) {

View file

@ -77,9 +77,9 @@ fn open(name: &str) -> IoResult<Box<Reader>> {
} }
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).as_slice(); let program = args.get(0).as_slice();
let opts = [ let opts = [
getopts::optflag("r", "", "use the BSD compatible algorithm (default)"), getopts::optflag("r", "", "use the BSD compatible algorithm (default)"),
@ -102,11 +102,11 @@ pub fn uumain(args: Vec<String>) {
print(getopts::usage("checksum and count the blocks in a file", opts).as_slice()); print(getopts::usage("checksum and count the blocks in a file", opts).as_slice());
println!(""); println!("");
println!("With no FILE, or when FILE is -, read standard input."); println!("With no FILE, or when FILE is -, read standard input.");
return; return 0;
} }
if matches.opt_present("version") { if matches.opt_present("version") {
println!("{} {}", program, VERSION); println!("{} {}", program, VERSION);
return; return 0;
} }
let sysv = matches.opt_present("sysv"); let sysv = matches.opt_present("sysv");
@ -128,4 +128,6 @@ pub fn uumain(args: Vec<String>) {
}; };
println!("{} {}", sum, blocks); println!("{} {}", sum, blocks);
0
} }

View file

@ -24,9 +24,9 @@ static NAME: &'static str = "tac";
static VERSION: &'static str = "1.0.0"; static VERSION: &'static str = "1.0.0";
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).clone(); let program = args.get(0).clone();
let opts = [ let opts = [
@ -69,6 +69,8 @@ pub fn uumain(args: Vec<String>) {
}; };
tac(files, before, regex, separator.as_slice()); tac(files, before, regex, separator.as_slice());
} }
0
} }
fn tac(filenames: Vec<String>, before: bool, _: bool, separator: &str) { fn tac(filenames: Vec<String>, before: bool, _: bool, separator: &str) {

View file

@ -13,24 +13,24 @@
*/ */
extern crate getopts; extern crate getopts;
#[phase(syntax, link)] extern crate log; #[phase(plugin, link)] extern crate log;
use std::io::{println, stdin, stdout, Append, File, Truncate, Write}; use std::io::{println, stdin, stdout, Append, File, Truncate, Write};
use std::io::{IoResult}; use std::io::{IoResult};
use std::io::util::{copy, NullWriter, MultiWriter}; use std::io::util::{copy, NullWriter, MultiWriter};
use std::os::{args, set_exit_status}; use std::os;
use getopts::{getopts, optflag, usage}; use getopts::{getopts, optflag, usage};
static NAME: &'static str = "tee"; static NAME: &'static str = "tee";
static VERSION: &'static str = "1.0.0"; static VERSION: &'static str = "1.0.0";
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
match options(args.as_slice()).and_then(exec) { match options(args.as_slice()).and_then(exec) {
Ok(_) => set_exit_status(0), Ok(_) => 0,
Err(_) => set_exit_status(1) Err(_) => 1
} }
} }
@ -153,5 +153,5 @@ fn with_path<T>(path: &Path, cb: || -> IoResult<T>) -> IoResult<T> {
} }
fn warn(message: &str) { fn warn(message: &str) {
error!("{}: {}", args().get(0), message); error!("{}: {}", os::args().get(0), message);
} }

View file

@ -23,9 +23,9 @@ static NAME: &'static str = "touch";
static VERSION: &'static str = "1.0.0"; static VERSION: &'static str = "1.0.0";
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let opts = [ let opts = [
getopts::optflag("a", "", "change only the access time"), getopts::optflag("a", "", "change only the access time"),
getopts::optflag("c", "no-create", "do not create any files"), getopts::optflag("c", "no-create", "do not create any files"),
@ -49,7 +49,7 @@ pub fn uumain(args: Vec<String>) {
if matches.opt_present("version") { if matches.opt_present("version") {
println!("{:s} {:s}", NAME, VERSION); println!("{:s} {:s}", NAME, VERSION);
return; return 0;
} }
if matches.opt_present("help") || matches.free.is_empty() { if matches.opt_present("help") || matches.free.is_empty() {
@ -59,7 +59,7 @@ pub fn uumain(args: Vec<String>) {
println!(""); println!("");
println!("{:s}", getopts::usage("Update the access and modification times of \ println!("{:s}", getopts::usage("Update the access and modification times of \
each FILE to the current time.", opts)); each FILE to the current time.", opts));
return; return 0;
} }
if matches.opt_present("date") && matches.opts_present(["reference".to_string(), "t".to_string()]) || if matches.opt_present("date") && matches.opts_present(["reference".to_string(), "t".to_string()]) ||
@ -131,6 +131,8 @@ pub fn uumain(args: Vec<String>) {
Err(e) => fail!("Unable to modify times\n{}", e.desc) Err(e) => fail!("Unable to modify times\n{}", e.desc)
} }
} }
0
} }
fn stat(path: &Path, follow: bool) -> std::io::FileStat { fn stat(path: &Path, follow: bool) -> std::io::FileStat {

View file

@ -147,9 +147,9 @@ fn usage(opts: &[OptGroup]) {
} }
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let opts = [ let opts = [
getopts::optflag("c", "complement", "use the complement of SET1"), getopts::optflag("c", "complement", "use the complement of SET1"),
getopts::optflag("C", "", "same as -c"), getopts::optflag("C", "", "same as -c"),
@ -161,25 +161,24 @@ pub fn uumain(args: Vec<String>) {
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_error!("{}", err.to_err_msg());
return; return 1;
} }
}; };
if matches.opt_present("help") { if matches.opt_present("help") {
usage(opts); usage(opts);
return; return 0;
} }
if matches.opt_present("version") { if matches.opt_present("version") {
println!("{} {}", NAME, VERSION); println!("{} {}", NAME, VERSION);
return; return 0;
} }
if matches.free.len() == 0 { if matches.free.len() == 0 {
usage(opts); usage(opts);
os::set_exit_status(1); return 1;
return;
} }
let dflag = matches.opt_present("d"); let dflag = matches.opt_present("d");
@ -187,8 +186,8 @@ pub fn uumain(args: Vec<String>) {
let sets = matches.free; let sets = matches.free;
if cflag && !dflag { if cflag && !dflag {
show_error!(1, "-c is only supported with -d"); show_error!("-c is only supported with -d");
return; return 1;
} }
if dflag { if dflag {
@ -199,4 +198,6 @@ pub fn uumain(args: Vec<String>) {
let set2 = expand_set(sets.get(1).as_slice()); let set2 = expand_set(sets.get(1).as_slice());
tr(set1.as_slice(), set2.as_slice()); tr(set1.as_slice(), set2.as_slice());
} }
0
} }

View file

@ -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,
@ -47,9 +35,9 @@ enum TruncateMode {
static NAME: &'static str = "truncate"; static NAME: &'static str = "truncate";
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).clone(); let program = args.get(0).clone();
let opts = [ let opts = [
@ -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_error!("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,12 +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
} }
} }
}
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())) {
@ -119,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_error!("{}", f.to_str());
return Err(1);
}
}
} }
None => parse_size(size.unwrap().as_slice()) None => parse_size(size.unwrap().as_slice())
}; };
@ -129,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,
@ -142,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_error!("{}", f.to_str());
return Err(1);
} }
} }
} }
Err(f) => { Err(f) => {
show_error!(1, "{}", f.to_str()); show_error!("{}", f.to_str());
return Err(1);
} }
} }
} }
} }
Ok(())
} }
fn parse_size(size: &str) -> (u64, TruncateMode) { fn parse_size(size: &str) -> (u64, TruncateMode) {

View file

@ -35,9 +35,9 @@ extern {
static NAME: &'static str = "tty"; static NAME: &'static str = "tty";
#[allow(dead_code)] #[allow(dead_code)]
fn main () { uumain(os::args()); } fn main () { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let options = [ let options = [
optflag("s", "silent", "print nothing, only return an exit status") optflag("s", "silent", "print nothing, only return an exit status")
]; ];
@ -49,7 +49,7 @@ pub fn uumain(args: Vec<String>) {
Err(f) => { Err(f) => {
println(f.to_err_msg().as_slice()); println(f.to_err_msg().as_slice());
usage(); usage();
return return 2;
} }
}; };
@ -71,10 +71,9 @@ pub fn uumain(args: Vec<String>) {
} }
}; };
os::set_exit_status(exit_code as int); exit_code as int
} }
fn usage () { fn usage () {
safe_writeln!(&mut stderr() as &mut Writer, "usage: tty [-s]"); safe_writeln!(&mut stderr() as &mut Writer, "usage: tty [-s]");
os::set_exit_status(2);
} }

View file

@ -52,9 +52,9 @@ unsafe fn getuname() -> utsrust {
static NAME: &'static str = "uname"; static NAME: &'static str = "uname";
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).as_slice(); let program = args.get(0).as_slice();
let opts = [ let opts = [
getopts::optflag("h", "help", "display this help and exit"), getopts::optflag("h", "help", "display this help and exit"),
@ -77,7 +77,7 @@ pub fn uumain(args: Vec<String>) {
println!(" {:s}", program); println!(" {:s}", program);
println!(""); println!("");
print(getopts::usage("The uname utility writes symbols representing one or more system characteristics to the standard output.", opts).as_slice()); print(getopts::usage("The uname utility writes symbols representing one or more system characteristics to the standard output.", opts).as_slice());
return; return 0;
} }
let uname = unsafe { getuname() }; let uname = unsafe { getuname() };
let mut output = String::new(); let mut output = String::new();
@ -103,5 +103,7 @@ pub fn uumain(args: Vec<String>) {
output.push_str(uname.machine.as_slice()); output.push_str(uname.machine.as_slice());
output.push_str(" "); output.push_str(" ");
} }
println!("{}", output.as_slice().trim_left()) println!("{}", output.as_slice().trim_left());
0
} }

View file

@ -27,9 +27,9 @@ mod util;
static NAME: &'static str = "unlink"; static NAME: &'static str = "unlink";
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).clone(); let program = args.get(0).clone();
let opts = [ let opts = [
getopts::optflag("h", "help", "display this help and exit"), getopts::optflag("h", "help", "display this help and exit"),
@ -50,12 +50,12 @@ pub fn uumain(args: Vec<String>) {
println!(" {0:s} [FILE]... [OPTION]...", program); println!(" {0:s} [FILE]... [OPTION]...", program);
println!(""); println!("");
print(getopts::usage("Unlink the file at [FILE].", opts).as_slice()); print(getopts::usage("Unlink the file at [FILE].", opts).as_slice());
return; return 0;
} }
if matches.opt_present("version") { if matches.opt_present("version") {
println!("unlink 1.0.0"); println!("unlink 1.0.0");
return; return 0;
} }
if matches.free.len() == 0 { if matches.free.len() == 0 {
@ -86,4 +86,6 @@ pub fn uumain(args: Vec<String>) {
crash!(1, "cannot unlink '{0}': {1}", path.display(), e.desc); crash!(1, "cannot unlink '{0}': {1}", path.display(), e.desc);
} }
} }
0
} }

View file

@ -48,9 +48,9 @@ extern {
} }
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).clone(); let program = args.get(0).clone();
let opts = [ let opts = [
getopts::optflag("v", "version", "output version information and exit"), getopts::optflag("v", "version", "output version information and exit"),
@ -62,7 +62,7 @@ pub fn uumain(args: Vec<String>) {
}; };
if matches.opt_present("version") { if matches.opt_present("version") {
println!("uptime 1.0.0"); println!("uptime 1.0.0");
return; return 0;
} }
if matches.opt_present("help") || matches.free.len() > 0 { if matches.opt_present("help") || matches.free.len() > 0 {
println!("Usage:"); println!("Usage:");
@ -71,7 +71,7 @@ pub fn uumain(args: Vec<String>) {
print(getopts::usage("Print the current time, the length of time the system has been up,\n\ print(getopts::usage("Print the current time, the length of time the system has been up,\n\
the number of users on the system, and the average number of jobs\n\ the number of users on the system, and the average number of jobs\n\
in the run queue over the last 1, 5 and 15 minutes.", opts).as_slice()); in the run queue over the last 1, 5 and 15 minutes.", opts).as_slice());
return; return 0;
} }
print_time(); print_time();
@ -80,6 +80,8 @@ pub fn uumain(args: Vec<String>) {
print_uptime(upsecs); print_uptime(upsecs);
print_nusers(user_count); print_nusers(user_count);
print_loadavg(); print_loadavg();
0
} }
fn print_loadavg() { fn print_loadavg() {

View file

@ -48,9 +48,9 @@ extern {
static NAME: &'static str = "users"; static NAME: &'static str = "users";
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).as_slice(); let program = args.get(0).as_slice();
let opts = [ let opts = [
getopts::optflag("h", "help", "display this help and exit"), getopts::optflag("h", "help", "display this help and exit"),
@ -69,12 +69,12 @@ pub fn uumain(args: Vec<String>) {
println!(" {:s} [OPTION]... [FILE]", program); println!(" {:s} [OPTION]... [FILE]", program);
println!(""); println!("");
print(getopts::usage("Output who is currently logged in according to FILE.", opts).as_slice()); print(getopts::usage("Output who is currently logged in according to FILE.", opts).as_slice());
return; return 0;
} }
if matches.opt_present("version") { if matches.opt_present("version") {
println!("users 1.0.0"); println!("users 1.0.0");
return; return 0;
} }
let mut filename = DEFAULT_FILE; let mut filename = DEFAULT_FILE;
@ -83,6 +83,8 @@ pub fn uumain(args: Vec<String>) {
} }
exec(filename); exec(filename);
0
} }
fn exec(filename: &str) { fn exec(filename: &str) {

View file

@ -9,7 +9,6 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
extern crate collections;
extern crate getopts; extern crate getopts;
extern crate base64; extern crate base64;
@ -54,14 +53,14 @@ extern crate whoami;
extern crate yes; extern crate yes;
use std::os; use std::os;
use collections::hashmap::HashMap; use std::collections::hashmap::HashMap;
static NAME: &'static str = "uutils"; static NAME: &'static str = "uutils";
static VERSION: &'static str = "1.0.0"; static VERSION: &'static str = "1.0.0";
fn util_map() -> HashMap<&str, fn(Vec<String>)> { fn util_map() -> HashMap<&str, fn(Vec<String>) -> int> {
fn uutrue(_: Vec<String>) { os::set_exit_status(0); } fn uutrue(_: Vec<String>) -> int { 0 }
fn uufalse(_: Vec<String>) { os::set_exit_status(1); } fn uufalse(_: Vec<String>) -> int { 1 }
let mut map = HashMap::new(); let mut map = HashMap::new();
map.insert("base64", base64::uumain); map.insert("base64", base64::uumain);
@ -109,7 +108,7 @@ fn util_map() -> HashMap<&str, fn(Vec<String>)> {
map map
} }
fn usage(cmap: &HashMap<&str, fn(Vec<String>)>) { fn usage(cmap: &HashMap<&str, fn(Vec<String>) -> int>) {
println!("{} {}", NAME, VERSION); println!("{} {}", NAME, VERSION);
println!(""); println!("");
println!("Usage:"); println!("Usage:");
@ -132,7 +131,7 @@ fn main() {
let binary_as_util = binary.filename_str().unwrap(); let binary_as_util = binary.filename_str().unwrap();
if umap.contains_key(&binary_as_util) { if umap.contains_key(&binary_as_util) {
let &uumain = umap.get(&binary_as_util); let &uumain = umap.get(&binary_as_util);
uumain(args); os::set_exit_status(uumain(args));
return return
} else if binary_as_util.starts_with("uutils") } else if binary_as_util.starts_with("uutils")
|| binary_as_util.starts_with("busybox") { || binary_as_util.starts_with("busybox") {
@ -151,7 +150,7 @@ fn main() {
let util = args.get(0).as_slice(); let util = args.get(0).as_slice();
if umap.contains_key(&util) { if umap.contains_key(&util) {
let &uumain = umap.get(&util); let &uumain = umap.get(&util);
uumain(args.clone()); os::set_exit_status(uumain(args.clone()));
return return
} else if args.get(0).as_slice() == "--help" { } else if args.get(0).as_slice() == "--help" {
// see if they want help on a specific util // see if they want help on a specific util
@ -159,7 +158,7 @@ fn main() {
let util = args.get(1).as_slice(); let util = args.get(1).as_slice();
if umap.contains_key(&util) { if umap.contains_key(&util) {
let &uumain = umap.get(&util); let &uumain = umap.get(&util);
uumain(vec!["--help".to_string()]); os::set_exit_status(uumain(vec!["--help".to_string()]));
return return
} else { } else {
println!("{}: applet not found", util); println!("{}: applet not found", util);
@ -168,6 +167,7 @@ fn main() {
} }
} }
usage(&umap); usage(&umap);
os::set_exit_status(0);
return return
} else { } else {
println!("{}: applet not found", util); println!("{}: applet not found", util);
@ -177,6 +177,7 @@ fn main() {
} else { } else {
// no arguments provided // no arguments provided
usage(&umap); usage(&umap);
os::set_exit_status(0);
return return
} }
} }

View file

@ -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"]
@ -34,9 +35,9 @@ struct Result {
static NAME: &'static str = "wc"; static NAME: &'static str = "wc";
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).clone(); let program = args.get(0).clone();
let opts = [ let opts = [
getopts::optflag("c", "bytes", "print the byte counts"), getopts::optflag("c", "bytes", "print the byte counts"),
@ -62,12 +63,12 @@ pub fn uumain(args: Vec<String>) {
print(getopts::usage("Print newline, word and byte counts for each FILE", opts).as_slice()); print(getopts::usage("Print newline, word and byte counts for each FILE", opts).as_slice());
println!(""); println!("");
println!("With no FILE, or when FILE is -, read standard input."); println!("With no FILE, or when FILE is -, read standard input.");
return; return 0;
} }
if matches.opt_present("version") { if matches.opt_present("version") {
println!("wc 1.0.0"); println!("wc 1.0.0");
return; return 0;
} }
let mut files = matches.free.clone(); let mut files = matches.free.clone();
@ -75,7 +76,12 @@ pub fn uumain(args: Vec<String>) {
files = vec!("-".to_string()); files = vec!("-".to_string());
} }
wc(files, &matches); match wc(files, &matches) {
Ok(()) => ( /* pass */ ),
Err(e) => return e
}
0
} }
static CR: u8 = '\r' as u8; static CR: u8 = '\r' as u8;
@ -89,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;
@ -101,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;
@ -185,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);
} }
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,
@ -224,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)); Ok(BufferedReader::new(reader))
}, },
Err(e) => { Err(e) => {
show_error!(1, "wc: {0:s}: {1:s}", path, e.desc.to_str()); show_error!("wc: {0:s}: {1:s}", path, e.desc.to_str());
Err(1)
} }
} }
None
} }

View file

@ -42,9 +42,9 @@ unsafe fn getusername() -> String {
static NAME: &'static str = "whoami"; static NAME: &'static str = "whoami";
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).as_slice(); let program = args.get(0).as_slice();
let opts = [ let opts = [
getopts::optflag("h", "help", "display this help and exit"), getopts::optflag("h", "help", "display this help and exit"),
@ -61,14 +61,16 @@ pub fn uumain(args: Vec<String>) {
println!(" {:s}", program); println!(" {:s}", program);
println!(""); println!("");
print(getopts::usage("print effective userid", opts).as_slice()); print(getopts::usage("print effective userid", opts).as_slice());
return; return 0;
} }
if matches.opt_present("version") { if matches.opt_present("version") {
println!("whoami 1.0.0"); println!("whoami 1.0.0");
return; return 0;
} }
exec(); exec();
0
} }
pub fn exec() { pub fn exec() {

View file

@ -25,9 +25,9 @@ mod util;
static NAME: &'static str = "yes"; static NAME: &'static str = "yes";
#[allow(dead_code)] #[allow(dead_code)]
fn main() { uumain(os::args()); } fn main() { os::set_exit_status(uumain(os::args())); }
pub fn uumain(args: Vec<String>) { pub fn uumain(args: Vec<String>) -> int {
let program = args.get(0).clone(); let program = args.get(0).clone();
let opts = [ let opts = [
getopts::optflag("h", "help", "display this help and exit"), getopts::optflag("h", "help", "display this help and exit"),
@ -46,11 +46,11 @@ pub fn uumain(args: Vec<String>) {
println!(" {0:s} [STRING]... [OPTION]...", program); println!(" {0:s} [STRING]... [OPTION]...", program);
println!(""); println!("");
print(getopts::usage("Repeatedly output a line with all specified STRING(s), or 'y'.", opts).as_slice()); print(getopts::usage("Repeatedly output a line with all specified STRING(s), or 'y'.", opts).as_slice());
return; return 0;
} }
if matches.opt_present("version") { if matches.opt_present("version") {
println!("yes 1.0.0"); println!("yes 1.0.0");
return; return 0;
} }
let mut string = "y".to_string(); let mut string = "y".to_string();
if !matches.free.is_empty() { if !matches.free.is_empty() {
@ -58,6 +58,8 @@ pub fn uumain(args: Vec<String>) {
} }
exec(string.as_slice()); exec(string.as_slice());
0
} }
pub fn exec(string: &str) { pub fn exec(string: &str) {