1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

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
This commit is contained in:
Sylvestre Ledru 2020-12-13 12:06:44 +01:00
parent cd97adb39d
commit 8a1628cf89

View file

@ -168,11 +168,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
) )
.parse(args); .parse(args);
list(matches); list(matches)
0
} }
fn list(options: getopts::Matches) { fn list(options: getopts::Matches) -> i32 {
let locs: Vec<String> = if options.free.is_empty() { let locs: Vec<String> = if options.free.is_empty() {
vec![String::from(".")] vec![String::from(".")]
} else { } else {
@ -181,8 +180,16 @@ fn list(options: getopts::Matches) {
let mut files = Vec::<PathBuf>::new(); let mut files = Vec::<PathBuf>::new();
let mut dirs = Vec::<PathBuf>::new(); let mut dirs = Vec::<PathBuf>::new();
let mut has_failed = false;
for loc in locs { for loc in locs {
let p = PathBuf::from(&loc); 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; let mut dir = false;
if p.is_dir() && !options.opt_present("d") { if p.is_dir() && !options.opt_present("d") {
@ -211,6 +218,11 @@ fn list(options: getopts::Matches) {
} }
enter_directory(&dir, &options); enter_directory(&dir, &options);
} }
if has_failed {
1
} else {
0
}
} }
#[cfg(any(unix, target_os = "redox"))] #[cfg(any(unix, target_os = "redox"))]