23
23
#include "../repository.h"
24
24
#include "win32/fscache.h"
25
25
#include "../attr.h"
26
+ #include "../string-list.h"
26
27
27
28
#define HCAST (type , handle ) ((type)(intptr_t)handle)
28
29
@@ -1651,6 +1652,65 @@ static char *lookup_prog(const char *dir, int dirlen, const char *cmd,
1651
1652
return NULL ;
1652
1653
}
1653
1654
1655
+ static char * path_lookup (const char * cmd , int exe_only );
1656
+
1657
+ static char * is_busybox_applet (const char * cmd )
1658
+ {
1659
+ static struct string_list applets = STRING_LIST_INIT_DUP ;
1660
+ static char * busybox_path ;
1661
+ static int busybox_path_initialized ;
1662
+
1663
+ /* Avoid infinite loop */
1664
+ if (!strncasecmp (cmd , "busybox" , 7 ) &&
1665
+ (!cmd [7 ] || !strcasecmp (cmd + 7 , ".exe" )))
1666
+ return NULL ;
1667
+
1668
+ if (!busybox_path_initialized ) {
1669
+ busybox_path = path_lookup ("busybox.exe" , 1 );
1670
+ busybox_path_initialized = 1 ;
1671
+ }
1672
+
1673
+ /* Assume that sh is compiled in... */
1674
+ if (!busybox_path || !strcasecmp (cmd , "sh" ))
1675
+ return xstrdup_or_null (busybox_path );
1676
+
1677
+ if (!applets .nr ) {
1678
+ struct child_process cp = CHILD_PROCESS_INIT ;
1679
+ struct strbuf buf = STRBUF_INIT ;
1680
+ char * p ;
1681
+
1682
+ strvec_pushl (& cp .args , busybox_path , "--help" , NULL );
1683
+
1684
+ if (capture_command (& cp , & buf , 2048 )) {
1685
+ string_list_append (& applets , "" );
1686
+ return NULL ;
1687
+ }
1688
+
1689
+ /* parse output */
1690
+ p = strstr (buf .buf , "Currently defined functions:\n" );
1691
+ if (!p ) {
1692
+ warning ("Could not parse output of busybox --help" );
1693
+ string_list_append (& applets , "" );
1694
+ return NULL ;
1695
+ }
1696
+ p = strchrnul (p , '\n' );
1697
+ for (;;) {
1698
+ size_t len ;
1699
+
1700
+ p += strspn (p , "\n\t ," );
1701
+ len = strcspn (p , "\n\t ," );
1702
+ if (!len )
1703
+ break ;
1704
+ p [len ] = '\0' ;
1705
+ string_list_insert (& applets , p );
1706
+ p = p + len + 1 ;
1707
+ }
1708
+ }
1709
+
1710
+ return string_list_has_string (& applets , cmd ) ?
1711
+ xstrdup (busybox_path ) : NULL ;
1712
+ }
1713
+
1654
1714
/*
1655
1715
* Determines the absolute path of cmd using the split path in path.
1656
1716
* If cmd contains a slash or backslash, no lookup is performed.
@@ -1679,6 +1739,9 @@ static char *path_lookup(const char *cmd, int exe_only)
1679
1739
path = sep + 1 ;
1680
1740
}
1681
1741
1742
+ if (!prog && !isexe )
1743
+ prog = is_busybox_applet (cmd );
1744
+
1682
1745
return prog ;
1683
1746
}
1684
1747
@@ -1877,8 +1940,8 @@ static int is_msys2_sh(const char *cmd)
1877
1940
}
1878
1941
1879
1942
static pid_t mingw_spawnve_fd (const char * cmd , const char * * argv , char * * deltaenv ,
1880
- const char * dir ,
1881
- int prepend_cmd , int fhin , int fhout , int fherr )
1943
+ const char * dir , const char * prepend_cmd ,
1944
+ int fhin , int fhout , int fherr )
1882
1945
{
1883
1946
static int restrict_handle_inheritance = -1 ;
1884
1947
STARTUPINFOEXW si ;
@@ -1969,9 +2032,9 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
1969
2032
/* concatenate argv, quoting args as we go */
1970
2033
strbuf_init (& args , 0 );
1971
2034
if (prepend_cmd ) {
1972
- char * quoted = (char * )quote_arg (cmd );
2035
+ char * quoted = (char * )quote_arg (prepend_cmd );
1973
2036
strbuf_addstr (& args , quoted );
1974
- if (quoted != cmd )
2037
+ if (quoted != prepend_cmd )
1975
2038
free (quoted );
1976
2039
}
1977
2040
for (; * argv ; argv ++ ) {
@@ -2130,7 +2193,8 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
2130
2193
return (pid_t )pi .dwProcessId ;
2131
2194
}
2132
2195
2133
- static pid_t mingw_spawnv (const char * cmd , const char * * argv , int prepend_cmd )
2196
+ static pid_t mingw_spawnv (const char * cmd , const char * * argv ,
2197
+ const char * prepend_cmd )
2134
2198
{
2135
2199
return mingw_spawnve_fd (cmd , argv , NULL , NULL , prepend_cmd , 0 , 1 , 2 );
2136
2200
}
@@ -2158,14 +2222,14 @@ pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **deltaenv,
2158
2222
pid = -1 ;
2159
2223
}
2160
2224
else {
2161
- pid = mingw_spawnve_fd (iprog , argv , deltaenv , dir , 1 ,
2225
+ pid = mingw_spawnve_fd (iprog , argv , deltaenv , dir , interpr ,
2162
2226
fhin , fhout , fherr );
2163
2227
free (iprog );
2164
2228
}
2165
2229
argv [0 ] = argv0 ;
2166
2230
}
2167
2231
else
2168
- pid = mingw_spawnve_fd (prog , argv , deltaenv , dir , 0 ,
2232
+ pid = mingw_spawnve_fd (prog , argv , deltaenv , dir , NULL ,
2169
2233
fhin , fhout , fherr );
2170
2234
free (prog );
2171
2235
}
@@ -2190,7 +2254,7 @@ static int try_shell_exec(const char *cmd, char *const *argv)
2190
2254
argv2 [0 ] = (char * )cmd ; /* full path to the script file */
2191
2255
COPY_ARRAY (& argv2 [1 ], & argv [1 ], argc );
2192
2256
exec_id = trace2_exec (prog , (const char * * )argv2 );
2193
- pid = mingw_spawnv (prog , (const char * * )argv2 , 1 );
2257
+ pid = mingw_spawnv (prog , (const char * * )argv2 , interpr );
2194
2258
if (pid >= 0 ) {
2195
2259
int status ;
2196
2260
if (waitpid (pid , & status , 0 ) < 0 )
@@ -2214,7 +2278,7 @@ int mingw_execv(const char *cmd, char *const *argv)
2214
2278
int exec_id ;
2215
2279
2216
2280
exec_id = trace2_exec (cmd , (const char * * )argv );
2217
- pid = mingw_spawnv (cmd , (const char * * )argv , 0 );
2281
+ pid = mingw_spawnv (cmd , (const char * * )argv , NULL );
2218
2282
if (pid < 0 ) {
2219
2283
trace2_exec_result (exec_id , -1 );
2220
2284
return -1 ;
0 commit comments