]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
ls: with -m, quote names containing commas when appropriate
authorPádraig Brady <P@draigBrady.com>
Thu, 9 Jul 2026 17:15:15 +0000 (18:15 +0100)
committerPádraig Brady <P@draigBrady.com>
Sat, 1 Aug 2026 11:38:28 +0000 (12:38 +0100)
* src/ls.c (decode_switches): Flag ',' for quoting when needed.
* tests/ls/m-option.sh: Add a test case.
* NEWS: Mention the improvement.

NEWS
src/ls.c
tests/ls/m-option.sh

diff --git a/NEWS b/NEWS
index 28d1da64d923e52b8baebca663ad1ae9f1c13e4f..523f0047c012b19a4a893f39ebb011dadea7bd95 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -84,6 +84,9 @@ GNU coreutils NEWS                                    -*- outline -*-
   'install -C' will now avoid updating file metadata when the destination
   already has the appropriate ownership and permissions.
 
+  'ls -m' now quotes files names containing commas when appropriate,
+  so users can better distinguish separating commas.
+
   'sort' will now better use available memory and parallel operation
   when reading from unknown sized inputs like pipes.
 
index 0b4695a333f0d720ebc8ba4a3f7bb0bd3e5f9fb3..b2335b217995430347bb3901937dc4239e30b53a 100644 (file)
--- a/src/ls.c
+++ b/src/ls.c
@@ -2359,6 +2359,12 @@ decode_switches (int argc, char **argv)
       for (char const *p = &"*=>@|"[indicator_style - file_type]; *p; p++)
         set_char_quoting (filename_quoting_options, *p, 1);
     }
+  if (format == with_commas
+      && (qs == shell_quoting_style
+          || qs == shell_escape_quoting_style
+          || qs == c_maybe_quoting_style
+          || qs == escape_quoting_style))
+    set_char_quoting (filename_quoting_options, ',', 1);
 
   dirname_quoting_options = clone_quoting_options (NULL);
   set_char_quoting (dirname_quoting_options, ':', 1);
index c9502e915643550610c7cbf6cef4d2036a183c23..afe3fd415639e963151cea5bf18dd0604a31359a 100755 (executable)
@@ -50,4 +50,26 @@ printf '%s\n' 'a, bb' > exp || framework_failure_
 ls -w5 -m a bb > out || fail=1
 compare exp out || fail=1
 
+# Ensure commas in names are quoted appropriately for all quoting styles.
+# Without this quoting, interactive output could become confusing,
+# especially in the presence of NBSP etc.
+touch 'com,ma' || framework_failure_
+cat <<\EOF > exp || framework_failure_
+literal: com,ma
+shell: 'com,ma'
+shell-always: 'com,ma'
+shell-escape: 'com,ma'
+shell-escape-always: 'com,ma'
+c: "com,ma"
+c-maybe: "com,ma"
+escape: com\,ma
+locale: 'com,ma'
+clocale: "com,ma"
+EOF
+for qs in $(cut -d: -f1 exp); do
+  printf '%s: ' "$qs"
+  ls -m --quoting-style="$qs" 'com,ma' || fail=1
+done > out
+compare exp out || fail=1
+
 Exit $fail