sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit 096da1297ba038efeb64acdc85a25b67757f95ad
parent b7886f3e672bd40a8b1021b2182ae764df223423
Author: FRIGN <dev@frign.de>
Date:   Thu, 26 Mar 2015 23:00:02 +0100

Properly handle width and precision in printf(1)

Diffstat:
Mprintf.c | 11++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/printf.c b/printf.c @@ -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) @@ -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++); @@ -84,6 +87,8 @@ main(int argc, char *argv[]) tmp[i - j] = 0; precision = estrtonum(tmp, 0, INT_MAX); free(tmp); + } else { + precision = 0; } } } @@ -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);