[hackers] [sbase] printf: allow flags for the %s format string aswell || Hiltjo Posthuma

From: <git_AT_suckless.org>
Date: Mon, 26 Jul 2021 20:10:50 +0200 (CEST)

commit 1cc5a57fd7f0c72d9e4c383cfb5c7decf39fa346
Author: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
AuthorDate: Sun Jul 25 14:24:41 2021 +0200
Commit: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
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
_AT_@ -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++);
Received on Mon Jul 26 2021 - 20:10:50 CEST

This archive was generated by hypermail 2.3.0 : Mon Jul 26 2021 - 20:12:32 CEST