ls: don't escape backslash in shell style quoting

This commit is contained in:
Terts Diepraam 2021-04-21 11:08:40 +02:00
parent ff620b5fa4
commit 795d89f11d
2 changed files with 41 additions and 2 deletions

View file

@ -1,6 +1,6 @@
use std::char::from_digit;
const SPECIAL_SHELL_CHARS: &str = "~`#$&*()\\|[]{};'\"<>?! ";
const SPECIAL_SHELL_CHARS: &str = "~`#$&*()|[]{};'\"<>?! ";
pub(super) enum QuotingStyle {
Shell {
@ -135,7 +135,6 @@ impl EscapedChar {
'\x0B' => Backslash('v'),
'\x0C' => Backslash('f'),
'\r' => Backslash('r'),
'\\' => Backslash('\\'),
'\x00'..='\x1F' | '\x7F' => Octal(EscapeOctal::from(c)),
'\'' => match quotes {
Quotes::Single => Backslash('\''),
@ -627,4 +626,22 @@ mod tests {
],
);
}
#[test]
fn test_backslash() {
// Escaped in C-style, but not in Shell-style escaping
check_names(
"one\\two",
vec![
("one\\two", "literal"),
("one\\two", "literal-show"),
("one\\\\two", "escape"),
("\"one\\\\two\"", "c"),
("one\\two", "shell"),
("\'one\\two\'", "shell-always"),
("one\\two", "shell-escape"),
("'one\\two'", "shell-escape-always"),
],
);
}
}

View file

@ -1049,6 +1049,7 @@ fn test_ls_quoting_style() {
at.touch("one two");
at.touch("one");
at.touch("one\\two");
// It seems that windows doesn't allow \n in filenames.
#[cfg(unix)]
@ -1168,6 +1169,27 @@ fn test_ls_quoting_style() {
.succeeds()
.stdout_only(format!("{}\n", correct));
}
for (arg, correct) in &[
("--quoting-style=literal", "one\\two"),
("-N", "one\\two"),
("--quoting-style=c", "\"one\\\\two\""),
("-Q", "\"one\\\\two\""),
("--quote-name", "\"one\\\\two\""),
("--quoting-style=escape", "one\\\\two"),
("-b", "one\\\\two"),
("--quoting-style=shell-escape", "one\\two"),
("--quoting-style=shell-escape-always", "'one\\two'"),
("--quoting-style=shell", "one\\two"),
("--quoting-style=shell-always", "'one\\two'"),
] {
scene
.ucmd()
.arg(arg)
.arg("one\\two")
.succeeds()
.stdout_only(format!("{}\n", correct));
}
}
#[test]