|
12 | 12 | #include "repository.h"
|
13 | 13 | #include "run-command.h"
|
14 | 14 | #include "strbuf.h"
|
| 15 | +#include "string-list.h" |
15 | 16 | #include "symlinks.h"
|
16 | 17 | #include "trace2.h"
|
17 | 18 | #include "win32.h"
|
@@ -1759,6 +1760,65 @@ static char *lookup_prog(const char *dir, int dirlen, const char *cmd,
|
1759 | 1760 | return NULL;
|
1760 | 1761 | }
|
1761 | 1762 |
|
| 1763 | +static char *path_lookup(const char *cmd, int exe_only); |
| 1764 | + |
| 1765 | +static char *is_busybox_applet(const char *cmd) |
| 1766 | +{ |
| 1767 | + static struct string_list applets = STRING_LIST_INIT_DUP; |
| 1768 | + static char *busybox_path; |
| 1769 | + static int busybox_path_initialized; |
| 1770 | + |
| 1771 | + /* Avoid infinite loop */ |
| 1772 | + if (!strncasecmp(cmd, "busybox", 7) && |
| 1773 | + (!cmd[7] || !strcasecmp(cmd + 7, ".exe"))) |
| 1774 | + return NULL; |
| 1775 | + |
| 1776 | + if (!busybox_path_initialized) { |
| 1777 | + busybox_path = path_lookup("busybox.exe", 1); |
| 1778 | + busybox_path_initialized = 1; |
| 1779 | + } |
| 1780 | + |
| 1781 | + /* Assume that sh is compiled in... */ |
| 1782 | + if (!busybox_path || !strcasecmp(cmd, "sh")) |
| 1783 | + return xstrdup_or_null(busybox_path); |
| 1784 | + |
| 1785 | + if (!applets.nr) { |
| 1786 | + struct child_process cp = CHILD_PROCESS_INIT; |
| 1787 | + struct strbuf buf = STRBUF_INIT; |
| 1788 | + char *p; |
| 1789 | + |
| 1790 | + strvec_pushl(&cp.args, busybox_path, "--help", NULL); |
| 1791 | + |
| 1792 | + if (capture_command(&cp, &buf, 2048)) { |
| 1793 | + string_list_append(&applets, ""); |
| 1794 | + return NULL; |
| 1795 | + } |
| 1796 | + |
| 1797 | + /* parse output */ |
| 1798 | + p = strstr(buf.buf, "Currently defined functions:\n"); |
| 1799 | + if (!p) { |
| 1800 | + warning("Could not parse output of busybox --help"); |
| 1801 | + string_list_append(&applets, ""); |
| 1802 | + return NULL; |
| 1803 | + } |
| 1804 | + p = strchrnul(p, '\n'); |
| 1805 | + for (;;) { |
| 1806 | + size_t len; |
| 1807 | + |
| 1808 | + p += strspn(p, "\n\t ,"); |
| 1809 | + len = strcspn(p, "\n\t ,"); |
| 1810 | + if (!len) |
| 1811 | + break; |
| 1812 | + p[len] = '\0'; |
| 1813 | + string_list_insert(&applets, p); |
| 1814 | + p = p + len + 1; |
| 1815 | + } |
| 1816 | + } |
| 1817 | + |
| 1818 | + return string_list_has_string(&applets, cmd) ? |
| 1819 | + xstrdup(busybox_path) : NULL; |
| 1820 | +} |
| 1821 | + |
1762 | 1822 | /*
|
1763 | 1823 | * Determines the absolute path of cmd using the split path in path.
|
1764 | 1824 | * If cmd contains a slash or backslash, no lookup is performed.
|
@@ -1787,6 +1847,9 @@ static char *path_lookup(const char *cmd, int exe_only)
|
1787 | 1847 | path = sep + 1;
|
1788 | 1848 | }
|
1789 | 1849 |
|
| 1850 | + if (!prog && !isexe) |
| 1851 | + prog = is_busybox_applet(cmd); |
| 1852 | + |
1790 | 1853 | return prog;
|
1791 | 1854 | }
|
1792 | 1855 |
|
|
0 commit comments