From 9600642ba4c86950fe3c6ac27e0fda814481ee1d Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Mon, 14 Nov 2022 11:06:43 -0600 Subject: [PATCH] Call function that works like strstart() but on a char chrstart(). --- toys/posix/printf.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/toys/posix/printf.c b/toys/posix/printf.c index eedd83e9..be06bd92 100644 --- a/toys/posix/printf.c +++ b/toys/posix/printf.c @@ -23,7 +23,7 @@ config PRINTF #include "toys.h" // Detect matching character (return true/false) and advance pointer if match. -static int eat(char **s, char c) +static int chrstart(char **s, char c) { int x = (**s == c); @@ -42,7 +42,7 @@ static int handle_slash(char **esc_val, int posix) if (*ptr == 'c') xexit(); // 0x12 hex escapes have 1-2 digits, \123 octal escapes have 1-3 digits. - if (eat(&ptr, 'x')) base = 16; + if (chrstart(&ptr, 'x')) base = 16; else { if (posix && *ptr=='0') ptr++; if (*ptr >= '0' && *ptr <= '7') base = 8; @@ -85,8 +85,8 @@ void printf_main(void) // Loop through characters in format while (*f) { - if (eat(&f, '\\')) putchar(handle_slash(&f, 0)); - else if (!eat(&f, '%') || *f == '%') putchar(*f++); + if (chrstart(&f, '\\')) putchar(handle_slash(&f, 0)); + else if (!chrstart(&f, '%') || *f == '%') putchar(*f++); // Handle %escape else { @@ -97,10 +97,10 @@ void printf_main(void) *to++ = '%'; while (strchr("-+# '0", *f) && (to-toybuf)<10) *to++ = *f++; for (;;) { - if (eat(&f, '*')) { + if (chrstart(&f, '*')) { if (*arg) wp[i] = atolx(*arg++); } else while (*f >= '0' && *f <= '9') wp[i] = (wp[i]*10)+(*f++)-'0'; - if (i++ || !eat(&f, '.')) break; + if (i++ || !chrstart(&f, '.')) break; wp[1] = 0; } c = *f++; @@ -110,7 +110,8 @@ void printf_main(void) // Output %esc using parsed format string if (c == 'b') { - while (*aa) putchar(eat(&aa, '\\') ? handle_slash(&aa, 1) : *aa++); + while (*aa) + putchar(chrstart(&aa, '\\') ? handle_slash(&aa, 1) : *aa++); continue; } else if (c == 'c') printf(toybuf, wp[0], wp[1], *aa); -- 2.39.2