aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2021-01-27 12:46:44 +0000
committerRon Yorston <rmy@pobox.com>2021-01-27 12:46:44 +0000
commitbe23fca994e1dd8b81ea49cb735834e4008839fb (patch)
tree3bee2e53aa2fda477b7a99437961a1f8f3ca2e57
parent7e1c2cb3b2c917f799d406e186fa84dcac19e992 (diff)
downloadbusybox-w32-be23fca994e1dd8b81ea49cb735834e4008839fb.tar.gz
busybox-w32-be23fca994e1dd8b81ea49cb735834e4008839fb.tar.bz2
busybox-w32-be23fca994e1dd8b81ea49cb735834e4008839fb.zip
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.
-rw-r--r--coreutils/printf.c26
1 files 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)
152} 152}
153 153
154#if ENABLE_PLATFORM_MINGW32 154#if ENABLE_PLATFORM_MINGW32
155static int fputs_stdout(const char *s) 155static size_t fwrite_stdout(const char *s, const char *t)
156{ 156{
157 return fputs(s, stdout); 157 return fwrite(s, t - s, 1, stdout);
158} 158}
159#endif 159#endif
160 160
@@ -202,8 +202,7 @@ static int print_esc_string(const char *str)
202 } 202 }
203#if ENABLE_PLATFORM_MINGW32 203#if ENABLE_PLATFORM_MINGW32
204 finish: 204 finish:
205 *t = '\0'; 205 fwrite_stdout(s, t);
206 fputs_stdout(s);
207 free(s); 206 free(s);
208 return ret; 207 return ret;
209#else 208#else
@@ -331,8 +330,7 @@ static char **print_formatted(char *f, char **argv, int *conv_err)
331 switch (*f) { 330 switch (*f) {
332 case '%': 331 case '%':
333#if ENABLE_PLATFORM_MINGW32 332#if ENABLE_PLATFORM_MINGW32
334 *t = '\0'; 333 fwrite_stdout(s, t);
335 fputs_stdout(s);
336 t = s; 334 t = s;
337#endif 335#endif
338 direc_start = f++; 336 direc_start = f++;
@@ -426,19 +424,10 @@ static char **print_formatted(char *f, char **argv, int *conv_err)
426#if ENABLE_PLATFORM_MINGW32 424#if ENABLE_PLATFORM_MINGW32
427 case '\\': 425 case '\\':
428 if (*++f == 'c') { 426 if (*++f == 'c') {
429 *t = '\0'; 427 fwrite_stdout(s, t);
430 fputs_stdout(s);
431 return saved_argv; /* causes main() to exit */ 428 return saved_argv; /* causes main() to exit */
432 } 429 }
433 *t = bb_process_escape_sequence((const char **)&f); 430 *t++ = bb_process_escape_sequence((const char **)&f);
434 if (*t == '\0') {
435 fputs_stdout(s);
436 bb_putchar(*t);
437 t = s;
438 }
439 else {
440 ++t;
441 }
442 f--; 431 f--;
443 break; 432 break;
444 default: 433 default:
@@ -457,8 +446,7 @@ static char **print_formatted(char *f, char **argv, int *conv_err)
457 } 446 }
458 } 447 }
459#if ENABLE_PLATFORM_MINGW32 448#if ENABLE_PLATFORM_MINGW32
460 *t = '\0'; 449 fwrite_stdout(s, t);
461 fputs_stdout(s);
462#endif 450#endif
463 451
464 return argv; 452 return argv;