Commit graph

17871 commits

Author SHA1 Message Date
Denys Vlasenko
ba9651b803 cut: we can't get empty cut_list[], remove the check for that
function                                             old     new   delta
.rodata                                           105685  105659     -26

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-16 00:19:43 +01:00
Denys Vlasenko
e8622f0d85 cut: disallow -f '' and -f '-'
function                                             old     new   delta
cut_main                                            1391    1410     +19

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-15 12:27:33 +01:00
Denys Vlasenko
b25ea3f156 cut: fix -F n-m to match toybox
function                                             old     new   delta
cut_main                                            1339    1391     +52

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-14 13:58:49 +01:00
Denys Vlasenko
d48400d8fb cut: simplify getopt32 code
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-13 20:35:28 +01:00
Denys Vlasenko
7624077772 cut: "it's legal to pass an empty list" seems to be untrue
function                                             old     new   delta
cut_main                                            1344    1339      -5

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-13 20:22:05 +01:00
Denys Vlasenko
f4f8dc6864 cut: whitespace fixes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-13 19:43:53 +01:00
Denys Vlasenko
ed6561685c cut: tweak comments
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-13 19:39:34 +01:00
Denys Vlasenko
96a230f17b cut: fix handling of -d ''
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-13 19:31:41 +01:00
Denys Vlasenko
9adafbc118 cut: prevent infinite loop if -F REGEX matches empty delimiter
function                                             old     new   delta
cut_main                                            1339    1348      +9

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-13 19:13:20 +01:00
Denys Vlasenko
a4894eaf71 cut: tweak --help
function                                             old     new   delta
packed_usage                                       34901   34934     +33
cut_main                                            1353    1339     -14
.rodata                                           105724  105685     -39
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/2 up/down: 33/-53)            Total: -20 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-11 01:10:05 +01:00
Denys Vlasenko
9e364b16d1 cut: fix -d$'\n' --output-delimiter=@@ behavior
function                                             old     new   delta
cut_main                                            1261    1353     +92
packed_usage                                       34925   34901     -24
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 92/-24)             Total: 68 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-10 21:01:52 +01:00
Denys Vlasenko
a208fa03de cut: implement --output-delimiter
function                                             old     new   delta
cut_main                                            1204    1261     +57
static.cut_longopts                                    -      20     +20
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/0 up/down: 77/0)               Total: 77 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-10 18:49:10 +01:00
Denys Vlasenko
b402b13fab cut: with -F disabled in .config, don't show it in --help
function                                             old     new   delta
packed_usage                                       34897   34849     -48

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-10 18:09:07 +01:00
Denys Vlasenko
38a3c0ad32 cut: comment out code which seems to be not needed
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-10 17:50:29 +01:00
Denys Vlasenko
103139d0e6 cut: fix "echo 1.2 | cut -d. -f1,3" (print "1", not "1.")
function                                             old     new   delta
cut_main                                            1228    1201     -27

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-10 17:21:30 +01:00
Denys Vlasenko
470f009552 cut: with -F, do not regcomp() pattern for every file
function                                             old     new   delta
cut_main                                            1218    1228     +10

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-10 02:55:58 +01:00
Denys Vlasenko
55fc6a18da cut: rename "cut_lists" to "cut_list"
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-10 02:29:48 +01:00
Denys Vlasenko
1220b1519d cut: rename some variables to hopefully better names
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-10 02:15:25 +01:00
Denys Vlasenko
808155ebf3 cut: "orig_line" is redundant, remove it
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-10 01:55:25 +01:00
Denys Vlasenko
478ac90f2c cut: allocate "printed" only if OPT_CHAR or OPT_BYTE
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-10 01:50:58 +01:00
Denys Vlasenko
ad12ab439b cut: localize 'spos' variable, convert !NUMVAR to NUMVAR == 0
This imporves readability

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-10 01:44:13 +01:00
Denys Vlasenko
73e9d25d75 cut: simplify OPT_ names, eliminate one variable
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-10 01:38:31 +01:00
Ron Yorston
f020414413 cut: improve detection of invalid ranges
Commit 0068ce2fa (cut: add toybox-compatible options -O OUTSEP,
-D, -F LIST) added detection of reversed ranges.  Further
improvements are possible.

- The test for reversed ranges compared the start after it had been
  decremented with the end before decrement.  It thus missed ranges
  of the form 2-1.

- Zero isn't a valid start value for a range.  (Nor is it a valid
  end value, but that's caught by the test for a reversed range.)

- The code

        if (!*ltok)
            e = INT_MAX;

  duplicates a check that's already been made.

- Display the actual range in the error message to make it easier
  to find which range was at fault.

function                                             old     new   delta
.rodata                                           100273  100287     +14
cut_main                                            1239    1237      -2
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 14/-2)              Total: 12 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-10 01:29:44 +01:00
Colin McAllister
50e2b59370 cut: fix "-s" flag to omit blank lines
Using cut with the delimiter flag ("-d") with the "-s" flag to only
output lines containing the delimiter will print blank lines. This is
deviant behavior from cut provided by GNU Coreutils. Blank lines should
be omitted if "-s" is used with "-d".

This change introduces a somewhat naiive, yet efficient solution, where
line length is checked before looping though bytes. If line length is
zero and the "-s" flag is used, the code will jump to parsing the next
line to avoid printing a newline character.

function                                             old     new   delta
cut_main                                            1196    1185     -11

Signed-off-by: Colin McAllister <colinmca242@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-10 01:27:04 +01:00
Natanael Copa
87e60dcf0f hexdump: fix regression with -n4 -e '"%u"'
Fix bug introduced in busybox 1.37.0 that broke kernel builds.

Fixes commit e2287f99fe (od: for !DESKTOP, match output more closely
to GNU coreutils 9.1, implement -s)

function                                             old     new   delta
rewrite                                              967     976      +9

Signed-off-by: Natanael Copa <ncopa@alpinelinux.org>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-09 11:17:14 +01:00
Ron Yorston
175b8dda19 libbb: tidy argument checks in getopt32()
When getopt32() has complementary options it's possible to specify
the minimum and maximum number of arguments allowed.  Checking
these values was inconsistent:

- '?' correctly checked that it was followed by a digit but set
  the otherwise unused spec_flgs variable on error.

- '=' failed to check that it was followed by a digit.

function                                             old     new   delta
vgetopt32                                           1307    1319     +12

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-09 02:16:44 +01:00
Christian Franke
e6f3a3b381 chpst: fix error check of nice(2) call
Check errno instead of return value because -1 is a valid return
value also on success.

Signed-off-by: Christian Franke <christian.franke@t-online.de>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-09 02:08:52 +01:00
Hajime Tazaki
23db689683 hush: fix G.argv0_for_re_execing to avoid endless loop
When the busybox is used as /sbin/init and the inittab file contains
below:

::respawn:-/bin/sh

/sbin/init spawns hush for the first time with the argv[0] contains '-',
and hush treats it as login shell.  Then it reads /etc/profile and if
the file contains the command execution like below, it invokes hush as
login shell because the argv[0] argument is still '-/bin/sh' and reads
/etc/profile again.  This will last until some failure (e.g., memory
failure) happens.

  [ "$(id -u)" -eq 0 ] && PS1="${PS1}# " || PS1="${PS1}\$ "

This commit fixes this issues by adding an offset (+1) to the
G.argv0_for_re_execing variable.

This issue happens on our out-of-tree UML (use mode linux) with nommu
configuration.

Link: https://lore.kernel.org/all/cover.1731290567.git.thehajime@gmail.com/
Signed-off-by: Hajime Tazaki <thehajime@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-12-09 01:58:45 +01:00
Denys Vlasenko
75ca8d074b hexdump: accept hex numbers in -n, closes 16195
function                                             old     new   delta
hexdump_main                                         366     383     +17

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-10-08 04:23:29 +02:00
Denys Vlasenko
49d9e06fba libbb: modify find_executable() to not temporarily write to PATH
This allows to simplify "which" applet code

function                                             old     new   delta
find_executable                                       93     111     +18
which_main                                           191     177     -14
builtin_source                                       316     294     -22
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/2 up/down: 18/-36)            Total: -18 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-10-08 04:03:17 +02:00
Denys Vlasenko
8c4bccb83e hush: fix "type ./cat" and "command -v ./cat" to not scan PATH
function                                             old     new   delta
find_executable_in_PATH                                -      67     +67
if_command_vV_print_and_exit                         114     116      +2
.rodata                                           105712  105710      -2
builtin_type                                         137     128      -9
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/2 up/down: 69/-11)             Total: 58 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-10-07 22:00:16 +02:00
Denys Vlasenko
baa49bdc1b hush: make "test -x" use cached groupinfo
While at it, correct "type" to skip non-executable files in PATH

function                                             old     new   delta
builtin_source                                       211     316    +105
builtin_test                                          10      32     +22
hush_main                                           1150    1170     +20
builtin_type                                         122     137     +15
if_command_vV_print_and_exit                         120     114      -6
find_in_path                                         131       -    -131
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 4/1 up/down: 162/-137)           Total: 25 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-10-07 15:18:45 +02:00
Denys Vlasenko
0929a129fc test: -x can return 0/1 early if all X bits are the same
function                                             old     new   delta
nexpr                                                702     725     +23

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-10-07 07:33:58 +02:00
Denys Vlasenko
96b0607302 ash: cache more of uid/gid syscalls
Testcase:
setuidgid 1:1 strace ash -c 'test -x TODO; test -x TODO; echo $?'
should show that second "test -x" does not query ids again.

function                                             old     new   delta
ash_main                                            1236    1256     +20
get_cached_euid                                        -      19     +19
get_cached_egid                                        -      19     +19
test_main                                             56      72     +16
test_exec                                            119     135     +16
is_in_supplementary_groups                            52      57      +5
nexpr                                                718     702     -16
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 4/1 up/down: 95/-16)             Total: 79 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-10-07 07:28:44 +02:00
Denys Vlasenko
d26e958725 ash: make "test -x" use cached groupinfo
function                                             old     new   delta
test_main2                                             -     407    +407
testcmd                                               10      23     +13
test_main                                            418      56    -362
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/1 up/down: 420/-362)           Total: 58 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-10-07 07:14:27 +02:00
Denys Vlasenko
4c1d645c86 libbb: simplify parameter passing in is_in_supplementary_groups()
function                                             old     new   delta
is_in_supplementary_groups                            54      52      -2
nexpr                                                721     718      -3
test_exec                                            125     119      -6
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-11)             Total: -11 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-10-07 06:36:00 +02:00
Denys Vlasenko
860b3d066f ash: command -v CMD must skip (go to next path) when CMD exists, but is not executable
Upstream commit:

    Date: Fri, 5 Apr 2024 17:55:46 +0800
    exec: Check executable bit when searching path

    Andrej Shadura <andrew.shadura@collabora.co.uk> wrote:
    ...
    > https://bugs.debian.org/874264
    > -------- Forwarded Message --------
    > Subject: dash: 'command -v' mistakenly returns a shell script whose
    > executable is not set
    > Date: Mon, 04 Sep 2017 10:45:48 -0400
    > From: Norman Ramsey <nr@cs.tufts.edu>
    > To: Debian Bug Tracking System <submit@bugs.debian.org>
    ...
    > I tracked a build bug in s-nail to a problem with dash.  Symptom:
    > building s-nail tries to run /home/nr/bin/clang, a script whose
    > executable bit is not set.  We tracked the problem to the result of
    > running `command -v clang` with /bin/sh:
    ...
    This is inherited from NetBSD.  There is even a commented-out
    block of code that tried to fix this.

    Anyway, we now have faccessat so we can simply use it.

function                                             old     new   delta
test_exec                                              -     125    +125
find_command                                         911     918      +7
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/0 up/down: 132/0)             Total: 132 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-10-07 05:46:31 +02:00
Denys Vlasenko
748b168154 libbb: move is_in_supplementary_groups() from test to libbb
function                                             old     new   delta
is_in_supplementary_groups                             -      54     +54
nexpr                                                766     721     -45
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/1 up/down: 54/-45)              Total: 9 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-10-07 01:20:43 +02:00
Denys Vlasenko
bb5525613e test: Invert return value of test_eaccess and rename it to test_st_mode
From dash:

    From: herbert <herbert@gondor.apana.org.au>
    Date: Wed, 2 Mar 2005 22:14:54 +1100
    Invert return value of test_eaccess and rename it to test_st_mode.

function                                             old     new   delta
nexpr                                                800     766     -34

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-10-06 19:25:07 +02:00
Denys Vlasenko
222802e833 test: code shrink
function                                             old     new   delta
nexpr                                                813     800     -13

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-10-06 19:08:10 +02:00
Thomas Devoogdt
8953429a82 networking/libiproute/iplink.c: fix support for older kernels
- The CAN netlink interface has been added in Linux v2.6.31 with only 3 options [1]:

  CAN_CTRLMODE_LOOPBACK    0x1  /* Loopback mode */
  CAN_CTRLMODE_LISTENONLY  0x2  /* Listen-only mode */
  CAN_CTRLMODE_3_SAMPLES   0x4  /* Triple sampling mode */

  So define the other options.

- IFLA_CAN_TERMINATION has been added in Linux 4.11 [2],
  define it for older kernels.

[1] https://github.com/torvalds/linux/blob/v2.6.31/include/linux/can/netlink.h#L80-L82
[2] 12a6075cab

Signed-off-by: Thomas Devoogdt <thomas@devoogdt.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-10-06 18:25:01 +02:00
Rudi Heitbaum
bf57f732a5 libbb: fix sha1 on !x86 if CONFIG_SHA1_HWACCEL=y
fixes non i386 and x86 builds

libbb/hash_md5_sha.c: In function 'sha1_end':
libbb/hash_md5_sha.c:1316:35: error: 'sha1_process_block64_shaNI' undeclared
 1316 |          || ctx->process_block == sha1_process_block64_shaNI

Signed-off-by: Rudi Heitbaum <rudi@heitbaum.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-10-06 17:56:42 +02:00
Denys Vlasenko
dff444bc37 hush: whitespace and comment fixes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-09-28 03:52:44 +02:00
Ron Yorston
24aa93d538 ash: reject unknown long options
Commit 64f70cc755 (Add --login support) added code in options()
to handle the bash-compatible '--login' option.  In doing so it
committed BusyBox ash to silently accepting all other long
options.

Restore compatibility with other ash variants by rejecting unknown
long options.

function                                             old     new   delta
options                                              589     624     +35

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-09-27 21:29:05 +02:00
Ron Yorston
39c5c08b28 lineedit: use stdout for shell history builtin
Commit fd47f0567 (lineedit: print prompt and editing operations
to stderr) changed various print statements to output to stderr.
The change in show_history() caused the shell history builtin to
send its output to stderr.

Revert that part of the commit.

function                                             old     new   delta
show_history                                          47      42      -5
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-5)               Total: -5 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-09-27 20:16:23 +02:00
Ron Yorston
26895db35d ed: fix line insertion before current line. Closes 15081
When text is inserted by insertLine() the lines following the
insertion are moved down and the insertion point is made the new
current line.  To avoid too much scanning of the linked list of
lines setCurNum() may use the position of the old current line to
determine the location of the new current line.

If the insertion point is before the old current line in the file
the latter will have been moved down, so its line pointer needs to
be adjusted.

function                                             old     new   delta
insertLine                                           162     180     +18
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 18/0)               Total: 18 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-09-27 20:14:04 +02:00
Sam James
480a07bd68 fixdep: add fstat error handling
When `fstat` fails, `st` is left uninitialised. In our case, Ben Kohler
noticed our release media builds were failing in Gentoo on x86 when building
busybox with occasional SIGBUS. This turned out to be EOVERFLOW (from 32-bit
ino_t) which wasn't being reported because nothing was checking the return value
from `fstat`.

Fix that to avoid UB (use of uninit var) and to give a more friendly
error to the user.

This actually turns out to be fixed already in the kernel from back in
2010 [0] and 2016 [1].

[0] a3ba81131a
[1] 46fe94ad18

Reported-by: Ben Kohler <bkohler@gentoo.org>
Signed-off-by: Sam James <sam@gentoo.org>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-09-27 20:03:30 +02:00
Ron Yorston
dbd14c4a42 libbb: send usage messages to correct stream
POSIX generally requires normal output to go to stdout and
diagnostic (i.e. error) output to go to stderr.

When usage messages for BusyBox applets are specifically requested
by the user (e.g. 'find --help') they should go to stdout; when
they're emitted due to an error they should go to stderr.

function                                             old     new   delta
bb_show_usage                                        148     146      -2
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-2)               Total: -2 bytes

Signed-off-by: Avi Halachmi <avihpit@yahoo.com>
Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-09-27 19:52:25 +02:00
Ron Yorston
2c232f1e60 libbb: use full_write1_str() to shrink busybox_main()
There are two calls to dup2() in busybox_main().  These were
introduced to coerce full_write2_str() into writing to stdout.

The relevant commits were: 21278dff7 (busybox: do not print help
to fd 2, print it to fd 1) and 5a7c72015 (busybox --list option.
+140 bytes. Rob wanted it.)

Later, in commit 729ecb87b (bbconfig: make it independent from
printf functions), the function full_write1_str() was added.

Using this in busybox_main() allows us to drop the dup2() calls.

function                                             old     new   delta
run_applet_and_exit                                  796     760     -36
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-36)             Total: -36 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-09-27 19:39:28 +02:00
Denys Vlasenko
e5b0547ac8 lineedit: make save_history() FAST_FUNC
function                                             old     new   delta
save_history                                         267     266      -1
hush_exit                                             98      97      -1
exitshell                                            140     138      -2
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-4)               Total: -4 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2024-09-27 19:35:50 +02:00