[PATCH] awk: fix printf %%

Daniel Thau danthau at bedrocklinux.org
Thu Sep 2 11:41:08 UTC 2021


A refactor of the awk printf code in
e2e3802987266c98df0efdf40ad5da4b07df0113
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.

Please let me know if there's anything else I can or should do ensure
the awk printf %% handling is remedied.

Thanks!

Signed-off-by: Daniel "paradigm" Thau <danthau at bedrocklinux.org>
---
 editors/awk.c          | 9 ++++++++-
 testsuite/awk.tests    | 6 ++++++
 testsuite/printf.tests | 5 +++++
 3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/editors/awk.c b/editors/awk.c
index 3adbca7aa..162000697 100644
--- a/editors/awk.c
+++ b/editors/awk.c
@@ -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++;
+		if (*++f == '%') { /* double % */
+			*f = '\0';
+			s = xstrdup(s);
+			slen = strlen(s);
+			*f++ = c = '%';
+			goto tail;
+		}
 		while (*f && !isalpha(*f)) {
 			if (*f == '*')
 				syntax_error("%*x formats are not supported");
diff --git a/testsuite/awk.tests b/testsuite/awk.tests
index dc2ae2e11..bcaafe8fd 100755
--- a/testsuite/awk.tests
+++ b/testsuite/awk.tests
@@ -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
diff --git a/testsuite/printf.tests b/testsuite/printf.tests
index 34a65926e..050edef71 100755
--- a/testsuite/printf.tests
+++ b/testsuite/printf.tests
@@ -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"\
-- 
2.20.1




More information about the busybox mailing list