|
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 |
|
@@ -1734,6 +1735,65 @@ static char *lookup_prog(const char *dir, int dirlen, const char *cmd,
|
1734 | 1735 | return NULL;
|
1735 | 1736 | }
|
1736 | 1737 |
|
| 1738 | +static char *path_lookup(const char *cmd, int exe_only); |
| 1739 | + |
| 1740 | +static char *is_busybox_applet(const char *cmd) |
| 1741 | +{ |
| 1742 | + static struct string_list applets = STRING_LIST_INIT_DUP; |
| 1743 | + static char *busybox_path; |
| 1744 | + static int busybox_path_initialized; |
| 1745 | + |
| 1746 | + /* Avoid infinite loop */ |
| 1747 | + if (!strncasecmp(cmd, "busybox", 7) && |
| 1748 | + (!cmd[7] || !strcasecmp(cmd + 7, ".exe"))) |
| 1749 | + return NULL; |
| 1750 | + |
| 1751 | + if (!busybox_path_initialized) { |
| 1752 | + busybox_path = path_lookup("busybox.exe", 1); |
| 1753 | + busybox_path_initialized = 1; |
| 1754 | + } |
| 1755 | + |
| 1756 | + /* Assume that sh is compiled in... */ |
| 1757 | + if (!busybox_path || !strcasecmp(cmd, "sh")) |
| 1758 | + return xstrdup_or_null(busybox_path); |
| 1759 | + |
| 1760 | + if (!applets.nr) { |
| 1761 | + struct child_process cp = CHILD_PROCESS_INIT; |
| 1762 | + struct strbuf buf = STRBUF_INIT; |
| 1763 | + char *p; |
| 1764 | + |
| 1765 | + strvec_pushl(&cp.args, busybox_path, "--help", NULL); |
| 1766 | + |
| 1767 | + if (capture_command(&cp, &buf, 2048)) { |
| 1768 | + string_list_append(&applets, ""); |
| 1769 | + return NULL; |
| 1770 | + } |
| 1771 | + |
| 1772 | + /* parse output */ |
| 1773 | + p = strstr(buf.buf, "Currently defined functions:\n"); |
| 1774 | + if (!p) { |
| 1775 | + warning("Could not parse output of busybox --help"); |
| 1776 | + string_list_append(&applets, ""); |
| 1777 | + return NULL; |
| 1778 | + } |
| 1779 | + p = strchrnul(p, '\n'); |
| 1780 | + for (;;) { |
| 1781 | + size_t len; |
| 1782 | + |
| 1783 | + p += strspn(p, "\n\t ,"); |
| 1784 | + len = strcspn(p, "\n\t ,"); |
| 1785 | + if (!len) |
| 1786 | + break; |
| 1787 | + p[len] = '\0'; |
| 1788 | + string_list_insert(&applets, p); |
| 1789 | + p = p + len + 1; |
| 1790 | + } |
| 1791 | + } |
| 1792 | + |
| 1793 | + return string_list_has_string(&applets, cmd) ? |
| 1794 | + xstrdup(busybox_path) : NULL; |
| 1795 | +} |
| 1796 | + |
1737 | 1797 | /*
|
1738 | 1798 | * Determines the absolute path of cmd using the split path in path.
|
1739 | 1799 | * If cmd contains a slash or backslash, no lookup is performed.
|
@@ -1762,6 +1822,9 @@ static char *path_lookup(const char *cmd, int exe_only)
|
1762 | 1822 | path = sep + 1;
|
1763 | 1823 | }
|
1764 | 1824 |
|
| 1825 | + if (!prog && !isexe) |
| 1826 | + prog = is_busybox_applet(cmd); |
| 1827 | + |
1765 | 1828 | return prog;
|
1766 | 1829 | }
|
1767 | 1830 |
|
|
0 commit comments