Commit Graph

624 Commits

Author SHA1 Message Date
Denys Vlasenko
e2952dfaff awk: input numbers are never octal or hex (only program consts can be)
function                                             old     new   delta
next_token                                           825     930    +105
getvar_i                                             114     129     +15
nextchar                                              49      53      +4
my_strtod                                            138       -    -138
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 3/0 up/down: 124/-138)          Total: -14 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2022-01-08 22:42:35 +01:00
Denys Vlasenko
34e0bb3931 sort: support -h
function                                             old     new   delta
static.scale_suffix                                    -      62     +62
.rodata                                           104304  104336     +32
compare_keys                                         820     848     +28
packed_usage                                       34159   34184     +25
static.suffix                                          -       9      +9
sort_opt_str                                          37      38      +1
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 4/0 up/down: 157/0)             Total: 157 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2022-01-05 12:05:55 +01:00
Denys Vlasenko
cc7d2e2178 sort: fix -s -r interaction: 'stable' order is not affected by -r
function                                             old     new   delta
compare_keys                                         818     820      +2

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2022-01-04 23:53:21 +01:00
Denys Vlasenko
286b33721d sed: correctly handle 'w FILE' commands writing to the same file
function                                             old     new   delta
sed_xfopen_w                                           -      84     +84

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2022-01-04 19:42:36 +01:00
Dominique Martinet
4fe954c148 sed: do not ignore 'g' modifier when match starts with ^
It is perfectly valid to start a regex with ^ and have other patterns
with \| that can match more than once, e.g. the following example
should print ca, as illustrated with gnu sed:
$ echo 'abca' | sed -e 's/^a\|b//g'
ca

busybox before patch:
$ echo 'abca' | busybox sed -e 's/^a\|b//g'
bca

busybox after patch:
$ echo 'abca' | ./busybox sed -e 's/^a\|b//g'
ca

regcomp handles ^ perfectly well as illustrated with the second 'a' that
did not match in the example, we ca leave the non-repeating to it if
appropriate.
The check had been added before using regcomp and was required at the
time (f36635cec6) but no longer makes sense now.

(tested with glibc and musl libc)

function                                             old     new   delta
add_cmd                                             1189    1176     -13

Signed-off-by: Dominique Martinet <asmadeus@codewreck.org>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-12-25 04:20:28 +01:00
Ron Yorston
7105e4afdd printf: allow 0 as a flag and allow multiple flags
The '%' character in a format specification may be followed by
one or more flags from the list "+- #0".  BusyBox printf didn't
support the '0' flag or allow multiple flags to be provided.
As a result the formats '%0*d' and '%0 d' were considered to be
invalid.

The lack of support for '0' was pointed out by Andrew Snyder on the
musl mailing list:

   https://www.openwall.com/lists/musl/2021/12/14/2

function                                             old     new   delta
printf_main                                          860     891     +31
.rodata                                            99281   99282      +1
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 32/0)               Total: 32 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-12-17 21:13:26 +01:00
Denys Vlasenko
00b5051cd2 libbb: code shrink in des encryption, in setup_salt()
function                                             old     new   delta
pw_encrypt                                           978     971      -7
.rodata                                           108208  108192     -16
des_crypt                                           1211    1181     -30
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-53)             Total: -53 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-11-28 12:21:23 +01:00
Ron Yorston
94eb1c4dc6 libbb: better coreutils compatibility for realpath
Add some tests which coreutils realpath pass but BusyBox realpath
fails (bar one).  Adjust xmalloc_realpath_coreutils() so the tests
pass:

- Expand symbolic links before testing whether the last path component
  exists.

- When the link target is a relative path canonicalize it by passing
  it through xmalloc_realpath_coreutils() as already happens for
  absolute paths.

- Ignore trailing slashes when finding the last path component and
  correctly handle the case where the only slash is at the start of
  the path.  This requires ignoring superfluous leading slashes.

- Undo all changes to the path so error messages from the caller show
  the original filename.

function                                             old     new   delta
xmalloc_realpath_coreutils                           214     313     +99

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-10-09 01:47:12 +02:00
Denys Vlasenko
86ba007b84 xxd: fix -p -r, closes 13881
function                                             old     new   delta
xxd_main                                             893     890      -3

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-10-08 23:03:54 +02:00
Ron Yorston
9fe1548bbf date,touch: allow timezone offsets in dates
Allow ISO 8601 style dates to include a timezone offset.  Like
the '@' format these dates aren't relative to the user's current
timezone and shouldn't be subject to DST adjustment.

- The implementation uses the strptime() '%z' format specifier.
  This an extension which may not be available so the use of
  timezones is a configuration option.

- The 'touch' applet has been updated to respect whether DST
  adjustment is required, matching 'date'.

function                                             old     new   delta
parse_datestr                                        624     730    +106
static.fmt_str                                       106     136     +30
touch_main                                           388     392      +4
date_main                                            818     819      +1
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 4/0 up/down: 141/0)             Total: 141 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-09-17 00:11:30 +02:00
Daniel Thau
7d06d6e186 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>
2021-09-05 03:42:51 +02:00
Ron Yorston
4357569fdc rev: correct output for long input lines
The input buffer is initialised to a reasonable size and extended
if necessary.  When this happened the offset into the buffer wasn't
reset to zero so subsequent lines were appended to the long line.

Fix this and add some tests.

function                                             old     new   delta
rev_main                                             377     368      -9
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-9)               Total: -9 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-08-22 00:08:18 +02:00
Denys Vlasenko
8e8cea2a1b awk: fix testsuite
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-08-16 07:37:22 +02:00
Denys Vlasenko
8d718686f7 tar: fix testsuite
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-08-16 00:48:50 +02:00
Denys Vlasenko
b259415b51 dd: fix testsuite
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-08-15 23:01:33 +02:00
Denys Vlasenko
eaa8ee40aa cut: fix testsuite
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-08-15 20:15:42 +02:00
Rob Landley
0068ce2fa0 cut: add toybox-compatible options -O OUTSEP, -D, -F LIST
function                                             old     new   delta
cut_main                                             884    1201    +317
packed_usage                                       33823   33885     +62
.rodata                                           104186  104179      -7
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/1 up/down: 379/-7)            Total: 372 bytes

Signed-off-by: Rob Landley <rob@landley.net>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-07-20 16:29:22 +02:00
Denys Vlasenko
d3480dd582 awk: disallow break/continue outside of loops
function                                             old     new   delta
.rodata                                           104139  104186     +47
chain_group                                          610     633     +23
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 70/0)               Total: 70 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-07-14 16:32:19 +02:00
Denys Vlasenko
8d269ef859 awk: fix printf "%-10c", 0
function                                             old     new   delta
awk_printf                                           596     626     +30

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-07-12 11:27:11 +02:00
Denys Vlasenko
39aabfe8f0 awk: unbreak "cmd" | getline
function                                             old     new   delta
evaluate                                            3337    3343      +6

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-07-11 12:51:43 +02:00
Denys Vlasenko
3d57a84907 awk: undo TI_PRINT, it introduced a bug (print with any redirect acting as printf)
function                                             old     new   delta
evaluate                                            3329    3337      +8

Patch by Ron Yorston <rmy@pobox.com>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-07-11 12:00:31 +02:00
Denys Vlasenko
4d902ea9de awk: fix beavior of "exit" without parameter
function                                             old     new   delta
evaluate                                            3336    3339      +3
awk_exit                                              93      94      +1
awk_main                                             829     827      -2
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/1 up/down: 4/-2)                Total: 2 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-07-02 22:28:51 +02:00
Denys Vlasenko
d7354df169 awk: evaluate all, even superfluous function args
function                                             old     new   delta
evaluate                                            3128    3135      +7

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-06-30 12:52:51 +02:00
Denys Vlasenko
d150710169 awk: allow empty fuinctions with no arguments, disallow function redefinitions
function                                             old     new   delta
.rodata                                           103681  103700     +19
parse_program                                        303     307      +4
evaluate                                            3145    3141      -4
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/1 up/down: 23/-4)              Total: 19 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-06-30 12:23:51 +02:00
Denys Vlasenko
6872c193a9 awk: fix parsing of expressions such as "v (a)"
function                                             old     new   delta
next_token                                           812     825     +13

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-06-29 12:16:36 +02:00
Ron Yorston
a84bbc5425 cp,mv: fix -t DIR option
When the '-t DIR' option is used the loop over the remaining
arguments should terminate when a NULL pointer is reached.

function                                             old     new   delta
mv_main                                              585     590      +5
cp_main                                              492     496      +4
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 9/0)                 Total: 9 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-06-25 00:23:29 +02:00
Denys Vlasenko
04f052c56d unlzma: fix a case where we could read before beginning of buffer
Testcase:

  21 01 01 00 00 00 00 00 e7 01 01 01 ef 00 df b6
  00 17 02 10 11 0f ff 00 16 00 00

Unfortunately, the bug is not reliably causing a segfault,
the behavior depends on what's in memory before the buffer.

function                                             old     new   delta
unpack_lzma_stream                                  2762    2768      +6

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-06-15 15:14:00 +02:00
Rafał Miłecki
4eb46e1be6 dd: support iflag=count_bytes
It allows passing amount of bytes in the count=

function                                             old     new   delta
packed_usage                                       33599   33617     +18
static.iflag_words                                    29      41     +12
dd_main                                             1601    1607      +6
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 36/0)               Total: 36 bytes

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-04-13 21:35:57 +02:00
Denys Vlasenko
307cd26e98 start-stop-daemon: explain -x + -a test
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-03-09 01:12:18 +01:00
Denys Vlasenko
ace81cd46c bc/dc: fix length(0) and length(0.000nnn) result
function                                             old     new   delta
zxc_vm_process                                      6464    6498     +34

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-02-26 14:23:13 +01:00
Ron Yorston
c849e72c0b nl: ensure '-b n' option displays file content
The command 'nl -b n' should output no line numbers, just some
spaces as a placeholder followed by the actual file content.

Add tests for line numbering by cat and nl.  The correct results
were obtained from coreutils.

function                                             old     new   delta
print_numbered_lines                                 152     157      +5
.rodata                                           182456  182453      -3
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 5/-3)                Total: 2 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-02-02 17:37:41 +01:00
Ron Yorston
e8fe9f9635 awk: allow printf('%c') to output NUL, closes 13486
Treat the output of printf as binary rather than a null-terminated
string so that NUL characters can be output.

This is considered to be a GNU extension, though it's also available
in mawk and FreeBSD's awk.

function                                             old     new   delta
evaluate                                            3487    3504     +17
awk_printf                                           504     519     +15
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 32/0)               Total: 32 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-02-02 13:51:14 +01:00
Denys Vlasenko
29a9043b36 bc,dc: make BC_LINE_LENGTH/DC_LINE_LENGTH more compatible with GNU
function                                             old     new   delta
xc_vm_init                                           640     682     +42

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-12-29 18:52:19 +01:00
Denys Vlasenko
77a51a2709 randomconfig fixes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-12-29 16:53:11 +01:00
Denys Vlasenko
6452c30036 factor: detect squares
If we have a square, the speedup can be extremely large
(in best case example below, it's ~40000 times faster):

$ time ./busybox_old factor 18446743988964486098
18446743988964486098: 2 3037000493 3037000493
real	0m4.246s
$ time ./busybox factor 18446743988964486098
18446743988964486098: 2 3037000493 3037000493
real	0m0.000s

function                                             old     new   delta
isqrt_odd                                              -      57     +57
print_w                                                -      36     +36
factorize                                            218     236     +18
print_h                                                -       7      +7
factorize_numstr                                      65      72      +7
------------------------------------------------------------------------------
(add/remove: 3/0 grow/shrink: 2/0 up/down: 125/0)             Total: 125 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-12-22 20:24:30 +01:00
Denys Vlasenko
758c2bd58e awk: add a test that $NF is empty
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-12-04 19:00:06 +01:00
Denys Vlasenko
665a659530 awk: FS regex matches only non-empty separators (gawk compat)
function                                             old     new   delta
awk_split                                            484     553     +69

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-12-02 19:07:31 +01:00
Denys Vlasenko
079487b487 hexdump: fix dup block comparison
function                                             old     new   delta
bb_dump_dump                                        1523    1520      -3

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-10-20 19:29:32 +02:00
Denys Vlasenko
dac5b83142 xxd: fix printing of trailing spaces
function                                             old     new   delta
bb_dump_dump                                        1497    1523     +26
xxd_main                                             459     466      +7
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 33/0)               Total: 33 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-10-20 18:57:02 +02:00
Denys Vlasenko
bd202a5ec1 xargs: fix -I SUBSTR behaviour
function                                             old     new   delta
process_stdin_with_replace                           165     204     +39

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-09-30 00:00:43 +02:00
Denys Vlasenko
a77f3ecf68 grep: for -L, exitcode 0 means files *without* matches were found, closes 13151
This is a recent change in GNU grep as well (after 3.1)

function                                             old     new   delta
grep_file                                           1215    1228     +13

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-08-15 00:39:30 +02:00
Denys Vlasenko
7c443d110d randomconfig fixes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-06-24 00:27:37 +02:00
Denys Vlasenko
5fa5c4bde8 randomconfig fixes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-06-23 21:28:19 +02:00
Denys Vlasenko
002be6e821 sysctl: do report EACCES errors on write
function                                             old     new   delta
sysctl_act_recursive                                 165     179     +14
sysctl_act_on_setting                                467     471      +4
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 18/0)               Total: 18 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-06-09 15:58:32 +02:00
Mark Edgar
b2ab920716 unexpand: correct behavior for --first-only --tabs=4
Prior to the patch, both -f and --first-only are in all cases either
no-op or ignored.
Without --tabs, --first-only is the default so specifying it is a no-op.
With --tabs, --all is implied, and --first-only is intended to reset this.

function                                             old     new   delta
expand_main                                          690     694      +4

Signed-off-by: Mark Edgar <medgar123@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-06-09 15:40:49 +02:00
Denys Vlasenko
6f7a009649 awk: disallow "str"++, closes bug 12981
function                                             old     new   delta
parse_expr                                           887     896      +9

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-06-09 01:33:54 +02:00
Sören Tempel
42a8984abc grep: add proper support for pattern_list
From POSIX.1-2008:

	The pattern_list's value shall consist of one or more patterns
	separated by <newline> characters;

As such, given patterns need to be split at newline characters. Without
doing so, busybox grep will interpret the newline as part of the pattern
which is not in accordance with POSIX.

See also: https://bugs.busybox.net/show_bug.cgi?id=12721

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-04-30 00:20:18 +02:00
Gray Wolf
c3295d233b grep: Fix -f FILE when FILE is empty and -x provided
Grep currently special-cased empty pattern file to be the same as
pattern file with one empty line (empty pattern). That does mirror how
GNU grep behaves, except when -x is provided. In that case .* pattern
needs to be used instead.

Signed-off-by: Gray Wolf <wolf@wolfsden.cz>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-04-29 16:01:42 +02:00
Ron Yorston
1c462d47a0 xargs: restore correct behaviour of -n option
Since commit 1ff7002b1 (xargs: fix handling of quoted arguments, closes
11441) the -n option hasn't worked properly:

   $ echo 1 2 3 | xargs -n 1 echo
   1
   2

   3

   $

Because state is now remembered between calls to process_stdin() it's
necessary to update the state before any premature return.

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-04-29 15:53:51 +02:00
Denys Vlasenko
bd8b05ba1b awk: fix more "length" cases, closes 12486
function                                             old     new   delta
next_token                                           808     831     +23

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-02 23:28:55 +01:00