1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-03 14:37:45 +00:00

mknod: return UResult from uumain() function

This commit is contained in:
Jeffrey Finkelstein 2021-12-27 18:49:35 -05:00
parent f7584cb755
commit 882a293974

View file

@ -7,9 +7,6 @@
// spell-checker:ignore (ToDO) parsemode makedev sysmacros perror IFBLK IFCHR IFIFO // spell-checker:ignore (ToDO) parsemode makedev sysmacros perror IFBLK IFCHR IFIFO
#[macro_use]
extern crate uucore;
use std::ffi::CString; use std::ffi::CString;
use clap::{crate_version, App, Arg, ArgMatches}; use clap::{crate_version, App, Arg, ArgMatches};
@ -17,6 +14,7 @@ use libc::{dev_t, mode_t};
use libc::{S_IFBLK, S_IFCHR, S_IFIFO, S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR}; use libc::{S_IFBLK, S_IFCHR, S_IFIFO, S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR};
use uucore::display::Quotable; use uucore::display::Quotable;
use uucore::error::{set_exit_code, UResult, USimpleError, UUsageError};
use uucore::InvalidEncodingHandling; use uucore::InvalidEncodingHandling;
static ABOUT: &str = "Create the special file NAME of the given TYPE."; static ABOUT: &str = "Create the special file NAME of the given TYPE.";
@ -81,8 +79,8 @@ fn _mknod(file_name: &str, mode: mode_t, dev: dev_t) -> i32 {
} }
} }
#[allow(clippy::cognitive_complexity)] #[uucore_procs::gen_uumain]
pub fn uumain(args: impl uucore::Args) -> i32 { pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let args = args let args = args
.collect_str(InvalidEncodingHandling::Ignore) .collect_str(InvalidEncodingHandling::Ignore)
.accept_any(); .accept_any();
@ -92,13 +90,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
let matches = uu_app().get_matches_from(args); let matches = uu_app().get_matches_from(args);
let mode = match get_mode(&matches) { let mode = get_mode(&matches).map_err(|e| USimpleError::new(1, e))?;
Ok(mode) => mode,
Err(err) => {
show_error!("{}", err);
return 1;
}
};
let file_name = matches.value_of("name").expect("Missing argument 'NAME'"); let file_name = matches.value_of("name").expect("Missing argument 'NAME'");
@ -113,31 +105,29 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
if ch == 'p' { if ch == 'p' {
if matches.is_present("major") || matches.is_present("minor") { if matches.is_present("major") || matches.is_present("minor") {
eprintln!("Fifos do not have major and minor device numbers."); Err(UUsageError::new(
eprintln!( 1,
"Try '{} --help' for more information.", "Fifos do not have major and minor device numbers.",
uucore::execution_phrase() ))
);
1
} else { } else {
_mknod(file_name, S_IFIFO | mode, 0) let exit_code = _mknod(file_name, S_IFIFO | mode, 0);
set_exit_code(exit_code);
Ok(())
} }
} else { } else {
match (matches.value_of("major"), matches.value_of("minor")) { match (matches.value_of("major"), matches.value_of("minor")) {
(None, None) | (_, None) | (None, _) => { (None, None) | (_, None) | (None, _) => {
eprintln!("Special files require major and minor device numbers."); return Err(UUsageError::new(
eprintln!( 1,
"Try '{} --help' for more information.", "Special files require major and minor device numbers.",
uucore::execution_phrase() ));
);
1
} }
(Some(major), Some(minor)) => { (Some(major), Some(minor)) => {
let major = major.parse::<u64>().expect("validated by clap"); let major = major.parse::<u64>().expect("validated by clap");
let minor = minor.parse::<u64>().expect("validated by clap"); let minor = minor.parse::<u64>().expect("validated by clap");
let dev = makedev(major, minor); let dev = makedev(major, minor);
if ch == 'b' { let exit_code = if ch == 'b' {
// block special file // block special file
_mknod(file_name, S_IFBLK | mode, dev) _mknod(file_name, S_IFBLK | mode, dev)
} else if ch == 'c' || ch == 'u' { } else if ch == 'c' || ch == 'u' {
@ -145,7 +135,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
_mknod(file_name, S_IFCHR | mode, dev) _mknod(file_name, S_IFCHR | mode, dev)
} else { } else {
unreachable!("{} was validated to be only b, c or u", ch); unreachable!("{} was validated to be only b, c or u", ch);
} };
set_exit_code(exit_code);
Ok(())
} }
} }
} }