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