Skip to content

Commit

Permalink
ls: Fix quoting for control characters in shell mode
Browse files Browse the repository at this point in the history
  • Loading branch information
RenjiSann committed Jul 22, 2024
1 parent 1282938 commit 83c1447
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/uucore/src/lib/features/quoting_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,25 @@ fn escape_name_inner(name: &OsStr, style: &QuotingStyle, dirname: bool) -> Strin
} => {
let name = name.to_string_lossy();

// Take ':' in account only if we are quoting a dirname
let escaped_chars = [
':',
// Before this line, characters only induce quoting in the
// context of displaying a directory name.
// (e.g. with the recursive flag)
'"', '`', '$', '\\',
// After this line, characters induce quoting only if control
// characters are shown.
'\n',
];

let strt_index = if dirname { 0 } else { 1 };
let (quotes, must_quote) = if name.contains(&[':', '"', '`', '$', '\\'][strt_index..]) {
let end_index = if *show_control {
escaped_chars.len()
} else {
escaped_chars.len() - 1
};

let (quotes, must_quote) = if name.contains(&escaped_chars[strt_index..end_index]) {
(Quotes::Single, true)
} else if name.contains('\'') {
(Quotes::Double, true)
Expand Down

0 comments on commit 83c1447

Please sign in to comment.