2424#include "../repository.h"
2525#include "win32/fscache.h"
2626#include "../attr.h"
27+ #include "../string-list.h"
2728
2829#define HCAST (type , handle ) ((type)(intptr_t)handle)
2930
@@ -1666,6 +1667,65 @@ static char *lookup_prog(const char *dir, int dirlen, const char *cmd,
16661667 return NULL ;
16671668}
16681669
1670+ static char * path_lookup (const char * cmd , int exe_only );
1671+
1672+ static char * is_busybox_applet (const char * cmd )
1673+ {
1674+ static struct string_list applets = STRING_LIST_INIT_DUP ;
1675+ static char * busybox_path ;
1676+ static int busybox_path_initialized ;
1677+
1678+ /* Avoid infinite loop */
1679+ if (!strncasecmp (cmd , "busybox" , 7 ) &&
1680+ (!cmd [7 ] || !strcasecmp (cmd + 7 , ".exe" )))
1681+ return NULL ;
1682+
1683+ if (!busybox_path_initialized ) {
1684+ busybox_path = path_lookup ("busybox.exe" , 1 );
1685+ busybox_path_initialized = 1 ;
1686+ }
1687+
1688+ /* Assume that sh is compiled in... */
1689+ if (!busybox_path || !strcasecmp (cmd , "sh" ))
1690+ return xstrdup_or_null (busybox_path );
1691+
1692+ if (!applets .nr ) {
1693+ struct child_process cp = CHILD_PROCESS_INIT ;
1694+ struct strbuf buf = STRBUF_INIT ;
1695+ char * p ;
1696+
1697+ strvec_pushl (& cp .args , busybox_path , "--help" , NULL );
1698+
1699+ if (capture_command (& cp , & buf , 2048 )) {
1700+ string_list_append (& applets , "" );
1701+ return NULL ;
1702+ }
1703+
1704+ /* parse output */
1705+ p = strstr (buf .buf , "Currently defined functions:\n" );
1706+ if (!p ) {
1707+ warning ("Could not parse output of busybox --help" );
1708+ string_list_append (& applets , "" );
1709+ return NULL ;
1710+ }
1711+ p = strchrnul (p , '\n' );
1712+ for (;;) {
1713+ size_t len ;
1714+
1715+ p += strspn (p , "\n\t ," );
1716+ len = strcspn (p , "\n\t ," );
1717+ if (!len )
1718+ break ;
1719+ p [len ] = '\0' ;
1720+ string_list_insert (& applets , p );
1721+ p = p + len + 1 ;
1722+ }
1723+ }
1724+
1725+ return string_list_has_string (& applets , cmd ) ?
1726+ xstrdup (busybox_path ) : NULL ;
1727+ }
1728+
16691729/*
16701730 * Determines the absolute path of cmd using the split path in path.
16711731 * If cmd contains a slash or backslash, no lookup is performed.
@@ -1694,6 +1754,9 @@ static char *path_lookup(const char *cmd, int exe_only)
16941754 path = sep + 1 ;
16951755 }
16961756
1757+ if (!prog && !isexe )
1758+ prog = is_busybox_applet (cmd );
1759+
16971760 return prog ;
16981761}
16991762
@@ -1897,8 +1960,8 @@ static int is_msys2_sh(const char *cmd)
18971960}
18981961
18991962static pid_t mingw_spawnve_fd (const char * cmd , const char * * argv , char * * deltaenv ,
1900- const char * dir ,
1901- int prepend_cmd , int fhin , int fhout , int fherr )
1963+ const char * dir , const char * prepend_cmd ,
1964+ int fhin , int fhout , int fherr )
19021965{
19031966 static int restrict_handle_inheritance = -1 ;
19041967 STARTUPINFOEXW si ;
@@ -1989,9 +2052,9 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
19892052 /* concatenate argv, quoting args as we go */
19902053 strbuf_init (& args , 0 );
19912054 if (prepend_cmd ) {
1992- char * quoted = (char * )quote_arg (cmd );
2055+ char * quoted = (char * )quote_arg (prepend_cmd );
19932056 strbuf_addstr (& args , quoted );
1994- if (quoted != cmd )
2057+ if (quoted != prepend_cmd )
19952058 free (quoted );
19962059 }
19972060 for (; * argv ; argv ++ ) {
@@ -2150,7 +2213,8 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
21502213 return (pid_t )pi .dwProcessId ;
21512214}
21522215
2153- static pid_t mingw_spawnv (const char * cmd , const char * * argv , int prepend_cmd )
2216+ static pid_t mingw_spawnv (const char * cmd , const char * * argv ,
2217+ const char * prepend_cmd )
21542218{
21552219 return mingw_spawnve_fd (cmd , argv , NULL , NULL , prepend_cmd , 0 , 1 , 2 );
21562220}
@@ -2178,14 +2242,14 @@ pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **deltaenv,
21782242 pid = -1 ;
21792243 }
21802244 else {
2181- pid = mingw_spawnve_fd (iprog , argv , deltaenv , dir , 1 ,
2245+ pid = mingw_spawnve_fd (iprog , argv , deltaenv , dir , interpr ,
21822246 fhin , fhout , fherr );
21832247 free (iprog );
21842248 }
21852249 argv [0 ] = argv0 ;
21862250 }
21872251 else
2188- pid = mingw_spawnve_fd (prog , argv , deltaenv , dir , 0 ,
2252+ pid = mingw_spawnve_fd (prog , argv , deltaenv , dir , NULL ,
21892253 fhin , fhout , fherr );
21902254 free (prog );
21912255 }
@@ -2210,7 +2274,7 @@ static int try_shell_exec(const char *cmd, char *const *argv)
22102274 argv2 [0 ] = (char * )cmd ; /* full path to the script file */
22112275 COPY_ARRAY (& argv2 [1 ], & argv [1 ], argc );
22122276 exec_id = trace2_exec (prog , (const char * * )argv2 );
2213- pid = mingw_spawnv (prog , (const char * * )argv2 , 1 );
2277+ pid = mingw_spawnv (prog , (const char * * )argv2 , interpr );
22142278 if (pid >= 0 ) {
22152279 int status ;
22162280 if (waitpid (pid , & status , 0 ) < 0 )
@@ -2234,7 +2298,7 @@ int mingw_execv(const char *cmd, char *const *argv)
22342298 int exec_id ;
22352299
22362300 exec_id = trace2_exec (cmd , (const char * * )argv );
2237- pid = mingw_spawnv (cmd , (const char * * )argv , 0 );
2301+ pid = mingw_spawnv (cmd , (const char * * )argv , NULL );
22382302 if (pid < 0 ) {
22392303 trace2_exec_result (exec_id , -1 );
22402304 return -1 ;
0 commit comments