diff --git a/src/uu/dircolors/src/dircolors.rs b/src/uu/dircolors/src/dircolors.rs index faef0683e..9b57050f9 100644 --- a/src/uu/dircolors/src/dircolors.rs +++ b/src/uu/dircolors/src/dircolors.rs @@ -374,6 +374,7 @@ where let term = env::var("TERM").unwrap_or_else(|_| "none".to_owned()); let term = term.as_str(); + let colorterm = env::var("COLORTERM").unwrap_or_default(); let mut state = ParseState::Global; @@ -396,8 +397,20 @@ where )); } let lower = key.to_lowercase(); + if lower == "term" || lower == "colorterm" { - if term.fnmatch(val) { + let should_match = if lower == "colorterm" { + // For COLORTERM ?*, only match if COLORTERM is non-empty + if val == "?*" { + !colorterm.is_empty() + } else { + colorterm.fnmatch(val) + } + } else { + term.fnmatch(val) + }; + + if should_match { state = ParseState::Matched; } else if state != ParseState::Matched { state = ParseState::Pass; diff --git a/tests/by-util/test_dircolors.rs b/tests/by-util/test_dircolors.rs index 06d490c4a..909d904c4 100644 --- a/tests/by-util/test_dircolors.rs +++ b/tests/by-util/test_dircolors.rs @@ -253,3 +253,14 @@ fn test_repeated() { new_ucmd!().arg(arg).arg(arg).succeeds().no_stderr(); } } + +#[test] +fn test_colorterm_empty_with_wildcard() { + new_ucmd!() + .env("COLORTERM", "") + .pipe_in("COLORTERM ?*\nowt 40;33\n") + .args(&["-b", "-"]) + .succeeds() + .stdout_is("LS_COLORS='';\nexport LS_COLORS\n") + .no_stderr(); +}