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
@@ -1736,6 +1737,65 @@ static char *lookup_prog(const char *dir, int dirlen, const char *cmd,
1736
1737
return NULL ;
1737
1738
}
1738
1739
1740
+ static char * path_lookup (const char * cmd , int exe_only );
1741
+
1742
+ static char * is_busybox_applet (const char * cmd )
1743
+ {
1744
+ static struct string_list applets = STRING_LIST_INIT_DUP ;
1745
+ static char * busybox_path ;
1746
+ static int busybox_path_initialized ;
1747
+
1748
+ /* Avoid infinite loop */
1749
+ if (!strncasecmp (cmd , "busybox" , 7 ) &&
1750
+ (!cmd [7 ] || !strcasecmp (cmd + 7 , ".exe" )))
1751
+ return NULL ;
1752
+
1753
+ if (!busybox_path_initialized ) {
1754
+ busybox_path = path_lookup ("busybox.exe" , 1 );
1755
+ busybox_path_initialized = 1 ;
1756
+ }
1757
+
1758
+ /* Assume that sh is compiled in... */
1759
+ if (!busybox_path || !strcasecmp (cmd , "sh" ))
1760
+ return xstrdup_or_null (busybox_path );
1761
+
1762
+ if (!applets .nr ) {
1763
+ struct child_process cp = CHILD_PROCESS_INIT ;
1764
+ struct strbuf buf = STRBUF_INIT ;
1765
+ char * p ;
1766
+
1767
+ strvec_pushl (& cp .args , busybox_path , "--help" , NULL );
1768
+
1769
+ if (capture_command (& cp , & buf , 2048 )) {
1770
+ string_list_append (& applets , "" );
1771
+ return NULL ;
1772
+ }
1773
+
1774
+ /* parse output */
1775
+ p = strstr (buf .buf , "Currently defined functions:\n" );
1776
+ if (!p ) {
1777
+ warning ("Could not parse output of busybox --help" );
1778
+ string_list_append (& applets , "" );
1779
+ return NULL ;
1780
+ }
1781
+ p = strchrnul (p , '\n' );
1782
+ for (;;) {
1783
+ size_t len ;
1784
+
1785
+ p += strspn (p , "\n\t ," );
1786
+ len = strcspn (p , "\n\t ," );
1787
+ if (!len )
1788
+ break ;
1789
+ p [len ] = '\0' ;
1790
+ string_list_insert (& applets , p );
1791
+ p = p + len + 1 ;
1792
+ }
1793
+ }
1794
+
1795
+ return string_list_has_string (& applets , cmd ) ?
1796
+ xstrdup (busybox_path ) : NULL ;
1797
+ }
1798
+
1739
1799
/*
1740
1800
* Determines the absolute path of cmd using the split path in path.
1741
1801
* If cmd contains a slash or backslash, no lookup is performed.
@@ -1764,6 +1824,9 @@ static char *path_lookup(const char *cmd, int exe_only)
1764
1824
path = sep + 1 ;
1765
1825
}
1766
1826
1827
+ if (!prog && !isexe )
1828
+ prog = is_busybox_applet (cmd );
1829
+
1767
1830
return prog ;
1768
1831
}
1769
1832
@@ -1967,8 +2030,8 @@ static int is_msys2_sh(const char *cmd)
1967
2030
}
1968
2031
1969
2032
static pid_t mingw_spawnve_fd (const char * cmd , const char * * argv , char * * deltaenv ,
1970
- const char * dir ,
1971
- int prepend_cmd , int fhin , int fhout , int fherr )
2033
+ const char * dir , const char * prepend_cmd ,
2034
+ int fhin , int fhout , int fherr )
1972
2035
{
1973
2036
STARTUPINFOEXW si ;
1974
2037
PROCESS_INFORMATION pi ;
@@ -2048,9 +2111,9 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
2048
2111
/* concatenate argv, quoting args as we go */
2049
2112
strbuf_init (& args , 0 );
2050
2113
if (prepend_cmd ) {
2051
- char * quoted = (char * )quote_arg (cmd );
2114
+ char * quoted = (char * )quote_arg (prepend_cmd );
2052
2115
strbuf_addstr (& args , quoted );
2053
- if (quoted != cmd )
2116
+ if (quoted != prepend_cmd )
2054
2117
free (quoted );
2055
2118
}
2056
2119
for (; * argv ; argv ++ ) {
@@ -2170,7 +2233,8 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
2170
2233
return (pid_t )pi .dwProcessId ;
2171
2234
}
2172
2235
2173
- static pid_t mingw_spawnv (const char * cmd , const char * * argv , int prepend_cmd )
2236
+ static pid_t mingw_spawnv (const char * cmd , const char * * argv ,
2237
+ const char * prepend_cmd )
2174
2238
{
2175
2239
return mingw_spawnve_fd (cmd , argv , NULL , NULL , prepend_cmd , 0 , 1 , 2 );
2176
2240
}
@@ -2198,14 +2262,14 @@ pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **deltaenv,
2198
2262
pid = -1 ;
2199
2263
}
2200
2264
else {
2201
- pid = mingw_spawnve_fd (iprog , argv , deltaenv , dir , 1 ,
2265
+ pid = mingw_spawnve_fd (iprog , argv , deltaenv , dir , interpr ,
2202
2266
fhin , fhout , fherr );
2203
2267
free (iprog );
2204
2268
}
2205
2269
argv [0 ] = argv0 ;
2206
2270
}
2207
2271
else
2208
- pid = mingw_spawnve_fd (prog , argv , deltaenv , dir , 0 ,
2272
+ pid = mingw_spawnve_fd (prog , argv , deltaenv , dir , NULL ,
2209
2273
fhin , fhout , fherr );
2210
2274
free (prog );
2211
2275
}
@@ -2230,7 +2294,7 @@ static int try_shell_exec(const char *cmd, char *const *argv)
2230
2294
argv2 [0 ] = (char * )cmd ; /* full path to the script file */
2231
2295
COPY_ARRAY (& argv2 [1 ], & argv [1 ], argc );
2232
2296
exec_id = trace2_exec (prog , (const char * * )argv2 );
2233
- pid = mingw_spawnv (prog , (const char * * )argv2 , 1 );
2297
+ pid = mingw_spawnv (prog , (const char * * )argv2 , interpr );
2234
2298
if (pid >= 0 ) {
2235
2299
int status ;
2236
2300
if (waitpid (pid , & status , 0 ) < 0 )
@@ -2254,7 +2318,7 @@ int mingw_execv(const char *cmd, char *const *argv)
2254
2318
int exec_id ;
2255
2319
2256
2320
exec_id = trace2_exec (cmd , (const char * * )argv );
2257
- pid = mingw_spawnv (cmd , (const char * * )argv , 0 );
2321
+ pid = mingw_spawnv (cmd , (const char * * )argv , NULL );
2258
2322
if (pid < 0 ) {
2259
2323
trace2_exec_result (exec_id , -1 );
2260
2324
return -1 ;
0 commit comments