|
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 |
|
@@ -1629,6 +1630,65 @@ static char *lookup_prog(const char *dir, int dirlen, const char *cmd,
|
1629 | 1630 | return NULL;
|
1630 | 1631 | }
|
1631 | 1632 |
|
| 1633 | +static char *path_lookup(const char *cmd, int exe_only); |
| 1634 | + |
| 1635 | +static char *is_busybox_applet(const char *cmd) |
| 1636 | +{ |
| 1637 | + static struct string_list applets = STRING_LIST_INIT_DUP; |
| 1638 | + static char *busybox_path; |
| 1639 | + static int busybox_path_initialized; |
| 1640 | + |
| 1641 | + /* Avoid infinite loop */ |
| 1642 | + if (!strncasecmp(cmd, "busybox", 7) && |
| 1643 | + (!cmd[7] || !strcasecmp(cmd + 7, ".exe"))) |
| 1644 | + return NULL; |
| 1645 | + |
| 1646 | + if (!busybox_path_initialized) { |
| 1647 | + busybox_path = path_lookup("busybox.exe", 1); |
| 1648 | + busybox_path_initialized = 1; |
| 1649 | + } |
| 1650 | + |
| 1651 | + /* Assume that sh is compiled in... */ |
| 1652 | + if (!busybox_path || !strcasecmp(cmd, "sh")) |
| 1653 | + return xstrdup_or_null(busybox_path); |
| 1654 | + |
| 1655 | + if (!applets.nr) { |
| 1656 | + struct child_process cp = CHILD_PROCESS_INIT; |
| 1657 | + struct strbuf buf = STRBUF_INIT; |
| 1658 | + char *p; |
| 1659 | + |
| 1660 | + strvec_pushl(&cp.args, busybox_path, "--help", NULL); |
| 1661 | + |
| 1662 | + if (capture_command(&cp, &buf, 2048)) { |
| 1663 | + string_list_append(&applets, ""); |
| 1664 | + return NULL; |
| 1665 | + } |
| 1666 | + |
| 1667 | + /* parse output */ |
| 1668 | + p = strstr(buf.buf, "Currently defined functions:\n"); |
| 1669 | + if (!p) { |
| 1670 | + warning("Could not parse output of busybox --help"); |
| 1671 | + string_list_append(&applets, ""); |
| 1672 | + return NULL; |
| 1673 | + } |
| 1674 | + p = strchrnul(p, '\n'); |
| 1675 | + for (;;) { |
| 1676 | + size_t len; |
| 1677 | + |
| 1678 | + p += strspn(p, "\n\t ,"); |
| 1679 | + len = strcspn(p, "\n\t ,"); |
| 1680 | + if (!len) |
| 1681 | + break; |
| 1682 | + p[len] = '\0'; |
| 1683 | + string_list_insert(&applets, p); |
| 1684 | + p = p + len + 1; |
| 1685 | + } |
| 1686 | + } |
| 1687 | + |
| 1688 | + return string_list_has_string(&applets, cmd) ? |
| 1689 | + xstrdup(busybox_path) : NULL; |
| 1690 | +} |
| 1691 | + |
1632 | 1692 | /*
|
1633 | 1693 | * Determines the absolute path of cmd using the split path in path.
|
1634 | 1694 | * If cmd contains a slash or backslash, no lookup is performed.
|
@@ -1657,6 +1717,9 @@ static char *path_lookup(const char *cmd, int exe_only)
|
1657 | 1717 | path = sep + 1;
|
1658 | 1718 | }
|
1659 | 1719 |
|
| 1720 | + if (!prog && !isexe) |
| 1721 | + prog = is_busybox_applet(cmd); |
| 1722 | + |
1660 | 1723 | return prog;
|
1661 | 1724 | }
|
1662 | 1725 |
|
|
0 commit comments