aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2021-07-12 11:27:11 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2021-07-12 11:27:11 +0200
commit8d269ef85984f6476e7fdbec2c5a70f3b5c48a72 (patch)
tree212f9c7fedad072a76e2bbd67190bd2c0f651529
parentcaa93ecdd3a9b998a69dcbfafdddbc9c58887ec3 (diff)
downloadbusybox-w32-8d269ef85984f6476e7fdbec2c5a70f3b5c48a72.tar.gz
busybox-w32-8d269ef85984f6476e7fdbec2c5a70f3b5c48a72.tar.bz2
busybox-w32-8d269ef85984f6476e7fdbec2c5a70f3b5c48a72.zip
awk: fix printf "%-10c", 0
function old new delta awk_printf 596 626 +30 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--editors/awk.c9
-rwxr-xr-xtestsuite/awk.tests8
2 files changed, 13 insertions, 4 deletions
diff --git a/editors/awk.c b/editors/awk.c
index 465033f5f..437d87ecf 100644
--- a/editors/awk.c
+++ b/editors/awk.c
@@ -2360,10 +2360,11 @@ static char *awk_printf(node *n, size_t *len)
2360 */ 2360 */
2361 if (c == 'c') { 2361 if (c == 'c') {
2362 char cc = is_numeric(arg) ? getvar_i(arg) : *getvar_s(arg); 2362 char cc = is_numeric(arg) ? getvar_i(arg) : *getvar_s(arg);
2363 s = xasprintf(s, cc); 2363 char *r = xasprintf(s, cc ? cc : '^' /* else strlen will be wrong */);
2364 /* + 1 if cc == NUL: handle printf "%c" 0 case 2364 slen = strlen(r);
2365 * (and printf "%22c" 0 etc, but still fails for e.g. printf "%-22c" 0) */ 2365 if (cc == '\0') /* if cc is NUL, re-format the string with it */
2366 slen = strlen(s) + (cc == '\0'); 2366 sprintf(r, s, cc);
2367 s = r;
2367 } else { 2368 } else {
2368 if (c == 's') { 2369 if (c == 's') {
2369 s = xasprintf(s, getvar_s(arg)); 2370 s = xasprintf(s, getvar_s(arg));
diff --git a/testsuite/awk.tests b/testsuite/awk.tests
index 242c897d1..3cddb4dd4 100755
--- a/testsuite/awk.tests
+++ b/testsuite/awk.tests
@@ -415,6 +415,14 @@ testing "awk printf('%c') can output NUL" \
415 "awk '{printf(\"hello%c null\n\", 0)}'" "hello\0 null\n" "" "\n" 415 "awk '{printf(\"hello%c null\n\", 0)}'" "hello\0 null\n" "" "\n"
416SKIP= 416SKIP=
417 417
418optional FEATURE_AWK_GNU_EXTENSIONS
419testing "awk printf('%-10c') can output NUL" \
420 "awk 'BEGIN { printf \"[%-10c]\n\", 0 }' | od -tx1" "\
4210000000 5b 00 20 20 20 20 20 20 20 20 20 5d 0a
4220000015
423" "" ""
424SKIP=
425
418# testing "description" "command" "result" "infile" "stdin" 426# testing "description" "command" "result" "infile" "stdin"
419testing 'awk negative field access' \ 427testing 'awk negative field access' \
420 'awk 2>&1 -- '\''{ $(-1) }'\' \ 428 'awk 2>&1 -- '\''{ $(-1) }'\' \