From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 9E139326923 for ; Tue, 3 Feb 2026 17:39:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770140384; cv=none; b=Y4vpFpQAtCv6rI62OZKcXi7qOdbbDRlV+JZ7kiHnmHlsRNyVwgggK6LT9ugJ6kt93TRLVUsq+Xii5j8EXdlxgw0YBSX2M0CxuiMBE0X7fY07exrYTVDFKvH4LAGNLhZR9Ah+FI+ODh5IZHKXwCgllxV5KHmWu5w0uPTEajVfNT4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770140384; c=relaxed/simple; bh=f2NETqC3hgdSGVzgBEkxPoGAD3kERii5kb6Ri7DdfHA=; h=Date:From:To:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=NUugtvGV9EiZ1k25YXsesI34hCnkpdo7HzQNRaZzN3LN5UtvNxZhJCnx0bSiH00NCbqupx5yoMieJu/StPWn1OmdjkZakdAt0+2vQ/CMn3JuciYobyqNLRGGPvUdu74YweyyaqFz0w9o5kA+Llg9yZzSs4VeStIjKKVy9xixNA0= 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=VbDEnXqt; arc=none smtp.client-ip=209.85.128.52 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="VbDEnXqt" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-4806bf03573so29397385e9.2 for ; Tue, 03 Feb 2026 09:39:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770140381; x=1770745181; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:to:from:date:from:to:cc:subject:date:message-id :reply-to; bh=UA/lCrV8BvQr6WQowlirJd7amoT4BPvhddXgQofCRHw=; b=VbDEnXqtTx0sL6Fc5jqzgzcrOuv/Z8fAobWcxKsD2S1pyUdbtLYgNzoZFn8/16Z/4z LtZTLa9ad9BDtA+sdKxZ1GDz+SCFC4I2OSNXfCotWe2Z4EkNu452XD103yBQiKTs6PZ0 eFZjVqIgUkzG2CkaX1+ROD4FmlRyiKI8CtTGUh1B7hV1JbZa/MGQr5vOnqppxdeRd8EE x05Wmk+O+JF8lujdiQrlNZbMaB5eDZ+vgvPxup6mqKAe1HljAeu1sxpLK08aMjv8e0by 8Ek2VGUb0klfaqanhb49aUxSfA63KG8Dko/vD97wubvmWeN7FmAJx0nnJl4GuPzOzxD5 1IPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770140381; x=1770745181; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:to:from:date:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=UA/lCrV8BvQr6WQowlirJd7amoT4BPvhddXgQofCRHw=; b=bWonmxsidFmsTm+6qgeh1UZ3oeV5FoGzb3jIbYSUQITyrZlW0cPTLLvqYeUg1pOE6J v6RlB5xJiWrARCOgzJHO3kTlDFG/ELLevhAWxBDoNCpar9iK/rzr57j5OWFwH3qhAlhr rWhFJqRNBpysIkj8QQByIpfo+Cr0sgbFYeDUAQ4vSBdUREdEIeSyFp67eZZlCXOYv6x7 mYo2/ic9mX712IDsORRizXGucwXwVyRAEoR8xEWGAfHLFVN9G4jdenTBzXV3Brm7Wqtb YamhWhqzRrvBzt4KOrahJb69hgwMfIE0LUxfX4dPE+qyORJSG8m1G/FP0bNPqD8IbpJj IXCg== X-Forwarded-Encrypted: i=1; AJvYcCWCjleaPkRIxTBdoXYPQzf+tDLAOBLhEwiblZLfTezK1V73idFtJUfUENnOsyvp6Kwn6bNxXgA9wn9QeVY=@vger.kernel.org X-Gm-Message-State: AOJu0YxhJqqhWj0gsOHqFs/elT9G3SJJgtctHc/M32Gmue1mv782jFpK EFlxlLfPwGq+i77i3e2UMXwJMaq757ps4b0XrrSFGw9LyKygebCfZQE6 X-Gm-Gg: AZuq6aJ9Xa3HR+kDnOGXTBeVTV2YxZLFgp2Ob0D4ZoGmSczBVQLW1ghZOvfhpYRsMxP ENJJIwMCgJFe1lFcsI76PZ4RW1C4mGMPemmjAXwXB0SPsFoGqrlQ3/WekfwxtWThZYw5A/hdJ3o TApbAZGb0Z1r/xHJrIq8JUhWz70yqJ9WJflKJotI/+ArOT73Og1Ay/TTg9rOFj5xmVMj/fcMZsZ gRB/fn9FWPfD1LJtGGs2GhUTWC3cm69pii04lfNqDJ94QoR7GpGsaZDg/xoXSk6o4wPk+jQ2A4S P9yKYnyZ1DoaQsCb3nWgzH5GzaGjDMO1VgV2GzNyuX2Y3pzoBrQgZbMhjQWEZ8k4gAaduwa83Bq DB5v4ZbOkGqc/iuFSHr27DRx6AMbZJ6dMpm2O0KJ9RLtAXUtPwqMzlgEAo91hyGPM/suzMFnHeE RX66c3ZBMW5+gxFY299JdUn//4CTRweXGqb0Q5pJAmWHbUCt9T8rgW X-Received: by 2002:a05:600c:1381:b0:475:dcbb:7903 with SMTP id 5b1f17b1804b1-4830e949290mr5358755e9.9.1770140380664; Tue, 03 Feb 2026 09:39:40 -0800 (PST) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43617e3a3bbsm221638f8f.15.2026.02.03.09.39.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Feb 2026 09:39:40 -0800 (PST) Date: Tue, 3 Feb 2026 17:39:39 +0000 From: David Laight To: Willy Tarreau , Thomas =?UTF-8?B?V2Vpw59zY2h1aA==?= , linux-kernel@vger.kernel.org, Cheng Li Subject: Re: [PATCH next 00/12] tools/nolibc: Enhance printf() Message-ID: <20260203173939.3c5939c7@pumpkin> In-Reply-To: <20260203103000.20206-1-david.laight.linux@gmail.com> References: <20260203103000.20206-1-david.laight.linux@gmail.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Tue, 3 Feb 2026 10:29:48 +0000 david.laight.linux@gmail.com wrote: > From: David Laight > > Update printf() so that it handles almost all the non-fp formats. > In particular: > - Left alignment. > - Zero padding. > - Field precision. > - Variable field width and precision. > - Width modifiers q, L, t and z. > - Conversion specifiers i and X (X generates lower case). > About the only thing that is missing is octal. > > The tests are updated to match. > > There is a slight increase in code size, but it is minimalised > by the the heavy use of bit-pattern matches. I failed to add 'next' to the last two patches, but I don't expect anything to actually happen until after the next merge window. If the patch to change all the number->ascii function is committed I'll add the trivial patch to support octal. For reference this is the final version: /* printf(). Supports most of the normal integer and string formats. * - %[#0-+ ][width|*[.precision|*]][{l,t,z,ll,L,j,q}]{d,i,u,c,x,X,p,s,m,%} * - %% generates a single % * - %m outputs strerror(errno). * - # only affects %x and prepends 0x to non-zero values. * - %o (octal) isn't supported. * - %X outputs a..f the same as %x. * - No support for wide characters. * - invalid formats are copied to the output buffer. */ typedef int (*__nolibc_printf_cb)(void *state, const char *buf, size_t size); #define __PF_FLAG(c) (1u << ((c) & 0x1f)) static __attribute__((unused, format(printf, 3, 0))) int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list args) { char c; int len, written, width, precision; unsigned int flags, c_flag; char tmpbuf[32 + 24]; const char *outstr; written = 0; while (1) { outstr = fmt; c = *fmt++; if (!c) break; width = 0; flags = 0; if (c != '%') { while (*fmt && *fmt != '%') fmt++; len = fmt - outstr; } else { /* we're in a format sequence */ c = *fmt++; /* Flag characters */ for (; c >= 0x20 && c <= 0x3f; c = *fmt++) { if ((__PF_FLAG(c) & (__PF_FLAG('-') | __PF_FLAG(' ') | __PF_FLAG('+') | __PF_FLAG('#') | __PF_FLAG('0'))) == 0) break; flags |= __PF_FLAG(c); } /* width and precision */ for (;; c = *fmt++) { if (c == '*') { precision = va_arg(args, unsigned int); c = *fmt++; } else { for (precision = 0; c >= '0' && c <= '9'; c = *fmt++) precision = precision * 10 + (c - '0'); } if (flags & __PF_FLAG('.')) break; width = precision; if (c != '.') { /* Default precision for strings */ precision = INT_MAX; break; } flags |= __PF_FLAG('.'); } /* Length modifiers are lower case except 'L' which is the same a 'q' */ if ((c >= 'a' && c <= 'z') || (c == 'L' && (c = 'q'))) { if (__PF_FLAG(c) & (__PF_FLAG('l') | __PF_FLAG('t') | __PF_FLAG('z') | __PF_FLAG('j') | __PF_FLAG('q'))) { if (c == 'l' && fmt[0] == 'l') { fmt++; c = 'q'; } /* These all miss "# -0+" */ flags |= __PF_FLAG(c); c = *fmt++; } } /* Conversion specifiers are lower case except 'X' treated as 'x' */ if (!((c >= 'a' && c <= 'z') || (c == 'X' && (c = 'x')))) goto bad_conversion_specifier; /* Numeric and pointer conversion specifiers. * We need to check for "%p" or "%#x" later, merging here gives better code. * But '#' collides with 'c' so shift right. */ c_flag = __PF_FLAG(c) | (flags & __PF_FLAG('#')) >> 1; if (c_flag & (__PF_FLAG('c') | __PF_FLAG('d') | __PF_FLAG('i') | __PF_FLAG('u') | __PF_FLAG('x') | __PF_FLAG('p') | __PF_FLAG('s'))) { unsigned long long v; long long signed_v; char *out = tmpbuf + 32; int sign = 0; /* Annoying 'p' === '0' so mask from flags */ if ((c_flag | (flags & ~__PF_FLAG('p'))) & (__PF_FLAG('p') | __PF_FLAG('s') | __PF_FLAG('l') | __PF_FLAG('t') | __PF_FLAG('z'))) { v = va_arg(args, unsigned long); signed_v = (long)v; } else if (flags & (__PF_FLAG('j') | __PF_FLAG('q'))) { v = va_arg(args, unsigned long long); signed_v = v; } else { v = va_arg(args, unsigned int); signed_v = (int)v; } if (c_flag & __PF_FLAG('c')) { tmpbuf[0] = v; len = 1; outstr = tmpbuf; goto do_output; } if (c_flag & __PF_FLAG('s')) { if (!v) { outstr = "(null)"; /* Match glibc, nothing output if precision too small */ len = precision >= 6 ? 6 : 0; goto do_output; } outstr = (void *)v; do_strnlen_output: len = strnlen(outstr, precision); goto do_output; } if (c_flag & (__PF_FLAG('d') | __PF_FLAG('i'))) { if (signed_v < 0) { sign = '-'; v = -(signed_v + 1); v++; } else if (flags & __PF_FLAG('+')) { sign = '+'; } else if (flags & __PF_FLAG(' ')) { sign = ' '; } } if (v == 0) { /* There are special rules for zero. */ if (c_flag & __PF_FLAG('p')) { /* match glibc, precision is ignored */ outstr = "(nil)"; len = 5; goto do_output; } if (!precision) { /* Explicit %nn.0d, no digits output */ len = 0; goto prepend_sign; } /* "#x" should output "0" not "0x0" */ *out = '0'; len = 1; } else { if (c_flag & (__PF_FLAG('d') | __PF_FLAG('i') | __PF_FLAG('u'))) { len = u64toa_r(v, out); } else { len = u64toh_r(v, out); if (c_flag & (__PF_FLAG('p') | __PF_FLAG('#' - 1))) sign = 'x' | '0' << 8; } } /* Add zero padding */ if (flags & (__PF_FLAG('0') | __PF_FLAG('.'))) { if (!(flags & __PF_FLAG('.'))) { if (flags & __PF_FLAG('-')) /* Left justify overrides zero pad */ goto prepend_sign; /* Zero pad to field width less sign */ precision = width; if (sign) { precision--; if (sign >= 256) precision--; } } if (precision > 30) /* Don't run off the start of tmpbuf[] */ precision = 30; for (; len < precision; len++) { /* Stop gcc generating horrid code and memset(). * This is OPTIMIZER_HIDE_VAR() from compiler.h. */ __asm__ volatile("" : "=r"(len) : "0"(len)); *--out = '0'; } } prepend_sign: for (; sign; sign >>= 8) { len++; *--out = sign; } outstr = out; } else if (c == 'm') { #ifdef NOLIBC_IGNORE_ERRNO outstr = "unknown error"; len = __builtin_strlen(outstr); #else outstr = strerror(errno); goto do_strnlen_output; #endif /* NOLIBC_IGNORE_ERRNO */ } else { bad_conversion_specifier: if (c != '%') /* Invalid format, output the format string */ fmt = outstr + 1; /* %% is documented as a 'conversion specifier'. * Any flags, precision or length modifier are ignored. */ outstr = fmt - 1; len = 1; width = 0; } } do_output: written += len; /* An OPTIMIZER_HIDE_VAR() seems to stop gcc back-merging this * code into one of the conditionals above. */ __asm__ volatile("" : "=r"(len) : "0"(len)); /* Output 'left pad', 'value' then 'right pad'. */ flags &= __PF_FLAG('-'); width -= len; if (flags && cb(state, outstr, len) != 0) return -1; while (width > 0) { int pad_len = ((width - 1) & 15) + 1; width -= pad_len; written += pad_len; if (cb(state, " ", pad_len) != 0) return -1; } if (!flags && cb(state, outstr, len) != 0) return -1; } if (cb(state, NULL, 0) != 0) return -1; return written; } #undef _PF_FLAG struct __nolibc_fprintf_cb_state { FILE *stream; unsigned int buf_offset; char buf[128]; }; static int __nolibc_fprintf_cb(void *v_state, const char *buf, size_t size) { struct __nolibc_fprintf_cb_state *state = v_state; unsigned int off = state->buf_offset; if (off + size > sizeof(state->buf) || buf == NULL) { state->buf_offset = 0; if (off && _fwrite(state->buf, off, state->stream)) return -1; if (size > sizeof(state->buf)) return _fwrite(buf, size, state->stream); off = 0; } if (size) { state->buf_offset = off + size; memcpy(state->buf + off, buf, size); } return 0; } static __attribute__((unused, format(printf, 2, 0))) int vfprintf(FILE *stream, const char *fmt, va_list args) { struct __nolibc_fprintf_cb_state state; state.stream = stream; state.buf_offset = 0; return __nolibc_printf(__nolibc_fprintf_cb, &state, fmt, args); } struct __nolibc_sprintf_cb_state { char *buf; size_t size; }; static int __nolibc_sprintf_cb(void *v_state, const char *buf, size_t size) { struct __nolibc_sprintf_cb_state *state = v_state; char *tgt; if (size >= state->size) { if (state->size <= 1) return 0; size = state->size - 1; } tgt = state->buf; if (size) { state->size -= size; state->buf = tgt + size; memcpy(tgt, buf, size); } else { *tgt = '\0'; } return 0; } static __attribute__((unused, format(printf, 3, 0))) int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) { struct __nolibc_sprintf_cb_state state = { .buf = buf, .size = size }; return __nolibc_printf(__nolibc_sprintf_cb, &state, fmt, args); }