awk: fix printf %%

A refactor of the awk printf code in
e2e3802987
appears to have broken the printf interpretation of two percent signs,
which normally outputs only one percent sign.

The patch below brings busybox awk printf behavior back into alignment
with the pre-e2e380 behavior, the busybox printf util, and other common
(awk and non-awk) printf implementations.

function                                             old     new   delta
awk_printf                                           626     672     +46

Signed-off-by: Daniel Thau <danthau at bedrocklinux.org>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Daniel Thau 2021-09-02 07:41:08 -04:00 committed by Denys Vlasenko
parent f4ba69d476
commit 7d06d6e186
3 changed files with 19 additions and 1 deletions

View File

@ -2346,8 +2346,15 @@ static char *awk_printf(node *n, size_t *len)
size_t slen;
s = f;
while (*f && (*f != '%' || *++f == '%'))
while (*f && *f != '%')
f++;
c = *++f;
if (c == '%') { /* double % */
slen = f - s;
s = xstrndup(s, slen);
f++;
goto tail;
}
while (*f && !isalpha(*f)) {
if (*f == '*')
syntax_error("%*x formats are not supported");

View File

@ -463,4 +463,10 @@ testing "awk \"cmd\" | getline" \
"HELLO\n" \
'' ''
# printf %% should print one % (had a bug where it didn't)
testing 'awk printf %% prints one %' \
"awk 'BEGIN { printf \"%%\n\" }'" \
"%\n" \
'' ''
exit $FAILCOUNT

View File

@ -79,6 +79,11 @@ testing "printf understands %Ld" \
"-5\n""0\n" \
"" ""
testing "printf understands %%" \
"${bb}printf '%%\n' 2>&1; echo \$?" \
"%\n""0\n" \
"" ""
testing "printf handles positive numbers for %d" \
"${bb}printf '%d\n' 3 +3 ' 3' ' +3' 2>&1; echo \$?" \
"3\n"\