|
25 | 25 | #include "../repository.h"
|
26 | 26 | #include "win32/fscache.h"
|
27 | 27 | #include "../attr.h"
|
| 28 | +#include "../string-list.h" |
28 | 29 |
|
29 | 30 | #define HCAST(type, handle) ((type)(intptr_t)handle)
|
30 | 31 |
|
@@ -1667,6 +1668,65 @@ static char *lookup_prog(const char *dir, int dirlen, const char *cmd,
|
1667 | 1668 | return NULL;
|
1668 | 1669 | }
|
1669 | 1670 |
|
| 1671 | +static char *path_lookup(const char *cmd, int exe_only); |
| 1672 | + |
| 1673 | +static char *is_busybox_applet(const char *cmd) |
| 1674 | +{ |
| 1675 | + static struct string_list applets = STRING_LIST_INIT_DUP; |
| 1676 | + static char *busybox_path; |
| 1677 | + static int busybox_path_initialized; |
| 1678 | + |
| 1679 | + /* Avoid infinite loop */ |
| 1680 | + if (!strncasecmp(cmd, "busybox", 7) && |
| 1681 | + (!cmd[7] || !strcasecmp(cmd + 7, ".exe"))) |
| 1682 | + return NULL; |
| 1683 | + |
| 1684 | + if (!busybox_path_initialized) { |
| 1685 | + busybox_path = path_lookup("busybox.exe", 1); |
| 1686 | + busybox_path_initialized = 1; |
| 1687 | + } |
| 1688 | + |
| 1689 | + /* Assume that sh is compiled in... */ |
| 1690 | + if (!busybox_path || !strcasecmp(cmd, "sh")) |
| 1691 | + return xstrdup_or_null(busybox_path); |
| 1692 | + |
| 1693 | + if (!applets.nr) { |
| 1694 | + struct child_process cp = CHILD_PROCESS_INIT; |
| 1695 | + struct strbuf buf = STRBUF_INIT; |
| 1696 | + char *p; |
| 1697 | + |
| 1698 | + strvec_pushl(&cp.args, busybox_path, "--help", NULL); |
| 1699 | + |
| 1700 | + if (capture_command(&cp, &buf, 2048)) { |
| 1701 | + string_list_append(&applets, ""); |
| 1702 | + return NULL; |
| 1703 | + } |
| 1704 | + |
| 1705 | + /* parse output */ |
| 1706 | + p = strstr(buf.buf, "Currently defined functions:\n"); |
| 1707 | + if (!p) { |
| 1708 | + warning("Could not parse output of busybox --help"); |
| 1709 | + string_list_append(&applets, ""); |
| 1710 | + return NULL; |
| 1711 | + } |
| 1712 | + p = strchrnul(p, '\n'); |
| 1713 | + for (;;) { |
| 1714 | + size_t len; |
| 1715 | + |
| 1716 | + p += strspn(p, "\n\t ,"); |
| 1717 | + len = strcspn(p, "\n\t ,"); |
| 1718 | + if (!len) |
| 1719 | + break; |
| 1720 | + p[len] = '\0'; |
| 1721 | + string_list_insert(&applets, p); |
| 1722 | + p = p + len + 1; |
| 1723 | + } |
| 1724 | + } |
| 1725 | + |
| 1726 | + return string_list_has_string(&applets, cmd) ? |
| 1727 | + xstrdup(busybox_path) : NULL; |
| 1728 | +} |
| 1729 | + |
1670 | 1730 | /*
|
1671 | 1731 | * Determines the absolute path of cmd using the split path in path.
|
1672 | 1732 | * If cmd contains a slash or backslash, no lookup is performed.
|
@@ -1695,6 +1755,9 @@ static char *path_lookup(const char *cmd, int exe_only)
|
1695 | 1755 | path = sep + 1;
|
1696 | 1756 | }
|
1697 | 1757 |
|
| 1758 | + if (!prog && !isexe) |
| 1759 | + prog = is_busybox_applet(cmd); |
| 1760 | + |
1698 | 1761 | return prog;
|
1699 | 1762 | }
|
1700 | 1763 |
|
|
0 commit comments