All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: "Thomas Weißschuh" <linux@weissschuh.net>
Cc: "licheng.li" <im.lechain@gmail.com>, Willy Tarreau <w@1wt.eu>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/2] tools/nolibc: add support zero pad (0) in printf
Date: Sat, 31 Jan 2026 12:32:09 +0000	[thread overview]
Message-ID: <20260131123209.1ed5df80@pumpkin> (raw)
In-Reply-To: <222455c7-17a8-4b1f-a574-4e945467eb12@weissschuh.net>

On Sat, 31 Jan 2026 11:18:49 +0100
Thomas Weißschuh <linux@weissschuh.net> wrote:

> Hey Cheng,
> 
> Jan 30, 2026 09:37:51 licheng.li <im.lechain@gmail.com>:
> 
> > From: Cheng Li <im.lechain@gmail.com>
> >
> > This patch correctly implements the '0' flag in __nolibc_printf() to
> > allow zero-padding for numeric and pointer outputs.  
> 
> Thanks for (all of) your patches.
> I am not sure when exactly I can take a proper look at them.
> As we are currently fairly late in the 6.20/7.0 development cycle I would like move your patches into the next one.
> We can still discuss the patches and you can send new revisions and patches,
> but they won't be picked up until in a few weeks.

Gives me time to re-write them :-)

There is still a bug in the 'align left' code as well.
snprintf(buf, 21, "%-25s", "abcd") outputs 20 spaces not "abcd" followed by 16.
Easiest fix is to move the truncation in the cb() function.

	David

> 
> > Thanks to David for pointing out the errors in the previous implementation.
> >
> > The logic ensures that the sign ('-') for negative numbers or the prefix
> > ('0x') for pointers is printed before the padding zeros, adhering to
> > standard printf behavior (e.g., producing "-0005" instead of "000-5").
> >
> > Examples of the corrected padding logic:
> > - ("%05d", -5)  -> "-0005"
> > - ("%05p", ptr)  -> "0x00..."
> >
> > Signed-off-by: Cheng Li <im.lechain@gmail.com>
> > ---
> > tools/include/nolibc/stdio.h | 16 ++++++++++++++--
> > 1 file changed, 14 insertions(+), 2 deletions(-)
> >
> > diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
> > index f31b77f61d3b..8a4af259a31b 100644
> > --- a/tools/include/nolibc/stdio.h
> > +++ b/tools/include/nolibc/stdio.h
> > @@ -267,7 +267,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char
> >             /* we're in an escape sequence, ofs == 1 */
> >             escape = 0;
> >
> > -           if (c == '-') {
> > +           if (c == '-' || c == '0') {
> >                 padc = c;
> >                 c = fmt[ofs++];
> >             }
> > @@ -364,9 +364,21 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char
> >             if (n) {
> >                 w = len < n ? len : n;
> >                 n -= w;
> > +               if (padc == '0') {
> > +                   if (outstr[0] == '-') {
> > +                       if (cb(state, outstr, 1) != 0)
> > +                           return -1;
> > +                       outstr++;
> > +                   }
> > +                   if (outstr[0] == '0' && outstr[1] == 'x') {
> > +                       if (cb(state, outstr, 2) != 0)
> > +                           return -1;
> > +                       outstr += 2;
> > +                   }
> > +               }
> >                 while (width > w && padc != '-') {
> >                     written += 1;
> > -                   if (cb(state, " ", 1) != 0)
> > +                   if (cb(state, &padc, 1) != 0)
> >                         return -1;
> >                     width--;
> >                 }
> > --
> > 2.52.0  
> 


  reply	other threads:[~2026-01-31 12:32 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-30  8:37 [PATCH 1/2] tools/nolibc: add support zero pad (0) in printf licheng.li
2026-01-30  8:37 ` [PATCH 2/2] selftests/nolibc: add tests for printf zero padding (0) licheng.li
2026-01-30 10:02 ` [PATCH 1/2] tools/nolibc: add support zero pad (0) in printf David Laight
2026-01-30 10:12   ` Cheng Li
2026-01-30 14:58     ` David Laight
2026-01-31  3:44       ` Cheng Li
2026-01-31 10:18 ` Thomas Weißschuh
2026-01-31 12:32   ` David Laight [this message]
2026-02-01  0:49     ` Cheng Li
2026-02-01  8:56       ` David Laight

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260131123209.1ed5df80@pumpkin \
    --to=david.laight.linux@gmail.com \
    --cc=im.lechain@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@weissschuh.net \
    --cc=w@1wt.eu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.