15
15
#include <sspi.h>
16
16
#include "win32/fscache.h"
17
17
#include "../attr.h"
18
+ #include "../string-list.h"
18
19
19
20
#define HCAST (type , handle ) ((type)(intptr_t)handle)
20
21
@@ -1614,6 +1615,65 @@ static char *lookup_prog(const char *dir, int dirlen, const char *cmd,
1614
1615
return NULL ;
1615
1616
}
1616
1617
1618
+ static char * path_lookup (const char * cmd , int exe_only );
1619
+
1620
+ static char * is_busybox_applet (const char * cmd )
1621
+ {
1622
+ static struct string_list applets = STRING_LIST_INIT_DUP ;
1623
+ static char * busybox_path ;
1624
+ static int busybox_path_initialized ;
1625
+
1626
+ /* Avoid infinite loop */
1627
+ if (!strncasecmp (cmd , "busybox" , 7 ) &&
1628
+ (!cmd [7 ] || !strcasecmp (cmd + 7 , ".exe" )))
1629
+ return NULL ;
1630
+
1631
+ if (!busybox_path_initialized ) {
1632
+ busybox_path = path_lookup ("busybox.exe" , 1 );
1633
+ busybox_path_initialized = 1 ;
1634
+ }
1635
+
1636
+ /* Assume that sh is compiled in... */
1637
+ if (!busybox_path || !strcasecmp (cmd , "sh" ))
1638
+ return xstrdup_or_null (busybox_path );
1639
+
1640
+ if (!applets .nr ) {
1641
+ struct child_process cp = CHILD_PROCESS_INIT ;
1642
+ struct strbuf buf = STRBUF_INIT ;
1643
+ char * p ;
1644
+
1645
+ strvec_pushl (& cp .args , busybox_path , "--help" , NULL );
1646
+
1647
+ if (capture_command (& cp , & buf , 2048 )) {
1648
+ string_list_append (& applets , "" );
1649
+ return NULL ;
1650
+ }
1651
+
1652
+ /* parse output */
1653
+ p = strstr (buf .buf , "Currently defined functions:\n" );
1654
+ if (!p ) {
1655
+ warning ("Could not parse output of busybox --help" );
1656
+ string_list_append (& applets , "" );
1657
+ return NULL ;
1658
+ }
1659
+ p = strchrnul (p , '\n' );
1660
+ for (;;) {
1661
+ size_t len ;
1662
+
1663
+ p += strspn (p , "\n\t ," );
1664
+ len = strcspn (p , "\n\t ," );
1665
+ if (!len )
1666
+ break ;
1667
+ p [len ] = '\0' ;
1668
+ string_list_insert (& applets , p );
1669
+ p = p + len + 1 ;
1670
+ }
1671
+ }
1672
+
1673
+ return string_list_has_string (& applets , cmd ) ?
1674
+ xstrdup (busybox_path ) : NULL ;
1675
+ }
1676
+
1617
1677
/*
1618
1678
* Determines the absolute path of cmd using the split path in path.
1619
1679
* If cmd contains a slash or backslash, no lookup is performed.
@@ -1642,6 +1702,9 @@ static char *path_lookup(const char *cmd, int exe_only)
1642
1702
path = sep + 1 ;
1643
1703
}
1644
1704
1705
+ if (!prog && !isexe )
1706
+ prog = is_busybox_applet (cmd );
1707
+
1645
1708
return prog ;
1646
1709
}
1647
1710
@@ -1841,8 +1904,8 @@ static int is_msys2_sh(const char *cmd)
1841
1904
}
1842
1905
1843
1906
static pid_t mingw_spawnve_fd (const char * cmd , const char * * argv , char * * deltaenv ,
1844
- const char * dir ,
1845
- int prepend_cmd , int fhin , int fhout , int fherr )
1907
+ const char * dir , const char * prepend_cmd ,
1908
+ int fhin , int fhout , int fherr )
1846
1909
{
1847
1910
static int restrict_handle_inheritance = -1 ;
1848
1911
STARTUPINFOEXW si ;
@@ -1933,9 +1996,9 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
1933
1996
/* concatenate argv, quoting args as we go */
1934
1997
strbuf_init (& args , 0 );
1935
1998
if (prepend_cmd ) {
1936
- char * quoted = (char * )quote_arg (cmd );
1999
+ char * quoted = (char * )quote_arg (prepend_cmd );
1937
2000
strbuf_addstr (& args , quoted );
1938
- if (quoted != cmd )
2001
+ if (quoted != prepend_cmd )
1939
2002
free (quoted );
1940
2003
}
1941
2004
for (; * argv ; argv ++ ) {
@@ -2094,7 +2157,8 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
2094
2157
return (pid_t )pi .dwProcessId ;
2095
2158
}
2096
2159
2097
- static pid_t mingw_spawnv (const char * cmd , const char * * argv , int prepend_cmd )
2160
+ static pid_t mingw_spawnv (const char * cmd , const char * * argv ,
2161
+ const char * prepend_cmd )
2098
2162
{
2099
2163
return mingw_spawnve_fd (cmd , argv , NULL , NULL , prepend_cmd , 0 , 1 , 2 );
2100
2164
}
@@ -2122,14 +2186,14 @@ pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **deltaenv,
2122
2186
pid = -1 ;
2123
2187
}
2124
2188
else {
2125
- pid = mingw_spawnve_fd (iprog , argv , deltaenv , dir , 1 ,
2189
+ pid = mingw_spawnve_fd (iprog , argv , deltaenv , dir , interpr ,
2126
2190
fhin , fhout , fherr );
2127
2191
free (iprog );
2128
2192
}
2129
2193
argv [0 ] = argv0 ;
2130
2194
}
2131
2195
else
2132
- pid = mingw_spawnve_fd (prog , argv , deltaenv , dir , 0 ,
2196
+ pid = mingw_spawnve_fd (prog , argv , deltaenv , dir , NULL ,
2133
2197
fhin , fhout , fherr );
2134
2198
free (prog );
2135
2199
}
@@ -2157,7 +2221,7 @@ static int try_shell_exec(const char *cmd, char *const *argv)
2157
2221
argv2 [0 ] = (char * )cmd ; /* full path to the script file */
2158
2222
COPY_ARRAY (& argv2 [1 ], & argv [1 ], argc );
2159
2223
exec_id = trace2_exec (prog , argv2 );
2160
- pid = mingw_spawnv (prog , argv2 , 1 );
2224
+ pid = mingw_spawnv (prog , argv2 , interpr );
2161
2225
if (pid >= 0 ) {
2162
2226
int status ;
2163
2227
if (waitpid (pid , & status , 0 ) < 0 )
@@ -2181,7 +2245,7 @@ int mingw_execv(const char *cmd, char *const *argv)
2181
2245
int exec_id ;
2182
2246
2183
2247
exec_id = trace2_exec (cmd , (const char * * )argv );
2184
- pid = mingw_spawnv (cmd , (const char * * )argv , 0 );
2248
+ pid = mingw_spawnv (cmd , (const char * * )argv , NULL );
2185
2249
if (pid < 0 ) {
2186
2250
trace2_exec_result (exec_id , -1 );
2187
2251
return -1 ;
0 commit comments