aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2023-03-19 10:24:43 +0000
committerRon Yorston <rmy@pobox.com>2023-03-19 10:24:43 +0000
commit6de29a7e5346bea9c1c2ad4faf4010ee78f1a97b (patch)
tree22adeab617d92cf7daec18e1b067f6e41d8a2945
parentcb50586aedc9725fb14dbb25263bba1598726037 (diff)
downloadbusybox-w32-6de29a7e5346bea9c1c2ad4faf4010ee78f1a97b.tar.gz
busybox-w32-6de29a7e5346bea9c1c2ad4faf4010ee78f1a97b.tar.bz2
busybox-w32-6de29a7e5346bea9c1c2ad4faf4010ee78f1a97b.zip
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.
-rw-r--r--util-linux/runuser.c20
-rw-r--r--win32/process.c5
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)
66 0x00, 0x00, 0x00, 0x10, 66 0x00, 0x00, 0x00, 0x10,
67 0x00, 0x20, 0x00, 0x00 67 0x00, 0x20, 0x00, 0x00
68 }; 68 };
69 char *cmd, **a; 69 char *cmd, *q, *newcmd, **a;
70 DWORD code; 70 DWORD code;
71 // This shouldn't be necessary but without it the binary complains 71 // This shouldn't be necessary but without it the binary complains
72 // it can't find CreateProcessAsUserA on older versions of Windows. 72 // it can't find CreateProcessAsUserA on older versions of Windows.
@@ -113,13 +113,12 @@ int runuser_main(int argc, char **argv)
113#if ENABLE_FEATURE_PREFER_APPLETS 113#if ENABLE_FEATURE_PREFER_APPLETS
114 if (!has_path(argv[1]) && find_applet_by_name(argv[1]) >= 0) { 114 if (!has_path(argv[1]) && find_applet_by_name(argv[1]) >= 0) {
115 file = xstrdup(bb_busybox_exec_path); 115 file = xstrdup(bb_busybox_exec_path);
116 cmd = argv[1];
117 } else 116 } else
118#endif 117#endif
119 if (has_path(argv[1])) { 118 if (has_path(argv[1])) {
120 file = cmd = file_is_win32_exe(argv[1]); 119 file = file_is_win32_exe(argv[1]);
121 } else { 120 } else {
122 file = cmd = find_first_executable(argv[1]); 121 file = find_first_executable(argv[1]);
123 } 122 }
124 123
125 if (file == NULL) { 124 if (file == NULL) {
@@ -129,19 +128,14 @@ int runuser_main(int argc, char **argv)
129 128
130 slash_to_bs(file); 129 slash_to_bs(file);
131 exe = file; 130 exe = file;
132 cmd = xstrdup(cmd); 131 cmd = quote_arg(argv[1]);
133 file = quote_arg(cmd);
134 if (file != cmd)
135 free(cmd);
136 cmd = file;
137 } 132 }
138 133
139 // Build the command line 134 // Build the command line
140 for (a = argv + 1 + (argc != 1); *a; ++a) { 135 for (a = argv + 1 + (argc != 1); *a; ++a) {
141 char *q = quote_arg(*a); 136 q = quote_arg(*a);
142 char *newcmd = xasprintf("%s %s", cmd, q); 137 newcmd = xasprintf("%s %s", cmd, q);
143 if (q != *a) 138 free(q);
144 free(q);
145 free(cmd); 139 free(cmd);
146 cmd = newcmd; 140 cmd = newcmd;
147 } 141 }
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)
163 } 163 }
164 164
165 if (!force_quotes && n == 0) { 165 if (!force_quotes && n == 0) {
166 return (char*)arg; 166 return xstrdup(arg);
167 } 167 }
168 168
169 /* insert double quotes and backslashes where necessary */ 169 /* insert double quotes and backslashes where necessary */
@@ -275,8 +275,7 @@ spawnveq(int mode, const char *path, char *const *argv, char *const *env)
275 275
276 done: 276 done:
277 for (i = 0;i < argc;i++) 277 for (i = 0;i < argc;i++)
278 if (new_argv[i] != argv[i]) 278 free(new_argv[i]);
279 free(new_argv[i]);
280 free(new_argv); 279 free(new_argv);
281 free(new_path); 280 free(new_path);
282 281