diff options
author | Jilles Tjoelker <jilles@FreeBSD.org> | 2017-04-29 21:48:11 +0000 |
---|---|---|
committer | Jilles Tjoelker <jilles@FreeBSD.org> | 2017-04-29 21:48:11 +0000 |
commit | 342b089bcabe39424149ef0f654713a062623657 (patch) | |
tree | 98c2f4139049f17f7330747ce27c54731d8edc5d /usr.bin | |
parent | 10e0318afac9a26bd6ef86585033783be4888582 (diff) | |
download | src-342b089bcabe39424149ef0f654713a062623657.tar.gz src-342b089bcabe39424149ef0f654713a062623657.zip |
printf: Output formatted data directly, instead of via asprintf.
Long ago, sh used to have its own optimized and restricted string formatting
implementation, which the printf builtin had to bypass via asprintf() to a
temporary buffer. Since sh has used libc's string formatting implementation
for a long time, remove the workaround.
Add a check to keep printf %c '' working the same way (output nothing);
POSIX allows both outputting nothing and outputting a NUL byte.
Also, this change avoids silently discarding format directives for whose
output asprintf() cannot allocate memory.
Notes
Notes:
svn path=/head/; revision=317598
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/printf/printf.c | 16 |
1 files changed, 6 insertions, 10 deletions
diff --git a/usr.bin/printf/printf.c b/usr.bin/printf/printf.c index f21a0152a477..62704416599e 100644 --- a/usr.bin/printf/printf.c +++ b/usr.bin/printf/printf.c @@ -70,20 +70,15 @@ static const char rcsid[] = #endif #define PF(f, func) do { \ - char *b = NULL; \ if (havewidth) \ if (haveprec) \ - (void)asprintf(&b, f, fieldwidth, precision, func); \ + (void)printf(f, fieldwidth, precision, func); \ else \ - (void)asprintf(&b, f, fieldwidth, func); \ + (void)printf(f, fieldwidth, func); \ else if (haveprec) \ - (void)asprintf(&b, f, precision, func); \ + (void)printf(f, precision, func); \ else \ - (void)asprintf(&b, f, func); \ - if (b) { \ - (void)fputs(b, stdout); \ - free(b); \ - } \ + (void)printf(f, func); \ } while (0) static int asciicode(void); @@ -394,7 +389,8 @@ printf_doformat(char *fmt, int *rval) char p; p = getchr(); - PF(start, p); + if (p != '\0') + PF(start, p); break; } case 's': { |