Hi,

>From da499fb89b024e2fdbbe04652b00b1f7f1152523 Mon Sep 17 00:00:00 2001
From: Hiltjo Posthuma <hil...@codemadness.org>
Date: Sun, 25 Jul 2021 14:24:41 +0200
Subject: [PATCH] printf: allow flags for the %s format string aswell

This is useful for example to left-align strings and pad them with spaces.

printf '%-12.12s: %s\n' 'user' "$USER"
---
 printf.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/printf.c b/printf.c
index 4bc645b..039dac7 100644
--- a/printf.c
+++ b/printf.c
@@ -127,7 +127,11 @@ main(int argc, char *argv[])
                        free(rarg);
                        break;
                case 's':
-                       printf("%*.*s", width, precision, arg);
+                       fmt = estrdup(flag ? "%#*.*s" : "%*.*s");
+                       if (flag)
+                               fmt[1] = flag;
+                       printf(fmt, width, precision, arg);
+                       free(fmt);
                        break;
                case 'd': case 'i': case 'o': case 'u': case 'x': case 'X':
                        for (j = 0; isspace(arg[j]); j++);
-- 
2.32.0

-- 
Kind regards,
Hiltjo

Reply via email to