[hackers] [sbase] Properly handle width and precision in printf(1) || FRIGN

From: <git_AT_suckless.org>
Date: Fri, 27 Mar 2015 10:06:07 +0100 (CET)

commit 096da1297ba038efeb64acdc85a25b67757f95ad
Author: FRIGN <dev_AT_frign.de>
Date: Thu Mar 26 23:00:02 2015 +0100

    Properly handle width and precision in printf(1)

diff --git a/printf.c b/printf.c
index c87cfcb..770b7cb 100644
--- a/printf.c
+++ b/printf.c
_AT_@ -49,7 +49,7 @@ main(int argc, char *argv[])
                 }
 
                 /* field width */
- width = 0;
+ width = -1;
                 for (i++; strchr("#-+ 0", format[i]); i++);
                 if (format[i] == '*') {
                         if (argi < argc)
_AT_@ -65,17 +65,20 @@ main(int argc, char *argv[])
                                 tmp[i - j] = 0;
                                 width = estrtonum(tmp, 0, INT_MAX);
                                 free(tmp);
+ } else {
+ width = 0;
                         }
                 }
 
                 /* field precision */
- precision = 6;
+ precision = -1;
                 if (format[i] == '.') {
                         if (format[++i] == '*') {
                                 if (argi < argc)
                                         precision = estrtonum(argv[argi++], 0, INT_MAX);
                                 else
                                         cooldown = 1;
+ i++;
                         } else {
                                 j = i;
                                 for (; strchr("+-0123456789", format[i]); i++);
_AT_@ -84,6 +87,8 @@ main(int argc, char *argv[])
                                         tmp[i - j] = 0;
                                         precision = estrtonum(tmp, 0, INT_MAX);
                                         free(tmp);
+ } else {
+ precision = 0;
                                 }
                         }
                 }
_AT_@ -117,7 +122,7 @@ main(int argc, char *argv[])
                         free(rarg);
                         break;
                 case 's':
- fputs(arg, stdout);
+ printf("%*.*s", width, precision, arg);
                         break;
                 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X':
                         arglen = strlen(arg);
Received on Fri Mar 27 2015 - 10:06:07 CET

This archive was generated by hypermail 2.3.0 : Fri Mar 27 2015 - 10:12:09 CET