aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2024-07-19 10:43:33 +0100
committerRon Yorston <rmy@pobox.com>2024-07-19 10:43:33 +0100
commit640ac19cd76573a307d39e06bc3d22a38194a76f (patch)
tree8d8e9b595cb61a9cf6ee6819cd19acff3f3da8b3
parent347c877fe9faab6ae6c6d1606006b28a2b3c7b64 (diff)
downloadbusybox-w32-640ac19cd76573a307d39e06bc3d22a38194a76f.tar.gz
busybox-w32-640ac19cd76573a307d39e06bc3d22a38194a76f.tar.bz2
busybox-w32-640ac19cd76573a307d39e06bc3d22a38194a76f.zip
win32: code shrink popen(3)
- Replace the half-baked code to quote the command passed to popen(3) with a call to quote_arg(). - Where the command to be run is a non-overridden applet in the current binary pass the path to the binary to CreateProcess() instead of adding '--busybox' to the command. Saves 128-136 bytes.
-rw-r--r--win32/popen.c53
1 files changed, 13 insertions, 40 deletions
diff --git a/win32/popen.c b/win32/popen.c
index 79433a27b..69c913b70 100644
--- a/win32/popen.c
+++ b/win32/popen.c
@@ -115,10 +115,8 @@ FILE *mingw_popen(const char *cmd, const char *mode)
115 pipe_data *p; 115 pipe_data *p;
116 FILE *fptr = NULL; 116 FILE *fptr = NULL;
117 int fd; 117 int fd;
118 int len, count; 118 char *arg, *cmd_buff;
119 char *cmd_buff = NULL; 119 const char *exe = NULL;
120 const char *s;
121 char *t;
122 120
123 if ( cmd == NULL || *cmd == '\0' || mode == NULL || 121 if ( cmd == NULL || *cmd == '\0' || mode == NULL ||
124 (*mode != 'r' && *mode != 'w') ) { 122 (*mode != 'r' && *mode != 'w') ) {
@@ -130,43 +128,21 @@ FILE *mingw_popen(const char *cmd, const char *mode)
130 return NULL; 128 return NULL;
131 } 129 }
132 130
133 /* count double quotes */
134 count = 0;
135 for ( s=cmd; *s; ++s ) {
136 if ( *s == '"' ) {
137 ++count;
138 }
139 }
140
141 len = strlen(bb_busybox_exec_path) + strlen(cmd) + 32 + count;
142 if ( (cmd_buff=malloc(len)) == NULL ) {
143 return NULL;
144 }
145 cmd_buff[0] = '\0';
146
147#if ENABLE_FEATURE_PREFER_APPLETS && NUM_APPLETS > 1 131#if ENABLE_FEATURE_PREFER_APPLETS && NUM_APPLETS > 1
148 if (find_applet_by_name("sh") >= 0) { 132 if (find_applet_by_name("sh") >= 0) {
149 sprintf(cmd_buff, "%s --busybox ", bb_busybox_exec_path); 133 exe = bb_busybox_exec_path;
150 } 134 }
151#endif 135#endif
152 strcat(cmd_buff, "sh -c \""); 136 arg = quote_arg(cmd);
153 137 cmd_buff = xasprintf("sh -c %s", arg);
154 /* escape double quotes */
155 for ( s=cmd,t=cmd_buff+strlen(cmd_buff); *s; ++s ) {
156 if ( *s == '"' ) {
157 *t++ = '\\';
158 }
159 *t++ = *s;
160 }
161 *t++ = '"';
162 *t = '\0';
163 138
164 /* Create the pipe */ 139 /* Create the pipe */
165 if ((fd=mingw_popen_internal(p, NULL, cmd_buff, mode, -1, NULL)) != -1) { 140 if ((fd=mingw_popen_internal(p, exe, cmd_buff, mode, -1, NULL)) != -1) {
166 fptr = _fdopen(fd, *mode == 'r' ? "rb" : "wb"); 141 fptr = _fdopen(fd, *mode == 'r' ? "rb" : "wb");
167 } 142 }
168 143
169 free(cmd_buff); 144 free(cmd_buff);
145 free(arg);
170 146
171 return fptr; 147 return fptr;
172} 148}
@@ -311,7 +287,8 @@ int mingw_pclose(FILE *fp)
311 * file; with mode "r" and a decompressor in open_transformer. */ 287 * file; with mode "r" and a decompressor in open_transformer. */
312pid_t mingw_fork_compressor(int fd, const char *compressor, const char *mode) 288pid_t mingw_fork_compressor(int fd, const char *compressor, const char *mode)
313{ 289{
314 char *cmd, *exe = NULL; 290 char *cmd, *freeme = NULL;
291 const char *exe = NULL;
315 int fd1; 292 int fd1;
316 pid_t pid; 293 pid_t pid;
317 294
@@ -323,25 +300,21 @@ pid_t mingw_fork_compressor(int fd, const char *compressor, const char *mode)
323 && !(mode[0] == 'w' && index_in_strings("lzma\0xz\0", compressor) >= 0) 300 && !(mode[0] == 'w' && index_in_strings("lzma\0xz\0", compressor) >= 0)
324# endif 301# endif
325 ) { 302 ) {
326 // shared format string 303 exe = bb_busybox_exec_path;
327 cmd = xasprintf("%s --busybox %s -cf -", bb_busybox_exec_path,
328 compressor);
329 } else { 304 } else {
330 // Look up compressor on PATH 305 // Look up compressor on PATH
331 exe = find_first_executable(compressor); 306 exe = freeme = find_first_executable(compressor);
332 if (exe == NULL) 307 if (exe == NULL)
333 bb_perror_msg_and_die("can't execute '%s'", compressor); 308 bb_perror_msg_and_die("can't execute '%s'", compressor);
334 cmd = xasprintf("%s --busybox %s -cf -" + 13, compressor);
335 } 309 }
336#else
337 cmd = xasprintf("%s -cf -", compressor);
338#endif 310#endif
311 cmd = xasprintf("%s -cf -", compressor);
339 312
340 if ((fd1 = mingw_popen_fd(exe, cmd, mode, fd, &pid)) == -1) 313 if ((fd1 = mingw_popen_fd(exe, cmd, mode, fd, &pid)) == -1)
341 bb_perror_msg_and_die("can't execute '%s'", compressor); 314 bb_perror_msg_and_die("can't execute '%s'", compressor);
342 315
343 free(cmd); 316 free(cmd);
344 free(exe); 317 free(freeme);
345 xmove_fd(fd1, fd); 318 xmove_fd(fd1, fd);
346 return pid; 319 return pid;
347} 320}