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.
This commit is contained in:
FRIGN 2015-04-27 20:27:37 +02:00 committed by sin
parent b333176b8c
commit 0b76449414

View File

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