diff options
| author | Bernhard Reutner-Fischer <rep.dot.nop@gmail.com> | 2018-10-19 15:25:41 +0200 |
|---|---|---|
| committer | Bernhard Reutner-Fischer <rep.dot.nop@gmail.com> | 2018-10-19 15:27:42 +0200 |
| commit | 3db4e7f84cf795de8559ea0d96eaa491999ccf24 (patch) | |
| tree | 0260c160e82214f81dc2aeaef7764e4bb0c632ef | |
| parent | 7effa31cd4b5c76d20f63882002eb023f05aaa46 (diff) | |
| download | busybox-w32-3db4e7f84cf795de8559ea0d96eaa491999ccf24.tar.gz busybox-w32-3db4e7f84cf795de8559ea0d96eaa491999ccf24.tar.bz2 busybox-w32-3db4e7f84cf795de8559ea0d96eaa491999ccf24.zip | |
printf: fix printing +-prefixed numbers
Thanks to Cristian Ionescu-Idbohrn for noticing.
Also fix "%d" ' 42' to skip leading whitespace.
function old new delta
print_direc 435 454 +19
bb_strtoll 99 103 +4
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 23/0) Total: 23 bytes
Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
| -rw-r--r-- | coreutils/printf.c | 5 | ||||
| -rw-r--r-- | libbb/bb_strtonum.c | 2 | ||||
| -rwxr-xr-x | testsuite/printf.tests | 33 |
3 files changed, 37 insertions, 3 deletions
diff --git a/coreutils/printf.c b/coreutils/printf.c index a666ff7ac..b2429c5cf 100644 --- a/coreutils/printf.c +++ b/coreutils/printf.c | |||
| @@ -191,6 +191,7 @@ static void print_direc(char *format, unsigned fmt_length, | |||
| 191 | if (have_width - 1 == have_prec) | 191 | if (have_width - 1 == have_prec) |
| 192 | have_width = NULL; | 192 | have_width = NULL; |
| 193 | 193 | ||
| 194 | /* multiconvert sets errno = 0, but %s needs it cleared */ | ||
| 194 | errno = 0; | 195 | errno = 0; |
| 195 | 196 | ||
| 196 | switch (format[fmt_length - 1]) { | 197 | switch (format[fmt_length - 1]) { |
| @@ -199,7 +200,7 @@ static void print_direc(char *format, unsigned fmt_length, | |||
| 199 | break; | 200 | break; |
| 200 | case 'd': | 201 | case 'd': |
| 201 | case 'i': | 202 | case 'i': |
| 202 | llv = my_xstrtoll(argument); | 203 | llv = my_xstrtoll(skip_whitespace(argument)); |
| 203 | print_long: | 204 | print_long: |
| 204 | if (!have_width) { | 205 | if (!have_width) { |
| 205 | if (!have_prec) | 206 | if (!have_prec) |
| @@ -217,7 +218,7 @@ static void print_direc(char *format, unsigned fmt_length, | |||
| 217 | case 'u': | 218 | case 'u': |
| 218 | case 'x': | 219 | case 'x': |
| 219 | case 'X': | 220 | case 'X': |
| 220 | llv = my_xstrtoull(argument); | 221 | llv = my_xstrtoull(skip_whitespace(argument)); |
| 221 | /* cheat: unsigned long and long have same width, so... */ | 222 | /* cheat: unsigned long and long have same width, so... */ |
| 222 | goto print_long; | 223 | goto print_long; |
| 223 | case 's': | 224 | case 's': |
diff --git a/libbb/bb_strtonum.c b/libbb/bb_strtonum.c index 2185017b0..cb70f1053 100644 --- a/libbb/bb_strtonum.c +++ b/libbb/bb_strtonum.c | |||
| @@ -81,7 +81,7 @@ long long FAST_FUNC bb_strtoll(const char *arg, char **endp, int base) | |||
| 81 | /* Check for the weird "feature": | 81 | /* Check for the weird "feature": |
| 82 | * a "-" string is apparently a valid "number" for strto[u]l[l]! | 82 | * a "-" string is apparently a valid "number" for strto[u]l[l]! |
| 83 | * It returns zero and errno is 0! :( */ | 83 | * It returns zero and errno is 0! :( */ |
| 84 | first = (arg[0] != '-' ? arg[0] : arg[1]); | 84 | first = (arg[0] != '-' && arg[0] != '+' ? arg[0] : arg[1]); |
| 85 | if (!isalnum(first)) return ret_ERANGE(); | 85 | if (!isalnum(first)) return ret_ERANGE(); |
| 86 | 86 | ||
| 87 | errno = 0; | 87 | errno = 0; |
diff --git a/testsuite/printf.tests b/testsuite/printf.tests index 963ded94b..34a65926e 100755 --- a/testsuite/printf.tests +++ b/testsuite/printf.tests | |||
| @@ -79,6 +79,39 @@ testing "printf understands %Ld" \ | |||
| 79 | "-5\n""0\n" \ | 79 | "-5\n""0\n" \ |
| 80 | "" "" | 80 | "" "" |
| 81 | 81 | ||
| 82 | testing "printf handles positive numbers for %d" \ | ||
| 83 | "${bb}printf '%d\n' 3 +3 ' 3' ' +3' 2>&1; echo \$?" \ | ||
| 84 | "3\n"\ | ||
| 85 | "3\n"\ | ||
| 86 | "3\n"\ | ||
| 87 | "3\n""0\n" \ | ||
| 88 | "" "" | ||
| 89 | |||
| 90 | testing "printf handles positive numbers for %i" \ | ||
| 91 | "${bb}printf '%i\n' 3 +3 ' 3' ' +3' 2>&1; echo \$?" \ | ||
| 92 | "3\n"\ | ||
| 93 | "3\n"\ | ||
| 94 | "3\n"\ | ||
| 95 | "3\n""0\n" \ | ||
| 96 | "" "" | ||
| 97 | |||
| 98 | testing "printf handles positive numbers for %x" \ | ||
| 99 | "${bb}printf '%x\n' 42 +42 ' 42' ' +42' 2>&1; echo \$?" \ | ||
| 100 | "2a\n"\ | ||
| 101 | "2a\n"\ | ||
| 102 | "2a\n"\ | ||
| 103 | "2a\n""0\n" \ | ||
| 104 | "" "" | ||
| 105 | |||
| 106 | testing "printf handles positive numbers for %f" \ | ||
| 107 | "${bb}printf '%0.3f\n' .42 +.42 ' .42' ' +.42' 2>&1; echo \$?" \ | ||
| 108 | "0.420\n"\ | ||
| 109 | "0.420\n"\ | ||
| 110 | "0.420\n"\ | ||
| 111 | "0.420\n""0\n" \ | ||
| 112 | "" "" | ||
| 113 | |||
| 114 | |||
| 82 | # "FIXED" now to act compatibly | 115 | # "FIXED" now to act compatibly |
| 83 | ## We are "more correct" here than bash/coreutils: they happily print -2 | 116 | ## We are "more correct" here than bash/coreutils: they happily print -2 |
| 84 | ## as if it is a huge unsigned number | 117 | ## as if it is a huge unsigned number |
