Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1233,9 +1233,12 @@ fn parse_time_style(s: Option<&String>) -> UResult<String> {
// the string starts with +, and ignore "locale".
Ok(s) => {
let s = s.strip_prefix("posix-").unwrap_or(s.as_str());
let s = match s.chars().next().unwrap() {
'+' => s.split('\n').next().unwrap(),
_ => s,
// `s` can be empty here (e.g. TIME_STYLE=posix-), so test the
// prefix instead of unwrapping the first char.
let s = if s.starts_with('+') {
s.split('\n').next().unwrap()
} else {
s
};
match s {
"locale" => None,
Expand All @@ -1251,9 +1254,9 @@ fn parse_time_style(s: Option<&String>) -> UResult<String> {
"full-iso" => Ok(format::FULL_ISO.to_string()),
"long-iso" => Ok(format::LONG_ISO.to_string()),
"iso" => Ok(format::ISO.to_string()),
_ => match s.chars().next().unwrap() {
'+' => Ok(s[1..].to_string()),
_ => Err(DuError::InvalidTimeStyleArg(s).into()),
_ => match s.strip_prefix('+') {
Some(rest) => Ok(rest.to_string()),
None => Err(DuError::InvalidTimeStyleArg(s).into()),
},
},
None => Ok(format::LONG_ISO.to_string()),
Expand Down
24 changes: 12 additions & 12 deletions src/uu/ls/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1054,19 +1054,19 @@ fn parse_time_style(options: &clap::ArgMatches) -> Result<(String, Option<String
Some(format::ISO.to_string() + " "),
)),
"locale" => ok(LOCALE_FORMAT),
_ => match field.chars().next().unwrap() {
'+' => {
// recent/older formats are (optionally) separated by a newline
let mut it = field[1..].split('\n');
let recent = it.next().unwrap_or_default();
let older = it.next();
match it.next() {
None => ok((recent, older)),
Some(_) => Err(LsError::TimeStyleParseError(String::from(field))),
}
// `field` can be empty here (e.g. --time-style=posix-), so test
// the prefix instead of unwrapping the first char.
_ if field.starts_with('+') => {
// recent/older formats are (optionally) separated by a newline
let mut it = field[1..].split('\n');
let recent = it.next().unwrap_or_default();
let older = it.next();
match it.next() {
None => ok((recent, older)),
Some(_) => Err(LsError::TimeStyleParseError(String::from(field))),
}
_ => Err(LsError::TimeStyleParseError(String::from(field))),
},
}
_ => Err(LsError::TimeStyleParseError(String::from(field))),
}
}
} else if options.get_flag(options::FULL_TIME) {
Expand Down
15 changes: 15 additions & 0 deletions tests/by-util/test_du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,21 @@ fn test_du_with_posixly_correct() {
assert_eq!(expected, result);
}

#[test]
fn test_du_time_style_empty() {
let ts = TestScenario::new(util_name!());
ts.fixtures.mkdir("a");
ts.ucmd()
.args(&["--time", "--time-style=", "a"])
.fails_with_code(1)
.stderr_contains("du: invalid argument '' for 'time style'");
ts.ucmd()
.args(&["--time", "a"])
.env("TIME_STYLE", "posix-")
.fails_with_code(1)
.stderr_contains("du: invalid argument '' for 'time style'");
}

#[test]
fn test_du_zero_env_block_size() {
let ts = TestScenario::new(util_name!());
Expand Down
9 changes: 9 additions & 0 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ fn test_invalid_value_time_style() {
.no_stderr();
}

#[test]
fn test_time_style_empty_after_posix_prefix() {
new_ucmd!()
.arg("-l")
.arg("--time-style=posix-")
.fails_with_code(2)
.stderr_contains("ls: invalid --time-style argument ''");
}

#[test]
fn test_ls_ls() {
new_ucmd!().succeeds();
Expand Down
Loading