From 8a1628cf890144b1475a61aee6224d6edfe4b861 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 13 Dec 2020 12:06:44 +0100 Subject: [PATCH] fix(ls): When a file doesn't exist, exit early and fails ls Currently, running $ ls doesntexist will return 0 while it should return 1 Ditto for $ ls doesntexist a --- src/uu/ls/src/ls.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index d6c7d0d7b..fba6c600b 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -168,11 +168,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 { ) .parse(args); - list(matches); - 0 + list(matches) } -fn list(options: getopts::Matches) { +fn list(options: getopts::Matches) -> i32 { let locs: Vec = if options.free.is_empty() { vec![String::from(".")] } else { @@ -181,8 +180,16 @@ fn list(options: getopts::Matches) { let mut files = Vec::::new(); let mut dirs = Vec::::new(); + let mut has_failed = false; for loc in locs { let p = PathBuf::from(&loc); + if !p.exists() { + show_error!("'{}': {}", &loc, "No such file or directory"); + // We found an error, the return code of ls should not be 0 + // And no need to continue the execution + has_failed = true; + continue; + } let mut dir = false; if p.is_dir() && !options.opt_present("d") { @@ -211,6 +218,11 @@ fn list(options: getopts::Matches) { } enter_directory(&dir, &options); } + if has_failed { + 1 + } else { + 0 + } } #[cfg(any(unix, target_os = "redox"))]