From nobody Sun Feb 8 12:36:48 2026 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (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 1797630AACB for ; Fri, 30 Jan 2026 07:18:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769757540; cv=none; b=DLarykhCjyfCHSusPN2+0klnEA9EGonEGwrxem1tP76JXpfrGj13GR9uf/zJ1sdEWYCEM70P47Hv7nVBrxCGV0Qnja909shh0zlBjvr1XodKUeZ/obxe3Wt5D2bqizFcAzBu6ZOajTGAfsa4UvCrMwYo1Tmy2dAvycx21L3fiHg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769757540; c=relaxed/simple; bh=FuW6yaWr72zWMOHQfVmB/+gdprXQ/qxTXWOxkh83iLc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IjgpfSZKcNczwOMlQWkrXuYExovNAmXHrkiEeP8J4oRB462Sz237lafLSMdWMkoxQgNVkXM6P9ZmPUQthV46vH8ZsyOe6gno6uP9nC2rGmESw9jfh757vPzxtMEfczCn4mlAZYWLzlk2DiIwQRccxxURP710lMRnIXCfg/YRGTo= 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=YdoJh+eF; arc=none smtp.client-ip=209.85.210.182 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="YdoJh+eF" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-81f39438187so988053b3a.2 for ; Thu, 29 Jan 2026 23:18:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769757538; x=1770362338; 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=frP1Uvt+xcmW08I1/Cz3HnPA0cTu4CUCvYKx28apYaU=; b=YdoJh+eFKnh/9xeV+1Ecddn2DyzeX88rGFRBvp7RPPPZsccTF+RaqHcOWsm4BZCY+5 HMy0Ov4HQqv2ESqA1rXFDqeRIDfywhNVSa1nSoe0/IkrX/mOX5dnt+i5aKZZSEQkV7vX 11kRglaobY/plJHhfPtuFHantnbOKyiGXV33R8XQ/dF3WZR67Rt0KiYTvcMoQ29FEWxb LR08Qll0yJt48xKZzyZOg0T7uPj9s0qedFu6H3SZvjLWYhV9sxt8pfFVjd0j3BoPtv6V E2dLrFIDMy9sYSlcS43Cq2ndUPsZTHIomG9V2iTXcOlMjoirofNfaGJ77fN9POCWB4Qm hv7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769757538; x=1770362338; 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=frP1Uvt+xcmW08I1/Cz3HnPA0cTu4CUCvYKx28apYaU=; b=OqAtHGSdbyRB3FAE2Bv/V62c/Kxz9sY7G0RsLFLcoFF8NaUeu+07SoOgFr+tNVE0ss 3QKyBOIVqYsfCeRl9Q266OHs0Mq9bNNlmZTkYo9t+v4Us/4Wk7W9+T15Cydkg9W0NIOs Jjt0zhQssqrG1LPX3e4AlxbdMlvEz0lddjzLViuhTeCfpJOe19uCgrT0YaOgy11WGMmP on9H+9SO/OCpWM71K3wsr9qnvmn/hB2L6KJXWTRiL7kiK/DKdRJsbZq0Iv/WbH73Ps+o D2xP3BhEodLhjJ587NPVmBYozN6/udLWKbmmd4nU3A6+ce47FIo5sqgA7yGPBPMmMrCL B2Qg== X-Forwarded-Encrypted: i=1; AJvYcCWWdNEQZQEtSmQ9X10OWeCWzbm5Je8IMVSYJ+cPlMUGunv0RLJXirLo0Kjrc5UuqcbZe4TwwO1NdAQVl8s=@vger.kernel.org X-Gm-Message-State: AOJu0YytZzHDnyLUTgwI9TJqObg/3EA6qU+stLmz5iI74FsEY/YZ5gEN dUFt+/sZlS39e1zWz4mtOqIbl5srMRvzVoHaTYXcI0glfzvzljv31FCA X-Gm-Gg: AZuq6aL6shgGr2jERO3n4dyGQW1TJD0zMb1juLYhrAoaYyNUpcKLZdOGZ6wal78evL6 osqwtmPzmjaNramq5VulHdKV5lBidguvphI/3V5A/ZaOry457GfkCsYqENp00CxzcALlL/lufoS wUAJVXW+U0sFU0n1SrLUP3LaJ49PyR34YZVxxtXNMRSYPPI50Use7DNY6dsBFXUStDpGJn9btxo mRtWPWGaQ7+c2RiOdGVuD6xXrUnjL449xL+LMtoXAlGq3XfZ9odU5ZeErN0Ih7iYg/DF4t+r+Ok PZktFmAomV2hC7efUG2iEoZjHe3isyDuUL6fr1bcgCLyTVw9G7yzYRaeMgtV9sboBNq74vP6mOV ARMU1HNJ1qeloRHeif67AP6qJKZtviebDX2sSL5yDRbJaDZNPlcVypG/Bb9Tm0xwCYUn/7BowAc o8+/Qtn987FhJ+9h9NXRcSLHoJswmkVkunqOQgrLf0QUtyVmexCGDSx+g5mm8JLuwHwYbtpN1xq 5TEhmp7RrNRhHYx6ei8nj0= X-Received: by 2002:a05:6300:197:b0:35d:53dc:cb61 with SMTP id adf61e73a8af0-392e005d37emr2005782637.24.1769757538417; Thu, 29 Jan 2026 23:18:58 -0800 (PST) Received: from LAPTOP-UKI113GK.localdomain ([119.123.175.125]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c642aaf1308sm6948316a12.30.2026.01.29.23.18.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Jan 2026 23:18:58 -0800 (PST) From: "licheng.li" To: David Laight , Willy Tarreau Cc: =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , linux-kernel@vger.kernel.org, im.lechain@gmail.com Subject: [PATCH v3 1/2] tools/nolibc: support left alignment (-) in printf Date: Fri, 30 Jan 2026 15:18:46 +0800 Message-ID: <20260130071847.28715-2-im.lechain@gmail.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260130071847.28715-1-im.lechain@gmail.com> References: <20260130071847.28715-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). This patch implements support for the '-' flag to forces left alignment by padding spaces on the right. The implementation reuses the padding character logic to handle both cases. Logic behavior: - "%5d" -> " -5" (unchanged, right align) - "%-5d" -> "-5 " (new, left align) Suggested-by: Willy Tarreau Signed-off-by: Cheng Li --- v3 changes: - Removed pad zeros support because of bug for signed number and pointer 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 | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index 1f16dab2ac88..f31b77f61d3b 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 '-') { + padc =3D c; + c =3D fmt[ofs++]; + } + /* width */ while (c >=3D '0' && c <=3D '9') { width *=3D 10; @@ -358,13 +364,20 @@ 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) { + while (width > w && padc !=3D '-') { + written +=3D 1; if (cb(state, " ", 1) !=3D 0) return -1; - written +=3D 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; + width--; + } } =20 written +=3D len; --=20 2.52.0