diff options
author | Denys Vlasenko | 2023-10-02 13:56:32 +0200 |
---|---|---|
committer | Denys Vlasenko | 2023-10-02 13:56:32 +0200 |
commit | 791b222dd55d3aa0e8b09be1be571e4829465dd6 (patch) | |
tree | 64af8d2c7a5787055bacb15c8a9f20ed51c2732f | |
parent | 2cc9d436e80632157b99e18d413a62b2d44d321a (diff) | |
download | busybox-791b222dd55d3aa0e8b09be1be571e4829465dd6.zip busybox-791b222dd55d3aa0e8b09be1be571e4829465dd6.tar.gz |
sleep: fix "sleep -- ARGS"
function old new delta
sleep_main 116 119 +3
printf_main 860 837 -23
single_argv 50 25 -25
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/2 up/down: 3/-48) Total: -45 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | coreutils/printf.c | 10 | ||||
-rw-r--r-- | coreutils/sleep.c | 4 | ||||
-rw-r--r-- | include/libbb.h | 1 | ||||
-rw-r--r-- | libbb/single_argv.c | 15 | ||||
-rw-r--r-- | shell/hush.c | 8 |
5 files changed, 19 insertions, 19 deletions
diff --git a/coreutils/printf.c b/coreutils/printf.c index 7763d7c..4edcfa9 100644 --- a/coreutils/printf.c +++ b/coreutils/printf.c @@ -425,9 +425,9 @@ int printf_main(int argc UNUSED_PARAM, char **argv) /* bash builtin errors out on "printf '-%s-\n' foo", * coreutils-6.9 works. Both work with "printf -- '-%s-\n' foo". * We will mimic coreutils. */ - if (argv[1] && argv[1][0] == '-' && argv[1][1] == '-' && !argv[1][2]) - argv++; - if (!argv[1]) { + argv = skip_dash_dash(argv); + + if (!argv[0]) { if ((ENABLE_ASH_PRINTF || ENABLE_HUSH_PRINTF) && applet_name[0] != 'p' ) { @@ -437,8 +437,8 @@ int printf_main(int argc UNUSED_PARAM, char **argv) bb_show_usage(); } - format = argv[1]; - argv2 = argv + 2; + format = argv[0]; + argv2 = argv + 1; conv_err = 0; do { diff --git a/coreutils/sleep.c b/coreutils/sleep.c index 6edff59..fa74f1f 100644 --- a/coreutils/sleep.c +++ b/coreutils/sleep.c @@ -71,8 +71,8 @@ int sleep_main(int argc UNUSED_PARAM, char **argv) * + we can't use bb_show_usage * + applet_name can be the name of the shell */ - ++argv; - if (!*argv) { + argv = skip_dash_dash(argv); + if (!argv[0]) { /* Without this, bare "sleep" in ash shows _ash_ --help */ /* (ash can be the "sh" applet as well, so check 2nd char) */ if (ENABLE_ASH_SLEEP && applet_name[1] != 'l') { diff --git a/include/libbb.h b/include/libbb.h index eb97a98..0883fb5 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1344,6 +1344,7 @@ int sanitize_env_if_suid(void) FAST_FUNC; /* For top, ps. Some argv[i] are replaced by malloced "-opt" strings */ void make_all_argv_opts(char **argv) FAST_FUNC; char* single_argv(char **argv) FAST_FUNC; +char **skip_dash_dash(char **argv) FAST_FUNC; extern const char *const bb_argv_dash[]; /* { "-", NULL } */ extern uint32_t option_mask32; uint32_t getopt32(char **argv, const char *applet_opts, ...) FAST_FUNC; diff --git a/libbb/single_argv.c b/libbb/single_argv.c index 64844dd..594cb0d 100644 --- a/libbb/single_argv.c +++ b/libbb/single_argv.c @@ -8,11 +8,18 @@ */ #include "libbb.h" -char* FAST_FUNC single_argv(char **argv) +char** FAST_FUNC skip_dash_dash(char **argv) { - if (argv[1] && strcmp(argv[1], "--") == 0) + argv++; + if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0') argv++; - if (!argv[1] || argv[2]) + return argv; +} + +char* FAST_FUNC single_argv(char **argv) +{ + argv = skip_dash_dash(argv); + if (!argv[0] || argv[1]) bb_show_usage(); - return argv[1]; + return argv[0]; } diff --git a/shell/hush.c b/shell/hush.c index 8e632e0..ca01e2b 100644 --- a/shell/hush.c +++ b/shell/hush.c @@ -10883,14 +10883,6 @@ static int FAST_FUNC builtin_history(char **argv UNUSED_PARAM) } #endif -static char **skip_dash_dash(char **argv) -{ - argv++; - if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0') - argv++; - return argv; -} - static int FAST_FUNC builtin_cd(char **argv) { const char *newdir; |