|
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 |
|
@@ -1586,6 +1587,65 @@ static char *lookup_prog(const char *dir, int dirlen, const char *cmd,
|
1586 | 1587 | return NULL;
|
1587 | 1588 | }
|
1588 | 1589 |
|
| 1590 | +static char *path_lookup(const char *cmd, int exe_only); |
| 1591 | + |
| 1592 | +static char *is_busybox_applet(const char *cmd) |
| 1593 | +{ |
| 1594 | + static struct string_list applets = STRING_LIST_INIT_DUP; |
| 1595 | + static char *busybox_path; |
| 1596 | + static int busybox_path_initialized; |
| 1597 | + |
| 1598 | + /* Avoid infinite loop */ |
| 1599 | + if (!strncasecmp(cmd, "busybox", 7) && |
| 1600 | + (!cmd[7] || !strcasecmp(cmd + 7, ".exe"))) |
| 1601 | + return NULL; |
| 1602 | + |
| 1603 | + if (!busybox_path_initialized) { |
| 1604 | + busybox_path = path_lookup("busybox.exe", 1); |
| 1605 | + busybox_path_initialized = 1; |
| 1606 | + } |
| 1607 | + |
| 1608 | + /* Assume that sh is compiled in... */ |
| 1609 | + if (!busybox_path || !strcasecmp(cmd, "sh")) |
| 1610 | + return xstrdup_or_null(busybox_path); |
| 1611 | + |
| 1612 | + if (!applets.nr) { |
| 1613 | + struct child_process cp = CHILD_PROCESS_INIT; |
| 1614 | + struct strbuf buf = STRBUF_INIT; |
| 1615 | + char *p; |
| 1616 | + |
| 1617 | + strvec_pushl(&cp.args, busybox_path, "--help", NULL); |
| 1618 | + |
| 1619 | + if (capture_command(&cp, &buf, 2048)) { |
| 1620 | + string_list_append(&applets, ""); |
| 1621 | + return NULL; |
| 1622 | + } |
| 1623 | + |
| 1624 | + /* parse output */ |
| 1625 | + p = strstr(buf.buf, "Currently defined functions:\n"); |
| 1626 | + if (!p) { |
| 1627 | + warning("Could not parse output of busybox --help"); |
| 1628 | + string_list_append(&applets, ""); |
| 1629 | + return NULL; |
| 1630 | + } |
| 1631 | + p = strchrnul(p, '\n'); |
| 1632 | + for (;;) { |
| 1633 | + size_t len; |
| 1634 | + |
| 1635 | + p += strspn(p, "\n\t ,"); |
| 1636 | + len = strcspn(p, "\n\t ,"); |
| 1637 | + if (!len) |
| 1638 | + break; |
| 1639 | + p[len] = '\0'; |
| 1640 | + string_list_insert(&applets, p); |
| 1641 | + p = p + len + 1; |
| 1642 | + } |
| 1643 | + } |
| 1644 | + |
| 1645 | + return string_list_has_string(&applets, cmd) ? |
| 1646 | + xstrdup(busybox_path) : NULL; |
| 1647 | +} |
| 1648 | + |
1589 | 1649 | /*
|
1590 | 1650 | * Determines the absolute path of cmd using the split path in path.
|
1591 | 1651 | * If cmd contains a slash or backslash, no lookup is performed.
|
@@ -1614,6 +1674,9 @@ static char *path_lookup(const char *cmd, int exe_only)
|
1614 | 1674 | path = sep + 1;
|
1615 | 1675 | }
|
1616 | 1676 |
|
| 1677 | + if (!prog && !isexe) |
| 1678 | + prog = is_busybox_applet(cmd); |
| 1679 | + |
1617 | 1680 | return prog;
|
1618 | 1681 | }
|
1619 | 1682 |
|
|
0 commit comments