]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
stat: use quoted file names with --terse
authorPádraig Brady <P@draigBrady.com>
Wed, 3 Jun 2026 15:28:34 +0000 (16:28 +0100)
committerPádraig Brady <P@draigBrady.com>
Thu, 4 Jun 2026 12:48:05 +0000 (13:48 +0100)
* src/stat.c: Use %Qn rather than a bare %n by default,
so output is maintained on a single line for parsing.

doc/coreutils.texi
src/stat.c
tests/stat/stat-fmt.sh

index 5ab056ce1cf0a8e873f5d53eaff4bd9fae613d1a..33c7f19594a960dae33a939290ae33ecebc505d6 100644 (file)
@@ -12539,13 +12539,13 @@ also identifies the items printed (in fuller form) in the default format.
 The format string would include another @samp{%C} at the end with an
 active SELinux security context.
 @example
-$ stat --format="%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o" ...
+$ stat --format="%Qn %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o" ...
 $ stat --terse ...
 @end example
 
 The same illustrating terse output in @option{--file-system} mode:
 @example
-$ stat -f --format="%n %i %l %t %s %S %b %f %a %c %d" ...
+$ stat -f --format="%Qn %i %l %t %s %S %b %f %a %c %d" ...
 $ stat -f --terse ...
 @end example
 @end table
index 3995acdfefe033b0be5ab2fe55e8e6e965fc36f4..c76f4880a782c1a5eee74be8d5bd70dbfa33418f 100644 (file)
@@ -179,10 +179,10 @@ static char const digits[] = "0123456789";
 static char const printf_flags[] = "'-+ #0I";
 
 /* Formats for the --terse option.  */
-static char const fmt_terse_fs[] = "%n %i %l %t %s %S %b %f %a %c %d\n";
-static char const fmt_terse_regular[] = "%n %s %b %f %u %g %D %i %h %t %T"
+static char const fmt_terse_fs[] = "%Qn %i %l %t %s %S %b %f %a %c %d\n";
+static char const fmt_terse_regular[] = "%Qn %s %b %f %u %g %D %i %h %t %T"
                                         " %X %Y %Z %W %o\n";
-static char const fmt_terse_selinux[] = "%n %s %b %f %u %g %D %i %h %t %T"
+static char const fmt_terse_selinux[] = "%Qn %s %b %f %u %g %D %i %h %t %T"
                                         " %X %Y %Z %W %o %C\n";
 
 #define PROGRAM_NAME "stat"
@@ -1687,7 +1687,7 @@ default_format (bool fs, bool terse, bool device)
         {
           /* TRANSLATORS: This string uses format specifiers from
              'stat --help' with --file-system, and NOT from printf.  */
-          format = xstrdup (_("  File: \"%n\"\n"
+          format = xstrdup (_("  File: %Qn\n"
                               "    ID: %-8i Namelen: %-7l Type: %T\n"
                               "Block size: %-10s Fundamental block size: %S\n"
                               "Blocks: Total: %-10b Free: %-10f Available: %a\n"
index 7e62f015caca49e7ead3cb4a98fa71b9128cabcf..beb87db50ad60bb3e98a5726e36ae61f70c2e65f 100755 (executable)
@@ -68,6 +68,14 @@ cat <<\EOF >exp
   File: 'a'$'\n\n''b'$'\n''c'
 EOF
 compare exp out || fail=1
+# likewise with --terse
+stat --terse "$fname" | cut -d ' ' -f1 > out || fail=1
+cat <<\EOF >exp
+'a'$'\n\n''b'$'\n''c'
+EOF
+compare exp out || fail=1
+stat -f --terse "$fname" | cut -d ' ' -f1 > out || fail=1
+compare exp out || fail=1
 
 # Check the behavior with invalid values of QUOTING_STYLE.
 for style in '' 'abcdef'; do