1212#include "repository.h"
1313#include "run-command.h"
1414#include "strbuf.h"
15+ #include "string-list.h"
1516#include "symlinks.h"
1617#include "trace2.h"
1718#include "win32.h"
@@ -1846,6 +1847,65 @@ static char *lookup_prog(const char *dir, int dirlen, const char *cmd,
18461847 return NULL ;
18471848}
18481849
1850+ static char * path_lookup (const char * cmd , int exe_only );
1851+
1852+ static char * is_busybox_applet (const char * cmd )
1853+ {
1854+ static struct string_list applets = STRING_LIST_INIT_DUP ;
1855+ static char * busybox_path ;
1856+ static int busybox_path_initialized ;
1857+
1858+ /* Avoid infinite loop */
1859+ if (!strncasecmp (cmd , "busybox" , 7 ) &&
1860+ (!cmd [7 ] || !strcasecmp (cmd + 7 , ".exe" )))
1861+ return NULL ;
1862+
1863+ if (!busybox_path_initialized ) {
1864+ busybox_path = path_lookup ("busybox.exe" , 1 );
1865+ busybox_path_initialized = 1 ;
1866+ }
1867+
1868+ /* Assume that sh is compiled in... */
1869+ if (!busybox_path || !strcasecmp (cmd , "sh" ))
1870+ return xstrdup_or_null (busybox_path );
1871+
1872+ if (!applets .nr ) {
1873+ struct child_process cp = CHILD_PROCESS_INIT ;
1874+ struct strbuf buf = STRBUF_INIT ;
1875+ char * p ;
1876+
1877+ strvec_pushl (& cp .args , busybox_path , "--help" , NULL );
1878+
1879+ if (capture_command (& cp , & buf , 2048 )) {
1880+ string_list_append (& applets , "" );
1881+ return NULL ;
1882+ }
1883+
1884+ /* parse output */
1885+ p = strstr (buf .buf , "Currently defined functions:\n" );
1886+ if (!p ) {
1887+ warning ("Could not parse output of busybox --help" );
1888+ string_list_append (& applets , "" );
1889+ return NULL ;
1890+ }
1891+ p = strchrnul (p , '\n' );
1892+ for (;;) {
1893+ size_t len ;
1894+
1895+ p += strspn (p , "\n\t ," );
1896+ len = strcspn (p , "\n\t ," );
1897+ if (!len )
1898+ break ;
1899+ p [len ] = '\0' ;
1900+ string_list_insert (& applets , p );
1901+ p = p + len + 1 ;
1902+ }
1903+ }
1904+
1905+ return string_list_has_string (& applets , cmd ) ?
1906+ xstrdup (busybox_path ) : NULL ;
1907+ }
1908+
18491909/*
18501910 * Determines the absolute path of cmd using the split path in path.
18511911 * If cmd contains a slash or backslash, no lookup is performed.
@@ -1874,6 +1934,9 @@ static char *path_lookup(const char *cmd, int exe_only)
18741934 path = sep + 1 ;
18751935 }
18761936
1937+ if (!prog && !isexe )
1938+ prog = is_busybox_applet (cmd );
1939+
18771940 return prog ;
18781941}
18791942
@@ -2077,8 +2140,8 @@ static int is_msys2_sh(const char *cmd)
20772140}
20782141
20792142static pid_t mingw_spawnve_fd (const char * cmd , const char * * argv , char * * deltaenv ,
2080- const char * dir ,
2081- int prepend_cmd , int fhin , int fhout , int fherr )
2143+ const char * dir , const char * prepend_cmd ,
2144+ int fhin , int fhout , int fherr )
20822145{
20832146 STARTUPINFOEXW si ;
20842147 PROCESS_INFORMATION pi ;
@@ -2158,9 +2221,9 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
21582221 /* concatenate argv, quoting args as we go */
21592222 strbuf_init (& args , 0 );
21602223 if (prepend_cmd ) {
2161- char * quoted = (char * )quote_arg (cmd );
2224+ char * quoted = (char * )quote_arg (prepend_cmd );
21622225 strbuf_addstr (& args , quoted );
2163- if (quoted != cmd )
2226+ if (quoted != prepend_cmd )
21642227 free (quoted );
21652228 }
21662229 for (; * argv ; argv ++ ) {
@@ -2280,7 +2343,8 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
22802343 return (pid_t )pi .dwProcessId ;
22812344}
22822345
2283- static pid_t mingw_spawnv (const char * cmd , const char * * argv , int prepend_cmd )
2346+ static pid_t mingw_spawnv (const char * cmd , const char * * argv ,
2347+ const char * prepend_cmd )
22842348{
22852349 return mingw_spawnve_fd (cmd , argv , NULL , NULL , prepend_cmd , 0 , 1 , 2 );
22862350}
@@ -2308,14 +2372,14 @@ pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **deltaenv,
23082372 pid = -1 ;
23092373 }
23102374 else {
2311- pid = mingw_spawnve_fd (iprog , argv , deltaenv , dir , 1 ,
2375+ pid = mingw_spawnve_fd (iprog , argv , deltaenv , dir , interpr ,
23122376 fhin , fhout , fherr );
23132377 free (iprog );
23142378 }
23152379 argv [0 ] = argv0 ;
23162380 }
23172381 else
2318- pid = mingw_spawnve_fd (prog , argv , deltaenv , dir , 0 ,
2382+ pid = mingw_spawnve_fd (prog , argv , deltaenv , dir , NULL ,
23192383 fhin , fhout , fherr );
23202384 free (prog );
23212385 }
@@ -2340,7 +2404,7 @@ static int try_shell_exec(const char *cmd, char *const *argv)
23402404 argv2 [0 ] = (char * )cmd ; /* full path to the script file */
23412405 COPY_ARRAY (& argv2 [1 ], & argv [1 ], argc );
23422406 exec_id = trace2_exec (prog , (const char * * )argv2 );
2343- pid = mingw_spawnv (prog , (const char * * )argv2 , 1 );
2407+ pid = mingw_spawnv (prog , (const char * * )argv2 , interpr );
23442408 if (pid >= 0 ) {
23452409 int status ;
23462410 if (waitpid (pid , & status , 0 ) < 0 )
@@ -2364,7 +2428,7 @@ int mingw_execv(const char *cmd, char *const *argv)
23642428 int exec_id ;
23652429
23662430 exec_id = trace2_exec (cmd , (const char * * )argv );
2367- pid = mingw_spawnv (cmd , (const char * * )argv , 0 );
2431+ pid = mingw_spawnv (cmd , (const char * * )argv , NULL );
23682432 if (pid < 0 ) {
23692433 trace2_exec_result (exec_id , -1 );
23702434 return -1 ;
0 commit comments