From: Pádraig Brady
Date: Wed, 3 Jun 2026 15:28:34 +0000 (+0100) Subject: stat: use quoted file names with --terse X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5d3f03b4fb6de49fe129a1ce4b03c382a9ee9450;p=thirdparty%2Fcoreutils.git stat: use quoted file names with --terse * src/stat.c: Use %Qn rather than a bare %n by default, so output is maintained on a single line for parsing. --- diff --git a/doc/coreutils.texi b/doc/coreutils.texi index 5ab056ce1c..33c7f19594 100644 --- a/doc/coreutils.texi +++ b/doc/coreutils.texi @@ -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 diff --git a/src/stat.c b/src/stat.c index 3995acdfef..c76f4880a7 100644 --- a/src/stat.c +++ b/src/stat.c @@ -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" diff --git a/tests/stat/stat-fmt.sh b/tests/stat/stat-fmt.sh index 7e62f015ca..beb87db50a 100755 --- a/tests/stat/stat-fmt.sh +++ b/tests/stat/stat-fmt.sh @@ -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