]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
stat: handle %%%N too
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 28 Feb 2026 00:49:08 +0000 (16:49 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 28 Feb 2026 00:50:36 +0000 (16:50 -0800)
* src/stat.c (main): Fix incorrect counting of '%'s before 'N'.
* tests/stat/stat-fmt.sh: Test for the bug.

src/stat.c
tests/stat/stat-fmt.sh

index 1e350152572bbb1f462a4309b81466313b73c5e9..e682bc6b9022a874ce30de21674ced017277b460 100644 (file)
@@ -1971,9 +1971,10 @@ main (int argc, char *argv[])
   if (format)
     {
       bool need_quoting_style = false;
-      for (char const *p = format; (p = strchr (p, '%')); ++p)
+      for (char const *p = format; (p = strchr (p, '%'));
+           p += (p[1] == '%') + 1)
         {
-          if (p[1] == 'N' && (p == format || p[-1] != '%'))
+          if (p[1] == 'N')
             {
               need_quoting_style = true;
               break;
index 066c481337d4bbfc2e3e1ebb2ab5b86dade5b8ff..dec5f98dbf04de8e3020da3ff82163ad68b77d14 100755 (executable)
@@ -42,9 +42,9 @@ compare exp out || fail=1
 
 # Check the behavior with invalid values of QUOTING_STYLE.
 for style in '' 'abcdef'; do
-  QUOTING_STYLE="$style" stat -c%N \' > out 2> err || fail=1
+  QUOTING_STYLE="$style" stat -c%%%N \' > out 2> err || fail=1
   cat <<\EOF > exp-out || framework_failure_
-"'"
+%"'"
 EOF
   cat <<EOF > exp-err || framework_failure_
 stat: ignoring invalid value of environment variable QUOTING_STYLE: '$style'
@@ -53,7 +53,7 @@ EOF
   compare exp-err err || fail=1
   # coreutils-9.10 and earlier would unnecessarily check QUOTING_STYLE in
   # these cases.
-  for format in '%%N' 'abc%%Ndef' 'abc%%Ndef%%N'; do
+  for format in '%%N' 'abc%%Ndef' 'abc%%Ndef%%N' '%%%%N'; do
     QUOTING_STYLE="$style" stat -c"$format" \' > out 2> err || fail=1
     printf "$format\n" > exp-out || framework_failure_
     compare exp-out out || fail=1