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:56:54 +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.

However, what about Eric's example?

  $ src/stat-p -c '_%-0 010.4:X_' k  # yours
  _234       _
  $ src/stat-j -c '_%-0 010.4:X_' k  # mine
  _0234      _

and printf (note how it includes the leading space):

  $ env printf '_%-0 010.4d_\n' 234
  _ 0234     _