26
26
#include "../repository.h"
27
27
#include "win32/fscache.h"
28
28
#include "../attr.h"
29
+ #include "../string-list.h"
29
30
30
31
#define HCAST (type , handle ) ((type)(intptr_t)handle)
31
32
@@ -1735,6 +1736,65 @@ static char *lookup_prog(const char *dir, int dirlen, const char *cmd,
1735
1736
return NULL ;
1736
1737
}
1737
1738
1739
+ static char * path_lookup (const char * cmd , int exe_only );
1740
+
1741
+ static char * is_busybox_applet (const char * cmd )
1742
+ {
1743
+ static struct string_list applets = STRING_LIST_INIT_DUP ;
1744
+ static char * busybox_path ;
1745
+ static int busybox_path_initialized ;
1746
+
1747
+ /* Avoid infinite loop */
1748
+ if (!strncasecmp (cmd , "busybox" , 7 ) &&
1749
+ (!cmd [7 ] || !strcasecmp (cmd + 7 , ".exe" )))
1750
+ return NULL ;
1751
+
1752
+ if (!busybox_path_initialized ) {
1753
+ busybox_path = path_lookup ("busybox.exe" , 1 );
1754
+ busybox_path_initialized = 1 ;
1755
+ }
1756
+
1757
+ /* Assume that sh is compiled in... */
1758
+ if (!busybox_path || !strcasecmp (cmd , "sh" ))
1759
+ return xstrdup_or_null (busybox_path );
1760
+
1761
+ if (!applets .nr ) {
1762
+ struct child_process cp = CHILD_PROCESS_INIT ;
1763
+ struct strbuf buf = STRBUF_INIT ;
1764
+ char * p ;
1765
+
1766
+ strvec_pushl (& cp .args , busybox_path , "--help" , NULL );
1767
+
1768
+ if (capture_command (& cp , & buf , 2048 )) {
1769
+ string_list_append (& applets , "" );
1770
+ return NULL ;
1771
+ }
1772
+
1773
+ /* parse output */
1774
+ p = strstr (buf .buf , "Currently defined functions:\n" );
1775
+ if (!p ) {
1776
+ warning ("Could not parse output of busybox --help" );
1777
+ string_list_append (& applets , "" );
1778
+ return NULL ;
1779
+ }
1780
+ p = strchrnul (p , '\n' );
1781
+ for (;;) {
1782
+ size_t len ;
1783
+
1784
+ p += strspn (p , "\n\t ," );
1785
+ len = strcspn (p , "\n\t ," );
1786
+ if (!len )
1787
+ break ;
1788
+ p [len ] = '\0' ;
1789
+ string_list_insert (& applets , p );
1790
+ p = p + len + 1 ;
1791
+ }
1792
+ }
1793
+
1794
+ return string_list_has_string (& applets , cmd ) ?
1795
+ xstrdup (busybox_path ) : NULL ;
1796
+ }
1797
+
1738
1798
/*
1739
1799
* Determines the absolute path of cmd using the split path in path.
1740
1800
* If cmd contains a slash or backslash, no lookup is performed.
@@ -1763,6 +1823,9 @@ static char *path_lookup(const char *cmd, int exe_only)
1763
1823
path = sep + 1 ;
1764
1824
}
1765
1825
1826
+ if (!prog && !isexe )
1827
+ prog = is_busybox_applet (cmd );
1828
+
1766
1829
return prog ;
1767
1830
}
1768
1831
@@ -1966,8 +2029,8 @@ static int is_msys2_sh(const char *cmd)
1966
2029
}
1967
2030
1968
2031
static pid_t mingw_spawnve_fd (const char * cmd , const char * * argv , char * * deltaenv ,
1969
- const char * dir ,
1970
- int prepend_cmd , int fhin , int fhout , int fherr )
2032
+ const char * dir , const char * prepend_cmd ,
2033
+ int fhin , int fhout , int fherr )
1971
2034
{
1972
2035
static int restrict_handle_inheritance = -1 ;
1973
2036
STARTUPINFOEXW si ;
@@ -2058,9 +2121,9 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
2058
2121
/* concatenate argv, quoting args as we go */
2059
2122
strbuf_init (& args , 0 );
2060
2123
if (prepend_cmd ) {
2061
- char * quoted = (char * )quote_arg (cmd );
2124
+ char * quoted = (char * )quote_arg (prepend_cmd );
2062
2125
strbuf_addstr (& args , quoted );
2063
- if (quoted != cmd )
2126
+ if (quoted != prepend_cmd )
2064
2127
free (quoted );
2065
2128
}
2066
2129
for (; * argv ; argv ++ ) {
@@ -2219,7 +2282,8 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
2219
2282
return (pid_t )pi .dwProcessId ;
2220
2283
}
2221
2284
2222
- static pid_t mingw_spawnv (const char * cmd , const char * * argv , int prepend_cmd )
2285
+ static pid_t mingw_spawnv (const char * cmd , const char * * argv ,
2286
+ const char * prepend_cmd )
2223
2287
{
2224
2288
return mingw_spawnve_fd (cmd , argv , NULL , NULL , prepend_cmd , 0 , 1 , 2 );
2225
2289
}
@@ -2247,14 +2311,14 @@ pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **deltaenv,
2247
2311
pid = -1 ;
2248
2312
}
2249
2313
else {
2250
- pid = mingw_spawnve_fd (iprog , argv , deltaenv , dir , 1 ,
2314
+ pid = mingw_spawnve_fd (iprog , argv , deltaenv , dir , interpr ,
2251
2315
fhin , fhout , fherr );
2252
2316
free (iprog );
2253
2317
}
2254
2318
argv [0 ] = argv0 ;
2255
2319
}
2256
2320
else
2257
- pid = mingw_spawnve_fd (prog , argv , deltaenv , dir , 0 ,
2321
+ pid = mingw_spawnve_fd (prog , argv , deltaenv , dir , NULL ,
2258
2322
fhin , fhout , fherr );
2259
2323
free (prog );
2260
2324
}
@@ -2279,7 +2343,7 @@ static int try_shell_exec(const char *cmd, char *const *argv)
2279
2343
argv2 [0 ] = (char * )cmd ; /* full path to the script file */
2280
2344
COPY_ARRAY (& argv2 [1 ], & argv [1 ], argc );
2281
2345
exec_id = trace2_exec (prog , (const char * * )argv2 );
2282
- pid = mingw_spawnv (prog , (const char * * )argv2 , 1 );
2346
+ pid = mingw_spawnv (prog , (const char * * )argv2 , interpr );
2283
2347
if (pid >= 0 ) {
2284
2348
int status ;
2285
2349
if (waitpid (pid , & status , 0 ) < 0 )
@@ -2303,7 +2367,7 @@ int mingw_execv(const char *cmd, char *const *argv)
2303
2367
int exec_id ;
2304
2368
2305
2369
exec_id = trace2_exec (cmd , (const char * * )argv );
2306
- pid = mingw_spawnv (cmd , (const char * * )argv , 0 );
2370
+ pid = mingw_spawnv (cmd , (const char * * )argv , NULL );
2307
2371
if (pid < 0 ) {
2308
2372
trace2_exec_result (exec_id , -1 );
2309
2373
return -1 ;
0 commit comments