|
26 | 26 | #include "../repository.h"
|
27 | 27 | #include "win32/fscache.h"
|
28 | 28 | #include "../attr.h"
|
| 29 | +#include "../string-list.h" |
29 | 30 |
|
30 | 31 | #define HCAST(type, handle) ((type)(intptr_t)handle)
|
31 | 32 |
|
@@ -1746,6 +1747,65 @@ static char *lookup_prog(const char *dir, int dirlen, const char *cmd,
|
1746 | 1747 | return NULL;
|
1747 | 1748 | }
|
1748 | 1749 |
|
| 1750 | +static char *path_lookup(const char *cmd, int exe_only); |
| 1751 | + |
| 1752 | +static char *is_busybox_applet(const char *cmd) |
| 1753 | +{ |
| 1754 | + static struct string_list applets = STRING_LIST_INIT_DUP; |
| 1755 | + static char *busybox_path; |
| 1756 | + static int busybox_path_initialized; |
| 1757 | + |
| 1758 | + /* Avoid infinite loop */ |
| 1759 | + if (!strncasecmp(cmd, "busybox", 7) && |
| 1760 | + (!cmd[7] || !strcasecmp(cmd + 7, ".exe"))) |
| 1761 | + return NULL; |
| 1762 | + |
| 1763 | + if (!busybox_path_initialized) { |
| 1764 | + busybox_path = path_lookup("busybox.exe", 1); |
| 1765 | + busybox_path_initialized = 1; |
| 1766 | + } |
| 1767 | + |
| 1768 | + /* Assume that sh is compiled in... */ |
| 1769 | + if (!busybox_path || !strcasecmp(cmd, "sh")) |
| 1770 | + return xstrdup_or_null(busybox_path); |
| 1771 | + |
| 1772 | + if (!applets.nr) { |
| 1773 | + struct child_process cp = CHILD_PROCESS_INIT; |
| 1774 | + struct strbuf buf = STRBUF_INIT; |
| 1775 | + char *p; |
| 1776 | + |
| 1777 | + strvec_pushl(&cp.args, busybox_path, "--help", NULL); |
| 1778 | + |
| 1779 | + if (capture_command(&cp, &buf, 2048)) { |
| 1780 | + string_list_append(&applets, ""); |
| 1781 | + return NULL; |
| 1782 | + } |
| 1783 | + |
| 1784 | + /* parse output */ |
| 1785 | + p = strstr(buf.buf, "Currently defined functions:\n"); |
| 1786 | + if (!p) { |
| 1787 | + warning("Could not parse output of busybox --help"); |
| 1788 | + string_list_append(&applets, ""); |
| 1789 | + return NULL; |
| 1790 | + } |
| 1791 | + p = strchrnul(p, '\n'); |
| 1792 | + for (;;) { |
| 1793 | + size_t len; |
| 1794 | + |
| 1795 | + p += strspn(p, "\n\t ,"); |
| 1796 | + len = strcspn(p, "\n\t ,"); |
| 1797 | + if (!len) |
| 1798 | + break; |
| 1799 | + p[len] = '\0'; |
| 1800 | + string_list_insert(&applets, p); |
| 1801 | + p = p + len + 1; |
| 1802 | + } |
| 1803 | + } |
| 1804 | + |
| 1805 | + return string_list_has_string(&applets, cmd) ? |
| 1806 | + xstrdup(busybox_path) : NULL; |
| 1807 | +} |
| 1808 | + |
1749 | 1809 | /*
|
1750 | 1810 | * Determines the absolute path of cmd using the split path in path.
|
1751 | 1811 | * If cmd contains a slash or backslash, no lookup is performed.
|
@@ -1774,6 +1834,9 @@ static char *path_lookup(const char *cmd, int exe_only)
|
1774 | 1834 | path = sep + 1;
|
1775 | 1835 | }
|
1776 | 1836 |
|
| 1837 | + if (!prog && !isexe) |
| 1838 | + prog = is_busybox_applet(cmd); |
| 1839 | + |
1777 | 1840 | return prog;
|
1778 | 1841 | }
|
1779 | 1842 |
|
|
0 commit comments