From: =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= Subject: [kvm-unit-tests PATCH 5/6] libcflat: support # flag in printf Date: Wed, 17 May 2017 22:14:04 +0200 Message-ID: <20170517201405.19867-6-rkrcmar@redhat.com> References: <20170517201405.19867-1-rkrcmar@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Paolo Bonzini To: kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:30290 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754413AbdEQUPW (ORCPT ); Wed, 17 May 2017 16:15:22 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B30BE3E9 for ; Wed, 17 May 2017 20:15:21 +0000 (UTC) In-Reply-To: <20170517201405.19867-1-rkrcmar@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: The only use is with %#x, where we'll automatically get 0x prefix. Advantage over 0x%x can be seen with padding. A simple test: printf(".%#08x.\n", 0); printf(".%#8x.\n", 0); printf(".%#-8x.\n", 0); printf(".%#08x.\n", 1); printf(".%#8x.\n", 1); printf(".%#-8x.\n", 1); printf(".%#08x.\n", 0x123456); printf(".%#8x.\n", 0x123456); printf(".%#-8x.\n", 0x123456); printf(".%#02x.\n", 0); printf(".%#2x.\n", 0); printf(".%#-2x.\n", 0); printf(".%#02x.\n", 1); printf(".%#2x.\n", 1); printf(".%#-2x.\n", 1); looks just like glibc: .00000000. . 0. .0 . .0x000001. . 0x1. .0x1 . .0x123456. .0x123456. .0x123456. .00. . 0. .0 . .0x1. .0x1. .0x1. Signed-off-by: Radim Krčmář --- lib/printf.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/printf.c b/lib/printf.c index 944768f3080d..bfe05f573f08 100644 --- a/lib/printf.c +++ b/lib/printf.c @@ -106,8 +106,16 @@ void print_unsigned(pstream_t *ps, unsigned long long n, int base, if (p == buf) *p++ = '0'; else if (props.alternate && base == 16) { - *p++ = 'x'; - *p++ = '0'; + if (props.pad == '0') { + addchar(ps, '0'); + addchar(ps, 'x'); + + if (props.npad > 0) + props.npad = MAX(props.npad - 2, 0); + } else { + *p++ = 'x'; + *p++ = '0'; + } } for (i = 0; i < (p - buf) / 2; ++i) { @@ -169,6 +177,9 @@ int vsnprintf(char *buf, int size, const char *fmt, va_list va) case '\0': --fmt; break; + case '#': + props.alternate = true; + goto morefmt; case '0': props.pad = '0'; ++fmt; -- 2.13.0