From be23fca994e1dd8b81ea49cb735834e4008839fb Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Wed, 27 Jan 2021 12:46:44 +0000 Subject: printf: code shrink Instead of constructing null-terminated strings to print, output the characters between the 's' and 't' pointers using fwrite(). This avoids having to output two separate strings when an escaped literal NUL character is encountered in the format string. Saves 32 bytes. --- coreutils/printf.c | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/coreutils/printf.c b/coreutils/printf.c index d1d22f39c..01f730073 100644 --- a/coreutils/printf.c +++ b/coreutils/printf.c @@ -152,9 +152,9 @@ static double my_xstrtod(const char *arg) } #if ENABLE_PLATFORM_MINGW32 -static int fputs_stdout(const char *s) +static size_t fwrite_stdout(const char *s, const char *t) { - return fputs(s, stdout); + return fwrite(s, t - s, 1, stdout); } #endif @@ -202,8 +202,7 @@ static int print_esc_string(const char *str) } #if ENABLE_PLATFORM_MINGW32 finish: - *t = '\0'; - fputs_stdout(s); + fwrite_stdout(s, t); free(s); return ret; #else @@ -331,8 +330,7 @@ static char **print_formatted(char *f, char **argv, int *conv_err) switch (*f) { case '%': #if ENABLE_PLATFORM_MINGW32 - *t = '\0'; - fputs_stdout(s); + fwrite_stdout(s, t); t = s; #endif direc_start = f++; @@ -426,19 +424,10 @@ static char **print_formatted(char *f, char **argv, int *conv_err) #if ENABLE_PLATFORM_MINGW32 case '\\': if (*++f == 'c') { - *t = '\0'; - fputs_stdout(s); + fwrite_stdout(s, t); return saved_argv; /* causes main() to exit */ } - *t = bb_process_escape_sequence((const char **)&f); - if (*t == '\0') { - fputs_stdout(s); - bb_putchar(*t); - t = s; - } - else { - ++t; - } + *t++ = bb_process_escape_sequence((const char **)&f); f--; break; default: @@ -457,8 +446,7 @@ static char **print_formatted(char *f, char **argv, int *conv_err) } } #if ENABLE_PLATFORM_MINGW32 - *t = '\0'; - fputs_stdout(s); + fwrite_stdout(s, t); #endif return argv; -- cgit v1.2.3-55-g6feb