Subject: Re: bug#7325: new test failure due to non-portability of printf formats like %05.3s

From: Jim Meyering <[email-masked]>
Date: Thu, 04 Nov 2010 08:37:00 +0100

Paul Eggert wrote:
> The test-case part of that looks OK, but the change to stat.c
> can be simplified.  Also, there's a similar problem with a format
> like %020X, which should be fixed too.
>
> While we're on the subject, there are other ways that stat invokers
> can exercise undefined behavior (in the C sense) by passing
> weird formats in.  This really should get fixed at some point.
> The patch proposed below addresses just the immediate issue.
>
>>>From a24a9ce8f3711670a6413c60b5e9ebd3e51a4e06 Mon Sep 17 00:00:00 2001
> From: Paul Eggert <[email-masked]>
> Date: Wed, 3 Nov 2010 15:49:50 -0700
> Subject: [PATCH] stat: handle leading '0' when formatting secs and ns
>
> * src/stat.c (epoch_sec): Remove. All callers changed to use
> out_epoch_sec instead.
> (out_ns): Use numeric format, not string format, to output
> the nanoseconds count; this avoids unportable use of (e.g.)
> %05s in a printf format.
> (out_epoch_sec): New function.  This also uses a numeric format,
> instead of a string format, to output a number.

Thanks!  I like it.
FYI, I need slightly different tests for that,
so am amending with these:

diff --git a/tests/misc/stat-nanoseconds b/tests/misc/stat-nanoseconds
index 314f43e..44862eb 100755
--- a/tests/misc/stat-nanoseconds
+++ b/tests/misc/stat-nanoseconds
@@ -26,11 +26,11 @@ ls --full-time | grep 18:43:33.023456789 \

 test "$(stat -c      %:X k)" = 023456789  || fail=1
 test "$(stat -c     %3:X k)" = 23456789   || fail=1
-test "$(stat -c   %3.3:X k)" = 023        || fail=1
-test "$(stat -c    %.3:X k)" = 023        || fail=1
+test "$(stat -c   %3.3:X k)" = ' 23'      || fail=1
+test "$(stat -c    %.3:X k)" = 23         || fail=1
 test "$(stat -c  %03.3:X k)" = 023        || fail=1
-test "$(stat -c  %-5.3:X k)" = '023  '    || fail=1
-test "$(stat -c  %05.3:X k)" = '  023'    || fail=1
-test "$(stat -c %010.3:X k)" = '       023' || fail=1
+test "$(stat -c  %-5.3:X k)" = '23   '    || fail=1
+test "$(stat -c  %05.3:X k)" = 00023      || fail=1
+test "$(stat -c %010.3:X k)" = 0000000023 || fail=1

 Exit $fail