From 6de29a7e5346bea9c1c2ad4faf4010ee78f1a97b Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sun, 19 Mar 2023 10:24:43 +0000 Subject: runuser,drop: code shrink Make quote_arg() always return an allocated string so we can free it unconditionally. Always use argv[1] as the first part of the command string. Saves 48 bytes. --- util-linux/runuser.c | 20 +++++++------------- win32/process.c | 5 ++--- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/util-linux/runuser.c b/util-linux/runuser.c index 6b87c641c..993a4ed68 100644 --- a/util-linux/runuser.c +++ b/util-linux/runuser.c @@ -66,7 +66,7 @@ int runuser_main(int argc, char **argv) 0x00, 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x00 }; - char *cmd, **a; + char *cmd, *q, *newcmd, **a; DWORD code; // This shouldn't be necessary but without it the binary complains // it can't find CreateProcessAsUserA on older versions of Windows. @@ -113,13 +113,12 @@ int runuser_main(int argc, char **argv) #if ENABLE_FEATURE_PREFER_APPLETS if (!has_path(argv[1]) && find_applet_by_name(argv[1]) >= 0) { file = xstrdup(bb_busybox_exec_path); - cmd = argv[1]; } else #endif if (has_path(argv[1])) { - file = cmd = file_is_win32_exe(argv[1]); + file = file_is_win32_exe(argv[1]); } else { - file = cmd = find_first_executable(argv[1]); + file = find_first_executable(argv[1]); } if (file == NULL) { @@ -129,19 +128,14 @@ int runuser_main(int argc, char **argv) slash_to_bs(file); exe = file; - cmd = xstrdup(cmd); - file = quote_arg(cmd); - if (file != cmd) - free(cmd); - cmd = file; + cmd = quote_arg(argv[1]); } // Build the command line for (a = argv + 1 + (argc != 1); *a; ++a) { - char *q = quote_arg(*a); - char *newcmd = xasprintf("%s %s", cmd, q); - if (q != *a) - free(q); + q = quote_arg(*a); + newcmd = xasprintf("%s %s", cmd, q); + free(q); free(cmd); cmd = newcmd; } diff --git a/win32/process.c b/win32/process.c index 7db7741fd..428d6c703 100644 --- a/win32/process.c +++ b/win32/process.c @@ -163,7 +163,7 @@ quote_arg(const char *arg) } if (!force_quotes && n == 0) { - return (char*)arg; + return xstrdup(arg); } /* insert double quotes and backslashes where necessary */ @@ -275,8 +275,7 @@ spawnveq(int mode, const char *path, char *const *argv, char *const *env) done: for (i = 0;i < argc;i++) - if (new_argv[i] != argv[i]) - free(new_argv[i]); + free(new_argv[i]); free(new_argv); free(new_path); -- cgit v1.2.3-55-g6feb