[hackers] [sbase] Add flag support to printf(1) || FRIGN

From: <git_AT_suckless.org>
Date: Mon, 27 Apr 2015 20:34:02 +0200 (CEST)

commit 0b7644941494f9adb3d30c2ecec85a0192c99ae4
Author: FRIGN <dev_AT_frign.de>
Date: Mon Apr 27 20:27:37 2015 +0200

    Add flag support to printf(1)
    
    We skipped flags before, so no real bug, but it's trivial to add
    now, so there's no reason not to do it.

diff --git a/printf.c b/printf.c
index 6c99ad2..1335906 100644
--- a/printf.c
+++ b/printf.c
_AT_@ -22,7 +22,7 @@ main(int argc, char *argv[])
         long long num;
         double dou;
         int cooldown = 0, width, precision;
- char *format, *tmp, *arg, *fmt;
+ char *format, *tmp, *arg, *fmt, flag;
 
         argv0 = argv[0];
         if (argc < 2)
_AT_@ -48,9 +48,13 @@ main(int argc, char *argv[])
                         continue;
                 }
 
+ /* flag */
+ for (flag = ' ', i++; strchr("#-+ 0", format[i]); i++) {
+ flag = format[i];
+ }
+
                 /* field width */
                 width = -1;
- for (i++; strchr("#-+ 0", format[i]); i++);
                 if (format[i] == '*') {
                         if (argi < argc)
                                 width = estrtonum(argv[argi++], 0, INT_MAX);
_AT_@ -133,14 +137,16 @@ main(int argc, char *argv[])
                                 num = rarg[0];
                         } else
                                 num = (strlen(arg) > 0) ? estrtonum(arg, LLONG_MIN, LLONG_MAX) : 0;
- fmt = estrdup("%*ll#");
- fmt[4] = format[i];
+ fmt = estrdup("%#*ll#");
+ fmt[1] = flag;
+ fmt[5] = format[i];
                         printf(fmt, width, num);
                         free(fmt);
                         break;
                 case 'a': case 'A': case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
- fmt = estrdup("%*.*#");
- fmt[4] = format[i];
+ fmt = estrdup("%#*.*#");
+ fmt[1] = flag;
+ fmt[5] = format[i];
                         dou = (strlen(arg) > 0) ? estrtod(arg) : 0;
                         printf(fmt, width, precision, dou);
                         free(fmt);
Received on Mon Apr 27 2015 - 20:34:02 CEST

This archive was generated by hypermail 2.3.0 : Mon Apr 27 2015 - 20:36:20 CEST