Skip to content

Commit d25ded6

Browse files
committed
ls: -u/-c/--time control sorting when -l is not used
Fallback on those flags if --sort is not otherwise set.
1 parent e48c4a7 commit d25ded6

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

src/uu/ls/src/ls.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,13 @@ fn extract_sort(options: &clap::ArgMatches) -> Sort {
452452
Sort::Version
453453
} else if options.get_flag(options::sort::EXTENSION) {
454454
Sort::Extension
455+
} else if !options.get_flag(options::format::LONG)
456+
&& (options.get_flag(options::time::ACCESS)
457+
|| options.get_flag(options::time::CHANGE)
458+
|| options.get_one::<String>(options::TIME).is_some())
459+
{
460+
// If -l is not specified, -u/-c/--time controls sorting.
461+
Sort::Time
455462
} else {
456463
Sort::Name
457464
}

tests/by-util/test_ls.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,24 +2076,29 @@ fn test_ls_order_time() {
20762076
let result = scene.ucmd().arg("--sort=time").arg("-r").succeeds();
20772077
result.stdout_only("test-1\ntest-2\ntest-3\ntest-4\n");
20782078

2079+
let args: [&[&str]; 10] = [
2080+
&["-t", "-u"],
2081+
&["-u"], //-t is optional: when -l is not set -u/--time controls sorting
2082+
&["-t", "--time=atime"],
2083+
&["--time=atime"],
2084+
&["--time=atim"], // spell-checker:disable-line
2085+
&["--time=a"],
2086+
&["-t", "--time=access"],
2087+
&["--time=access"],
2088+
&["-t", "--time=use"],
2089+
&["--time=use"],
2090+
];
20792091
// 3 was accessed last in the read
20802092
// So the order should be 2 3 4 1
2081-
for arg in [
2082-
"-u",
2083-
"--time=atime",
2084-
"--time=atim", // spell-checker:disable-line
2085-
"--time=a",
2086-
"--time=access",
2087-
"--time=use",
2088-
] {
2089-
let result = scene.ucmd().arg("-t").arg(arg).succeeds();
2093+
for args in args {
2094+
let result = scene.ucmd().args(args).succeeds();
20902095
at.open("test-3").metadata().unwrap().accessed().unwrap();
20912096
at.open("test-4").metadata().unwrap().accessed().unwrap();
20922097

20932098
// It seems to be dependent on the platform whether the access time is actually set
20942099
#[cfg(unix)]
20952100
{
2096-
let expected = unwrap_or_return!(expected_result(&scene, &["-t", arg]));
2101+
let expected = unwrap_or_return!(expected_result(&scene, args));
20972102
at.open("test-3").metadata().unwrap().accessed().unwrap();
20982103
at.open("test-4").metadata().unwrap().accessed().unwrap();
20992104

@@ -2109,6 +2114,10 @@ fn test_ls_order_time() {
21092114
{
21102115
let result = scene.ucmd().arg("-tc").succeeds();
21112116
result.stdout_only("test-2\ntest-4\ntest-3\ntest-1\n");
2117+
2118+
// When -l is not set, -c also controls sorting
2119+
let result = scene.ucmd().arg("-c").succeeds();
2120+
result.stdout_only("test-2\ntest-4\ntest-3\ntest-1\n");
21122121
}
21132122
}
21142123

0 commit comments

Comments
 (0)