1
0
mirror of git://git.suckless.org/sbase synced 2024-12-25 16:32:37 +00:00

Properly handle width and precision in printf(1)

This commit is contained in:
FRIGN 2015-03-26 23:00:02 +01:00
parent b7886f3e67
commit 096da1297b

View File

@ -49,7 +49,7 @@ main(int argc, char *argv[])
} }
/* field width */ /* field width */
width = 0; width = -1;
for (i++; strchr("#-+ 0", format[i]); i++); for (i++; strchr("#-+ 0", format[i]); i++);
if (format[i] == '*') { if (format[i] == '*') {
if (argi < argc) if (argi < argc)
@ -65,17 +65,20 @@ main(int argc, char *argv[])
tmp[i - j] = 0; tmp[i - j] = 0;
width = estrtonum(tmp, 0, INT_MAX); width = estrtonum(tmp, 0, INT_MAX);
free(tmp); free(tmp);
} else {
width = 0;
} }
} }
/* field precision */ /* field precision */
precision = 6; precision = -1;
if (format[i] == '.') { if (format[i] == '.') {
if (format[++i] == '*') { if (format[++i] == '*') {
if (argi < argc) if (argi < argc)
precision = estrtonum(argv[argi++], 0, INT_MAX); precision = estrtonum(argv[argi++], 0, INT_MAX);
else else
cooldown = 1; cooldown = 1;
i++;
} else { } else {
j = i; j = i;
for (; strchr("+-0123456789", format[i]); i++); for (; strchr("+-0123456789", format[i]); i++);
@ -84,6 +87,8 @@ main(int argc, char *argv[])
tmp[i - j] = 0; tmp[i - j] = 0;
precision = estrtonum(tmp, 0, INT_MAX); precision = estrtonum(tmp, 0, INT_MAX);
free(tmp); free(tmp);
} else {
precision = 0;
} }
} }
} }
@ -117,7 +122,7 @@ main(int argc, char *argv[])
free(rarg); free(rarg);
break; break;
case 's': case 's':
fputs(arg, stdout); printf("%*.*s", width, precision, arg);
break; break;
case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': case 'd': case 'i': case 'o': case 'u': case 'x': case 'X':
arglen = strlen(arg); arglen = strlen(arg);