From e905c2ec717aa433bff238e0eba67d460dba1cc2 Mon Sep 17 00:00:00 2001 From: Wim Hueskes Date: Sat, 23 Jul 2016 23:18:02 +0200 Subject: [PATCH] od: do not panic on invalid user input use macros from uucore where possible --- src/od/multifilereader.rs | 5 +++-- src/od/od.rs | 15 ++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/od/multifilereader.rs b/src/od/multifilereader.rs index 2e3d2d909..c2bcb688e 100644 --- a/src/od/multifilereader.rs +++ b/src/od/multifilereader.rs @@ -53,8 +53,9 @@ impl<'b> MultifileReader<'b> { // print an error at the time that the file is needed, // then move on the the next file. // This matches the behavior of the original `od` - let _ = - writeln!(&mut std::io::stderr(), "od: '{}': {}", fname, e); + eprintln!("{}: '{}': {}", + executable!().split("::").next().unwrap(), // remove module + fname, e); self.any_err = true } } diff --git a/src/od/od.rs b/src/od/od.rs index f46ed8c64..707b75ee1 100644 --- a/src/od/od.rs +++ b/src/od/od.rs @@ -39,7 +39,6 @@ macro_rules! hashmap { }} } -static NAME: &'static str = "od"; static VERSION: &'static str = env!("CARGO_PKG_VERSION"); #[derive(Debug)] @@ -86,7 +85,10 @@ pub fn uumain(args: Vec) -> i32 { let matches = match opts.parse(&args[1..]) { Ok(m) => m, - Err(f) => panic!("Invalid options\n{}", f) + Err(f) => { + disp_err!("{}", f); + return 1; + } }; if matches.opt_present("h") { @@ -94,18 +96,21 @@ pub fn uumain(args: Vec) -> i32 { Usage: {0} [OPTION]... [FILENAME]... - Displays data in various human-readable formats.", NAME)); + Displays data in various human-readable formats.", executable!())); println!("{}", opts.usage(&msg)); return 0; } if matches.opt_present("version") { - println!("{} {}", NAME, VERSION); + println!("{} {}", executable!(), VERSION); return 0; } let input_offset_base = match parse_radix(matches.opt_str("A")) { Ok(r) => r, - Err(f) => { panic!("Invalid -A/--address-radix\n{}", f) } + Err(f) => { + disp_err!("Invalid -A/--address-radix\n{}", f); + return 1; + } }; // Gather up file names - args which don't start with '-'