From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailtransmit05.runbox.com (mailtransmit05.runbox.com [185.226.149.38]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B687A3ACF16 for ; Mon, 2 Mar 2026 10:18:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.226.149.38 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772446756; cv=none; b=OKYwjj4MQfK7kdPyJtU3pCeel25e+Vnrq/eiIRglh1YgYeXvcAXKx4oN/QtBoj2ssp2wzaTVPIOFEgFjnvSdjQvjVzr6z7+VUqgTUSG36MSVNz0ui0J1GtUYj/9fmyQNZ/kTDA7fd99S0ryZEBUcZB9fTgYtze8KSfXh3n4t9h8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772446756; c=relaxed/simple; bh=oUuKM7Lz3NS9EPu4zb2IRkNBVb2Ts8eL5y+60Y2fCrk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BkN3gO6Iz1Og6ByANVFYw1+pfo40K7Zxv/607FK7hwhYBLsB5xaEVOEJVbLBPH07chNZIR6o1mU09ag4m0IW6OwnGjV+9EaIxh5szVCTn1hC6vPbR04aQ7vJNHpQomsiyvevBLACIyDxADxdJ//rGeCJL1TnHIdiieotgs+AXAQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=runbox.com; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b=ul+xNE6X; arc=none smtp.client-ip=185.226.149.38 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=runbox.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b="ul+xNE6X" Received: from mailtransmit03.runbox ([10.9.9.163] helo=aibo.runbox.com) by mailtransmit05.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1vx0MZ-003Bdp-UD; Mon, 02 Mar 2026 11:18:55 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=runbox.com; s=selector2; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To :Message-Id:Date:Subject:Cc:To:From; bh=b4B3QhvEGTvDq9Z1PC3/IWKV0iKMpZ0UDqB4a5XMnIw=; b=ul+xNE6Xatt26RVKeMHMocm81m rhn3zUvbQyW8fOO6GWKwBolX8rFkpKQ8MGVb5rf83inOSmmwsLUEBbtPXvqNg0QZfuMLZiaagXQLV 3xqylsLHzgKe0PKkkULHdcjMyugYPegvDqwLmJuXoB95Klf9m1qNzHDDhVWWRz/RSc0pqgah0tIZd h4u78DPWhxK+S0Jlrrc6xAIsldxCb8QRQKwxspfLZACB1WBnJ37LudeXgr7UQt/R9ASWL3uPoHVz+ EfL6ehHLV0HkLY50PBQRa1M/pkouITWU8+zBgSaqKbb1k41wN6isFSfrY5yeALThd6ZhHxgiKBC7F wgfj7b4g==; Received: from [10.9.9.72] (helo=submission01.runbox) by mailtransmit03.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1vx0MZ-0002zD-KO; Mon, 02 Mar 2026 11:18:55 +0100 Received: by submission01.runbox with esmtpsa [Authenticated ID (1493616)] (TLS1.2:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) id 1vx0MD-006y7o-NP; Mon, 02 Mar 2026 11:18:33 +0100 From: david.laight.linux@gmail.com To: Willy Tarreau , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , linux-kernel@vger.kernel.org, Cheng Li Cc: David Laight Subject: [PATCH v4 next 20/23] tools/nolibc/printf: Add support for left aligning fields Date: Mon, 2 Mar 2026 10:18:12 +0000 Message-Id: <20260302101815.3043-21-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20260302101815.3043-1-david.laight.linux@gmail.com> References: <20260302101815.3043-1-david.laight.linux@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: 8bit From: David Laight Output the characters before or after the pad - writing the pad takes more code. Include additional/changed tests Signed-off-by: David Laight --- Unchanged for v4. Changes for v3: - Formally part of patch 7. tools/include/nolibc/stdio.h | 6 +++++- tools/testing/selftests/nolibc/nolibc-test.c | 4 +++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index ef8e7069d5cf..695676b44151 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -544,7 +544,11 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list /* Stop gcc back-merging this code into one of the conditionals above. */ _NOLIBC_OPTIMIZER_HIDE_VAR(len); + /* Output the characters on the required side of any padding. */ width -= len; + flags = _NOLIBC_PF_FLAGS_CONTAIN(flags, '-'); + if (flags && cb(state, outstr, len) != 0) + return -1; while (width > 0) { /* Output pad in 16 byte blocks with the small block first. */ int pad_len = ((width - 1) & 15) + 1; @@ -553,7 +557,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list if (cb(state, " ", pad_len) != 0) return -1; } - if (cb(state, outstr, len) != 0) + if (!flags && cb(state, outstr, len) != 0) return -1; } diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 4267c69ec37c..89ded1dd7318 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -1845,9 +1845,11 @@ static int run_printf(int min, int max) CASE_TEST(truncation); EXPECT_VFPRINTF(1, "0123456789012345678901234", "%s", "0123456789012345678901234"); break; CASE_TEST(string_width); EXPECT_VFPRINTF(1, " 1", "%10s", "1"); break; CASE_TEST(number_width); EXPECT_VFPRINTF(1, " 1", "%10d", 1); break; + CASE_TEST(number_left); EXPECT_VFPRINTF(1, "|-5 |", "|%-8d|", -5); break; + CASE_TEST(string_align); EXPECT_VFPRINTF(1, "|foo |", "|%-8s|", "foo"); break; CASE_TEST(width_trunc); EXPECT_VFPRINTF(1, " 1", "%25d", 1); break; CASE_TEST(errno); errno = 22; EXPECT_VFPRINTF(is_nolibc, "errno=22", "%m"); break; - CASE_TEST(errno-neg); errno = -22; EXPECT_VFPRINTF(is_nolibc, " errno=-22", "%12m"); break; + CASE_TEST(errno-neg); errno = -22; EXPECT_VFPRINTF(is_nolibc, "errno=-22 ", "%-12m"); break; CASE_TEST(scanf); EXPECT_ZR(1, test_scanf()); break; CASE_TEST(printf_error); EXPECT_ZR(1, test_printf_error()); break; case __LINE__: -- 2.39.5