From 9c20ed5246abbfe2fdae16a70b7fac1d0d1e51f0 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Fri, 21 Apr 2023 15:08:28 +0100 Subject: su: allow additional arguments after su -c The syntax becomes: su [-c CMD [ARG...]] This makes it a lot easier to use values from the current shell, instead of shell-quoting them for re-input and embedding at CMD. Also, previously -c expected an option-argument, and now it's a flag which requires a CMD operand, just like 'sh', so that both these forms now error the same way (as they should): sh -c -- su -c -- (previously su -c -- did not error because '--' was assumed CMD) Adds 64 bytes. (Based on GitHub PR #317) --- loginutils/suw32.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'loginutils/suw32.c') diff --git a/loginutils/suw32.c b/loginutils/suw32.c index df6d07232..46196d14c 100644 --- a/loginutils/suw32.c +++ b/loginutils/suw32.c @@ -16,10 +16,10 @@ //kbuild:lib-$(CONFIG_SUW32) += suw32.o //usage:#define suw32_trivial_usage -//usage: "[-c \"CMD\"]" +//usage: "[-c \"CMD\" [ARG...]]" //usage:#define suw32_full_usage "\n\n" //usage: "Run shell with elevated privileges\n" -//usage: "\n -c CMD Command to pass to 'sh -c'" +//usage: "\n -c CMD [ARG...] Command [args] to pass to 'sh -c'" #include "libbb.h" #include "lazyload.h" @@ -27,14 +27,15 @@ int suw32_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int suw32_main(int argc UNUSED_PARAM, char **argv) { - char *opt_command = NULL; SHELLEXECUTEINFO info; + unsigned opts, c_opt; char *bb_path, *cwd; DECLARE_PROC_ADDR(BOOL, ShellExecuteExA, SHELLEXECUTEINFOA *); - getopt32(argv, "c:", &opt_command); - if (argv[optind]) - bb_show_usage(); + opts = getopt32(argv, "c"); + c_opt = opts & 1; + if (!c_opt != !argv[optind]) + bb_show_usage(); // -c without CMD, or operand without -c /* ShellExecuteEx() needs backslash as separator in UNC paths. */ bb_path = xstrdup(bb_busybox_exec_path); @@ -59,9 +60,16 @@ int suw32_main(int argc UNUSED_PARAM, char **argv) cwd = xmalloc_realpath(getcwd(NULL, 0)); info.lpParameters = xasprintf("--busybox ash -d \"%s\" -t \"BusyBox ash (Admin)\" ", cwd); - if (opt_command) - info.lpParameters = - xasprintf("%s -s -c %s", info.lpParameters, quote_arg(opt_command)); + + if (c_opt) { + info.lpParameters = xappendword(info.lpParameters, "-s -c --"); + while (argv[optind]) { + char *a = quote_arg(argv[optind++]); + info.lpParameters = xappendword(info.lpParameters, a); + free(a); + } + } + /* info.lpDirectory = NULL; */ info.nShow = SW_SHOWNORMAL; -- cgit v1.2.3-55-g6feb