Skip to content

Commit d79c18a

Browse files
authored
Merge pull request #8791 from dekuu5/stat/fix-symlink-behavior
stat: fix %N symlink to be single quote instead of double quote
1 parent f253efe commit d79c18a

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

src/uu/stat/src/stat.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,10 @@ fn quote_file_name(file_name: &str, quoting_style: &QuotingStyle) -> String {
439439
let escaped = file_name.replace('\'', r"\'");
440440
format!("'{escaped}'")
441441
}
442-
QuotingStyle::ShellEscapeAlways => format!("\"{file_name}\""),
442+
QuotingStyle::ShellEscapeAlways => {
443+
let quote = if file_name.contains('\'') { '"' } else { '\'' };
444+
format!("{quote}{file_name}{quote}")
445+
}
443446
QuotingStyle::Quote => file_name.to_string(),
444447
}
445448
}
@@ -1378,7 +1381,7 @@ fn pretty_time(meta: &Metadata, md_time_field: MetadataTimeField) -> String {
13781381

13791382
#[cfg(test)]
13801383
mod tests {
1381-
use crate::{pad_and_print_bytes, print_padding};
1384+
use crate::{pad_and_print_bytes, print_padding, quote_file_name};
13821385

13831386
use super::{Flags, Precision, ScanUtil, Stater, Token, group_num, precision_trunc};
13841387

@@ -1529,4 +1532,19 @@ mod tests {
15291532
print_padding(&mut buffer, 5).unwrap();
15301533
assert_eq!(&buffer, b" ");
15311534
}
1535+
1536+
#[test]
1537+
fn test_quote_file_name() {
1538+
let file_name = "nice' file";
1539+
assert_eq!(
1540+
quote_file_name(file_name, &crate::QuotingStyle::ShellEscapeAlways),
1541+
"\"nice' file\""
1542+
);
1543+
1544+
let file_name = "nice\" file";
1545+
assert_eq!(
1546+
quote_file_name(file_name, &crate::QuotingStyle::ShellEscapeAlways),
1547+
"\'nice\" file\'"
1548+
);
1549+
}
15321550
}

tests/by-util/test_stat.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,13 @@ fn test_quoting_style_locale() {
435435
.args(&["-c", "%N", "'"])
436436
.succeeds()
437437
.stdout_only("\"'\"\n");
438+
439+
// testing file having "
440+
at.touch("\"");
441+
ts.ucmd()
442+
.args(&["-c", "%N", "\""])
443+
.succeeds()
444+
.stdout_only("\'\"\'\n");
438445
}
439446

440447
#[test]

0 commit comments

Comments
 (0)