commit 1cc5a57fd7f0c72d9e4c383cfb5c7decf39fa346
Author: Hiltjo Posthuma <[email protected]>
AuthorDate: Sun Jul 25 14:24:41 2021 +0200
Commit: Hiltjo Posthuma <[email protected]>
CommitDate: Mon Jul 26 20:10:09 2021 +0200
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"
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++);