aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2023-04-28 14:05:59 +0100
committerRon Yorston <rmy@pobox.com>2023-04-28 14:05:59 +0100
commit5d417695bcd5af331acf69690312deff61b233d0 (patch)
tree71f632f40cc05e9ac1f3dea798c34c887b6a8bba
parente790046619f539356b950a5b07cba2f477401b4b (diff)
downloadbusybox-w32-5d417695bcd5af331acf69690312deff61b233d0.tar.gz
busybox-w32-5d417695bcd5af331acf69690312deff61b233d0.tar.bz2
busybox-w32-5d417695bcd5af331acf69690312deff61b233d0.zip
win32: code shrink copying of argv
There are two places where a copy of an argv array is made with extra space at the start. Move this code into a function. Saves 56-64 bytes.
-rw-r--r--include/mingw.h1
-rw-r--r--networking/httpd.c10
-rw-r--r--win32/process.c18
3 files changed, 19 insertions, 10 deletions
diff --git a/include/mingw.h b/include/mingw.h
index c2c37ba48..6204ded6d 100644
--- a/include/mingw.h
+++ b/include/mingw.h
@@ -526,6 +526,7 @@ int utimes(const char *file_name, const struct timeval times[2]);
526#define is_unc_path(x) (strlen(x) > 4 && is_dir_sep(x[0]) && \ 526#define is_unc_path(x) (strlen(x) > 4 && is_dir_sep(x[0]) && \
527 is_dir_sep(x[1]) && !is_dir_sep(x[2])) 527 is_dir_sep(x[1]) && !is_dir_sep(x[2]))
528 528
529char ** FAST_FUNC grow_argv(char **argv, int n);
529pid_t FAST_FUNC mingw_spawn(char **argv); 530pid_t FAST_FUNC mingw_spawn(char **argv);
530pid_t FAST_FUNC mingw_spawn_detach(char **argv); 531pid_t FAST_FUNC mingw_spawn_detach(char **argv);
531intptr_t FAST_FUNC mingw_spawn_proc(const char **argv); 532intptr_t FAST_FUNC mingw_spawn_proc(const char **argv);
diff --git a/networking/httpd.c b/networking/httpd.c
index 302d1611d..2b2d14076 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -2804,7 +2804,7 @@ static void mini_httpd_win32(int fg, int sock, int argc, char **argv)
2804 2804
2805 argv_copy[0] = (char *)bb_busybox_exec_path; 2805 argv_copy[0] = (char *)bb_busybox_exec_path;
2806 argv_copy[1] = (char *)"--busybox"; 2806 argv_copy[1] = (char *)"--busybox";
2807 argv_copy[2] = (char *)"httpd"; 2807 argv_copy[2] = (char *)"-httpd" + 1; // skip '-'
2808 argv_copy[3] = (char *)"-I"; 2808 argv_copy[3] = (char *)"-I";
2809 memcpy(&argv_copy[5], &argv[1], argc * sizeof(argv[0])); 2809 memcpy(&argv_copy[5], &argv[1], argc * sizeof(argv[0]));
2810 2810
@@ -2850,14 +2850,12 @@ static void mini_httpd_inetd(void)
2850static void mingw_daemonize(char **argv) 2850static void mingw_daemonize(char **argv)
2851{ 2851{
2852 char **new_argv; 2852 char **new_argv;
2853 int argc, fd; 2853 int fd;
2854 2854
2855 argc = string_array_len((char **)argv); 2855 new_argv = grow_argv(argv + 1, 3);
2856 new_argv = xmalloc(sizeof(*argv)*(argc+3));
2857 new_argv[0] = (char *)bb_busybox_exec_path; 2856 new_argv[0] = (char *)bb_busybox_exec_path;
2858 new_argv[1] = (char *)"--busybox"; 2857 new_argv[1] = (char *)"--busybox";
2859 new_argv[2] = (char *)"-httpd"; 2858 new_argv[2] = (char *)"-httpd"; // '-' marks the daemonised process
2860 memcpy(&new_argv[3], &argv[1], sizeof(*argv)*argc);
2861 2859
2862 fd = xopen(bb_dev_null, O_RDWR); 2860 fd = xopen(bb_dev_null, O_RDWR);
2863 xdup2(fd, 0); 2861 xdup2(fd, 0);
diff --git a/win32/process.c b/win32/process.c
index 5c818a3c0..e93efa103 100644
--- a/win32/process.c
+++ b/win32/process.c
@@ -259,6 +259,19 @@ mingw_spawn_applet(int mode,
259} 259}
260#endif 260#endif
261 261
262/* Make a copy of an argv array with n extra slots at the start */
263char ** FAST_FUNC
264grow_argv(char **argv, int n)
265{
266 char **new_argv;
267 int argc;
268
269 argc = string_array_len(argv) + 1;
270 new_argv = xmalloc(sizeof(*argv) * (argc + n));
271 memcpy(new_argv + n, argv, sizeof(*argv) * argc);
272 return new_argv;
273}
274
262static intptr_t 275static intptr_t
263mingw_spawn_interpreter(int mode, const char *prog, char *const *argv, 276mingw_spawn_interpreter(int mode, const char *prog, char *const *argv,
264 char *const *envp, int level) 277 char *const *envp, int level)
@@ -267,7 +280,6 @@ mingw_spawn_interpreter(int mode, const char *prog, char *const *argv,
267 int nopts; 280 int nopts;
268 interp_t interp; 281 interp_t interp;
269 char **new_argv; 282 char **new_argv;
270 int argc;
271 char *path = NULL; 283 char *path = NULL;
272 284
273 if (!parse_interpreter(prog, &interp)) 285 if (!parse_interpreter(prog, &interp))
@@ -279,11 +291,9 @@ mingw_spawn_interpreter(int mode, const char *prog, char *const *argv,
279 } 291 }
280 292
281 nopts = interp.opts != NULL; 293 nopts = interp.opts != NULL;
282 argc = string_array_len((char **)argv); 294 new_argv = grow_argv((char **)(argv + 1), nopts + 2);
283 new_argv = xmalloc(sizeof(*argv)*(argc+nopts+2));
284 new_argv[1] = interp.opts; 295 new_argv[1] = interp.opts;
285 new_argv[nopts+1] = (char *)prog; /* pass absolute path */ 296 new_argv[nopts+1] = (char *)prog; /* pass absolute path */
286 memcpy(new_argv+nopts+2, argv+1, sizeof(*argv)*argc);
287 297
288#if ENABLE_FEATURE_PREFER_APPLETS && NUM_APPLETS > 1 298#if ENABLE_FEATURE_PREFER_APPLETS && NUM_APPLETS > 1
289 if (unix_path(interp.path) && find_applet_by_name(interp.name) >= 0) { 299 if (unix_path(interp.path) && find_applet_by_name(interp.name) >= 0) {