1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

readlink: fix -n and -z no delimiter at the end

This commit is contained in:
Niyaz Nigmatullin 2022-08-16 08:21:12 +03:00
parent 6879ed7b34
commit 391143fe5b

View file

@ -36,7 +36,7 @@ const ARG_FILES: &str = "files";
pub fn uumain(args: impl uucore::Args) -> UResult<()> { pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().get_matches_from(args); let matches = uu_app().get_matches_from(args);
let mut no_newline = matches.contains_id(OPT_NO_NEWLINE); let mut no_trailing_delimiter = matches.contains_id(OPT_NO_NEWLINE);
let use_zero = matches.contains_id(OPT_ZERO); let use_zero = matches.contains_id(OPT_ZERO);
let silent = matches.contains_id(OPT_SILENT) || matches.contains_id(OPT_QUIET); let silent = matches.contains_id(OPT_SILENT) || matches.contains_id(OPT_QUIET);
let verbose = matches.contains_id(OPT_VERBOSE); let verbose = matches.contains_id(OPT_VERBOSE);
@ -66,9 +66,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
return Err(UUsageError::new(1, "missing operand")); return Err(UUsageError::new(1, "missing operand"));
} }
if no_newline && files.len() > 1 && !silent { if no_trailing_delimiter && files.len() > 1 && !silent {
show_error!("ignoring --no-newline with multiple arguments"); show_error!("ignoring --no-newline with multiple arguments");
no_newline = false; no_trailing_delimiter = false;
} }
for f in &files { for f in &files {
@ -79,7 +79,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
canonicalize(&p, can_mode, res_mode) canonicalize(&p, can_mode, res_mode)
}; };
match path_result { match path_result {
Ok(path) => show(&path, no_newline, use_zero).map_err_context(String::new)?, Ok(path) => show(&path, no_trailing_delimiter, use_zero).map_err_context(String::new)?,
Err(err) => { Err(err) => {
if verbose { if verbose {
return Err(USimpleError::new( return Err(USimpleError::new(
@ -167,12 +167,12 @@ pub fn uu_app<'a>() -> Command<'a> {
) )
} }
fn show(path: &Path, no_newline: bool, use_zero: bool) -> std::io::Result<()> { fn show(path: &Path, no_trailing_delimiter: bool, use_zero: bool) -> std::io::Result<()> {
let path = path.to_str().unwrap(); let path = path.to_str().unwrap();
if use_zero { if no_trailing_delimiter {
print!("{}\0", path);
} else if no_newline {
print!("{}", path); print!("{}", path);
} else if use_zero {
print!("{}\0", path);
} else { } else {
println!("{}", path); println!("{}", path);
} }