From 8ef06144a11c5487a9628527526e58d091234a7c Mon Sep 17 00:00:00 2001 From: Jimmy Lu Date: Mon, 2 Dec 2013 23:41:16 -0500 Subject: [PATCH 1/2] add basename --- README.md | 1 - basename/basename.rs | 126 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 basename/basename.rs diff --git a/README.md b/README.md index 0fe404faa..3b626ce87 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,6 @@ To do ----- - base64 -- basename - chcon - chgrp - chmod diff --git a/basename/basename.rs b/basename/basename.rs new file mode 100644 index 000000000..1e88ad024 --- /dev/null +++ b/basename/basename.rs @@ -0,0 +1,126 @@ +#[link(name="basename", vers="1.0.0", author="Jimmy Lu")]; + +/* + * This file is part of the uutils coreutils package. + * + * (c) Jimmy Lu + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +extern mod extra; + +use std::io::stderr; +use std::os; +use std::str; +use std::str::StrSlice; +use extra::getopts::groups; + +static VERSION: &'static str = "1.0.0"; + +fn main() { + + let args = os::args(); + + let program = strip_dir( &args[ 0 ].clone() ); + + // + // Argument parsing + // + + let opts = ~[ + groups::optflag("h", "help", "display this help and exit"), + groups::optflag("V", "version", "output version information and exit"), + ]; + + let matches = match groups::getopts(args.tail(), opts) { + Ok(m) => m, + Err(f) => { + let stderr = stderr(); + stderr.write_str( program + ": " + f.to_err_msg() + "\n" ); + os::set_exit_status(1); + return; + } + }; + + if matches.opt_present( "help" ) { + + println!( "Usage: {0:s} NAME [SUFFIX]", program ); + println!( " or: {0:s} OPTION", program ); + println( "Print NAME with any leading directory components removed." ); + println( "If specified, also remove a trailing SUFFIX." ); + + print( groups::usage( "", opts ) ); + + return; + } + + if matches.opt_present( "version" ) { + + println( program + " " + VERSION ); + return; + } + + // too few arguments + if args.len() < 2 { + + println( program + ": missing operand" ); + println( "Try `" + program + " --help' for more information." ); + return; + } + // too many arguments + else if args.len() > 3 { + + println( program + ": extra operand `" + args[ 3 ] + "'" ); + println( "Try `" + program + " --help' for more information." ); + return; + } + + // + // Main Program Processing + // + + let fullname = args[ 1 ].clone(); + + let mut name = strip_dir( &fullname ); + + if args.len() > 2 { + + let suffix = args[ 2 ].clone(); + name = strip_suffix( &name, &suffix ); + } + + println( name ); +} + +fn strip_dir( fullname :&~str ) -> ~str { + + let mut name = ~""; + + for c in fullname.rev_iter() { + + if c == '/' || c == '\\' { + return name; + } + + name = str::from_char( c ) + name; + + } + + return fullname.clone(); +} + +fn strip_suffix( name: &~str, suffix: &~str ) -> ~str { + + if name == suffix { + return name.clone(); + } + + if name.ends_with( *suffix ) { + + return name.slice_to( name.len() - suffix.len() ).into_owned(); + } + + return name.clone(); +} From 15465e21ecde2160f7c970c58f915ef72f1bba3f Mon Sep 17 00:00:00 2001 From: Jimmy Lu Date: Wed, 4 Dec 2013 10:41:32 -0500 Subject: [PATCH 2/2] update coding style and add basename to makefile --- Makefile | 1 + basename/basename.rs | 55 ++++++++++++++++++-------------------------- 2 files changed, 23 insertions(+), 33 deletions(-) diff --git a/Makefile b/Makefile index f225ed4d7..36de2be43 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,7 @@ EXES := \ wc \ whoami \ yes \ + basename \ TESTS := \ diff --git a/basename/basename.rs b/basename/basename.rs index 1e88ad024..07180f80f 100644 --- a/basename/basename.rs +++ b/basename/basename.rs @@ -20,10 +20,9 @@ use extra::getopts::groups; static VERSION: &'static str = "1.0.0"; fn main() { - let args = os::args(); - let program = strip_dir( &args[ 0 ].clone() ); + let program = strip_dir(&args[ 0 ].clone()); // // Argument parsing @@ -38,42 +37,38 @@ fn main() { Ok(m) => m, Err(f) => { let stderr = stderr(); - stderr.write_str( program + ": " + f.to_err_msg() + "\n" ); + stderr.write_str(program + ": " + f.to_err_msg() + "\n"); os::set_exit_status(1); return; } }; - if matches.opt_present( "help" ) { + if matches.opt_present("help") { + println!("Usage: {0:s} NAME [SUFFIX]", program); + println!(" or: {0:s} OPTION", program); + println("Print NAME with any leading directory components removed."); + println("If specified, also remove a trailing SUFFIX."); - println!( "Usage: {0:s} NAME [SUFFIX]", program ); - println!( " or: {0:s} OPTION", program ); - println( "Print NAME with any leading directory components removed." ); - println( "If specified, also remove a trailing SUFFIX." ); - - print( groups::usage( "", opts ) ); + print(groups::usage("", opts)); return; } - if matches.opt_present( "version" ) { - - println( program + " " + VERSION ); + if matches.opt_present("version") { + println(program + " " + VERSION); return; } // too few arguments if args.len() < 2 { - - println( program + ": missing operand" ); - println( "Try `" + program + " --help' for more information." ); + println(program + ": missing operand"); + println("Try `" + program + " --help' for more information."); return; } // too many arguments else if args.len() > 3 { - - println( program + ": extra operand `" + args[ 3 ] + "'" ); - println( "Try `" + program + " --help' for more information." ); + println(program + ": extra operand `" + args[ 3 ] + "'"); + println("Try `" + program + " --help' for more information."); return; } @@ -83,43 +78,37 @@ fn main() { let fullname = args[ 1 ].clone(); - let mut name = strip_dir( &fullname ); + let mut name = strip_dir(&fullname); if args.len() > 2 { - let suffix = args[ 2 ].clone(); - name = strip_suffix( &name, &suffix ); + name = strip_suffix(&name, &suffix); } - println( name ); + println(name); } -fn strip_dir( fullname :&~str ) -> ~str { - +fn strip_dir(fullname :&~str) -> ~str { let mut name = ~""; for c in fullname.rev_iter() { - if c == '/' || c == '\\' { return name; } - name = str::from_char( c ) + name; - + name = str::from_char(c) + name; } return fullname.clone(); } -fn strip_suffix( name: &~str, suffix: &~str ) -> ~str { - +fn strip_suffix(name: &~str, suffix: &~str) -> ~str { if name == suffix { return name.clone(); } - if name.ends_with( *suffix ) { - - return name.slice_to( name.len() - suffix.len() ).into_owned(); + if name.ends_with(*suffix) { + return name.slice_to(name.len() - suffix.len()).into_owned(); } return name.clone();