From nobody Mon Feb 9 00:25:45 2026 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B236134A3D6 for ; Wed, 28 Jan 2026 09:42:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769593355; cv=none; b=FhwEbpkg0GJX99UxBfrjH3YWIrfvldyKCe/ru/cBrHHKi8ghcYd01DdJPn05aPR1luIdN6tSQMSD7H2kdWWkou7blcn1E7Er3zPCeE7bRFAXh+A4jk4SJ4iAoyE+ahLL8OyqgYj5RvKQP0Ule/DeRhRBvCROADKpiu5Wib48olE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769593355; c=relaxed/simple; bh=NN9jUyq1wpyz1N2ymvXQiOghLe3QVnvwpTxh+KlXLTo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=in7tPcGCbXcHkKA8wYjTVa/25VucH6CTYJ71Fd4QMTnSwRkNDxipQCbvcFupDb8zJtlG3TTXqbC9N+DtANLgzC2qpsncoaOrtDBuC253fg7LuxswPfCH+k/abJuoLrTt3mdWoLpnXMMhvpHMTPw1fNXFLAyaodM/qo8PuvCuAkg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=FWu7Khy4; arc=none smtp.client-ip=209.85.210.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="FWu7Khy4" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-82361bcbd8fso1326977b3a.0 for ; Wed, 28 Jan 2026 01:42:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769593353; x=1770198153; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=A/vSljcfVndndO/d1EcoT2O4s0Gevvxwu2hMn0oN0ZM=; b=FWu7Khy4Zcp2Mbhvkd80uZ2UP/MrDA02ZJDkMFTGHW9k8FUu/huuRG9vPoQuSX4yJ1 1cjqAee3souulNyIiAp1vBaVUENkyZV4s9RUWdMb4shPBOkh6vpXImNVqy3TjpDLv9Ri htGJPnzQzo9rPRZmFdXukSDqRMBjwGBUMiFv4og9YtCSdN47e/6I5ax3k4AHeCyUYSml M7zNbI1hZx28Yl1U8AoS6VJXuU65tt01TllAWUVgTzvc54eQVLDKMYnJ+qkoferZUOhj eqv7uTyRxrBOn5CYdovR3fSsh4RN++TCv1n9ArCfVZFtzh5cj0mD5cnUo3CNYpCAbrw3 E8PQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769593353; x=1770198153; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=A/vSljcfVndndO/d1EcoT2O4s0Gevvxwu2hMn0oN0ZM=; b=Ylvoso4AdnHC3NhT3fxn0n7JHDojV5+1N1l0QdCdYIL+jRUleol+N3KZRejSRw8z3v ON73jbQXkr65CCsLGXT6/qiRlDDfbAzNnaV2KaaC9gs80xRhY1vDpvq9vWAShcoiLgiL fWL7TpGuLlhav1uAmljDRgepuZboL0gKJh2K90OgSiCBHNSN2y3LhjNxjYFBkLVxzrP/ 4TLUZkKp9qBoDCp1g+W+B3iTeJuHRMEYDzi95bvIklu67jglkRax3WpxnBajtcn70ACT /fRbFSmp3AnR8cDUnpqYIkYrtMWLbmD218XR1LFf6+DJcbjl8lWT0pcSwo3OW9SxG8vP fWjg== X-Forwarded-Encrypted: i=1; AJvYcCVYK9gzF2oU3sBnJjAfQIfLj3oSLGE8n2+6ZZe6SqstyKqES8rWRQfpWPt2a7VLkmdMxiW7D9cL6oMUcmo=@vger.kernel.org X-Gm-Message-State: AOJu0YwoKv1G+cXPWlNVNZF0CMvb0x85wBddtgS1R884Ym3mMsn7vqER devl2hlxgpiiV1+wRtra/KX6JMaibZ3cioY/0+lSdghkAobE3AA+U5O5 X-Gm-Gg: AZuq6aIJ4si7f2n9fDMarsxXQFW/Objxm5wH3j/bE7E6n+cKcfsfaYhe8rgf9GJ8t5/ AkggSCZSDiYCGm2gM6JCgC7VP4MF1Zq8nnHD+5lZqYHsw65tEPfisWniQT/Hl7RJerCRFoNLiZ1 YB0Nql9/zKZ5ImLXEASRM2LA00YzF9+z/hlaWpgvSy+h9qLB+aHNlAO1mvdd00VSZm/gB1iKuwT mwc/xOZuZtxxcsImGxWbkqaux3guTkLizcqe832d1YDRSQ+Ebvb1d3ztPe1Kmx1aOfgZBZ4pF+i ACM3hQC/tPZVQRCe16yFKQec6XbhKKJg7ssCDTxBW4hISZI1oJ1usAYRSwj/KGpNX7wDyfi+IPH lMjis1gYPoNLxyu0+kUkyRzT0l98rhiarHKLWSmXGX6qtAk4l6qAux5NF6VBdhR9hSRpgF77GSU /OWYCKdPmHYT8SSmrg3HE/NpW56f9HqfmpqK1l/6rOlPfOUGDS8dGWYjcfgOtccl+0zJZTChK0n iN0Db6K5DxCsRTiaGRqB/4= X-Received: by 2002:a05:6a00:39a6:b0:81f:3c7c:8591 with SMTP id d2e1a72fcca58-823692c0354mr3711479b3a.50.1769593352928; Wed, 28 Jan 2026 01:42:32 -0800 (PST) Received: from LAPTOP-UKI113GK.localdomain ([113.116.245.223]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-82379b222f1sm2093671b3a.7.2026.01.28.01.42.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jan 2026 01:42:32 -0800 (PST) From: "licheng.li" To: Willy Tarreau Cc: =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , linux-kernel@vger.kernel.org, im.lechain@gmail.com Subject: [PATCH v2 1/2] tools/nolibc: support left alignment (-) and zero padding (0) in printf Date: Wed, 28 Jan 2026 17:42:23 +0800 Message-ID: <20260128094224.11299-2-im.lechain@gmail.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260128094224.11299-1-im.lechain@gmail.com> References: <20260128094224.11299-1-im.lechain@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Cheng Li Currently, __nolibc_printf() in nolibc parses the width field but always pads with spaces on the left. It ignores the '-' flag (left alignment) and treats leading zeros in the width as part of the number parsing (resulting in space padding instead of zero padding). This patch implements support for: 1. The '-' flag: forces left alignment by padding spaces on the right. 2. The '0' flag: forces zero padding on the left instead of spaces. The implementation reuses the padding character logic to handle both cases. Logic behavior: - "%5d" -> " 12" (unchanged) - "%-5d" -> "12 " (new: left align) - "%05d" -> "00012" (new: zero pad) - "%-05d"-> "12 " (new: left align overrides zero pad) The code is optimized to keep the binary size impact minimal. Measuring the nolibc-test binary on x86_64: text data bss dec hex filename 43552 248 112 43912 ab88 nolibc-test (before) 43677 248 112 44037 ac05 nolibc-test (after) The net increase is 125 bytes. Suggested-by: Willy Tarreau Signed-off-by: Cheng Li --- v2 changes: - Adopted optimization suggestions from Willy Tarreau: - Incremented 'written' counter at the start of loops. - Reordered loop checks to optimize compiler register usage. - Updated commit message to explicitly mention zero-padding ('0') support. --- tools/include/nolibc/stdio.h | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index 1f16dab2ac88..df8a96e39986 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -250,7 +250,7 @@ typedef int (*__nolibc_printf_cb)(intptr_t state, const= char *buf, size_t size); static __attribute__((unused, format(printf, 4, 0))) int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const= char *fmt, va_list args) { - char escape, lpref, c; + char escape, lpref, padc, c; unsigned long long v; unsigned int written, width; size_t len, ofs, w; @@ -261,11 +261,17 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t s= tate, size_t n, const char while (1) { c =3D fmt[ofs++]; width =3D 0; + padc =3D ' '; =20 if (escape) { /* we're in an escape sequence, ofs =3D=3D 1 */ escape =3D 0; =20 + if (c =3D=3D '-' || c =3D=3D '0') { + padc =3D c; + c =3D fmt[ofs++]; + } + /* width */ while (c >=3D '0' && c <=3D '9') { width *=3D 10; @@ -358,13 +364,19 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t s= tate, size_t n, const char if (n) { w =3D len < n ? len : n; n -=3D w; - while (width-- > w) { - if (cb(state, " ", 1) !=3D 0) - return -1; + while (width > w && padc !=3D '-') { written +=3D 1; + if (cb(state, &padc, 1) !=3D 0) + return -1; + width--; } if (cb(state, outstr, w) !=3D 0) return -1; + while (width-- > w) { + written +=3D 1; + if (cb(state, " ", 1) !=3D 0) + return -1; + } } =20 written +=3D len; --=20 2.52.0