|
14 | 14 | #include <sspi.h>
|
15 | 15 | #include "win32/fscache.h"
|
16 | 16 | #include "../attr.h"
|
| 17 | +#include "../string-list.h" |
17 | 18 |
|
18 | 19 | #define HCAST(type, handle) ((type)(intptr_t)handle)
|
19 | 20 |
|
@@ -1570,6 +1571,65 @@ static char *lookup_prog(const char *dir, int dirlen, const char *cmd,
|
1570 | 1571 | return NULL;
|
1571 | 1572 | }
|
1572 | 1573 |
|
| 1574 | +static char *path_lookup(const char *cmd, int exe_only); |
| 1575 | + |
| 1576 | +static char *is_busybox_applet(const char *cmd) |
| 1577 | +{ |
| 1578 | + static struct string_list applets = STRING_LIST_INIT_DUP; |
| 1579 | + static char *busybox_path; |
| 1580 | + static int busybox_path_initialized; |
| 1581 | + |
| 1582 | + /* Avoid infinite loop */ |
| 1583 | + if (!strncasecmp(cmd, "busybox", 7) && |
| 1584 | + (!cmd[7] || !strcasecmp(cmd + 7, ".exe"))) |
| 1585 | + return NULL; |
| 1586 | + |
| 1587 | + if (!busybox_path_initialized) { |
| 1588 | + busybox_path = path_lookup("busybox.exe", 1); |
| 1589 | + busybox_path_initialized = 1; |
| 1590 | + } |
| 1591 | + |
| 1592 | + /* Assume that sh is compiled in... */ |
| 1593 | + if (!busybox_path || !strcasecmp(cmd, "sh")) |
| 1594 | + return xstrdup_or_null(busybox_path); |
| 1595 | + |
| 1596 | + if (!applets.nr) { |
| 1597 | + struct child_process cp = CHILD_PROCESS_INIT; |
| 1598 | + struct strbuf buf = STRBUF_INIT; |
| 1599 | + char *p; |
| 1600 | + |
| 1601 | + strvec_pushl(&cp.args, busybox_path, "--help", NULL); |
| 1602 | + |
| 1603 | + if (capture_command(&cp, &buf, 2048)) { |
| 1604 | + string_list_append(&applets, ""); |
| 1605 | + return NULL; |
| 1606 | + } |
| 1607 | + |
| 1608 | + /* parse output */ |
| 1609 | + p = strstr(buf.buf, "Currently defined functions:\n"); |
| 1610 | + if (!p) { |
| 1611 | + warning("Could not parse output of busybox --help"); |
| 1612 | + string_list_append(&applets, ""); |
| 1613 | + return NULL; |
| 1614 | + } |
| 1615 | + p = strchrnul(p, '\n'); |
| 1616 | + for (;;) { |
| 1617 | + size_t len; |
| 1618 | + |
| 1619 | + p += strspn(p, "\n\t ,"); |
| 1620 | + len = strcspn(p, "\n\t ,"); |
| 1621 | + if (!len) |
| 1622 | + break; |
| 1623 | + p[len] = '\0'; |
| 1624 | + string_list_insert(&applets, p); |
| 1625 | + p = p + len + 1; |
| 1626 | + } |
| 1627 | + } |
| 1628 | + |
| 1629 | + return string_list_has_string(&applets, cmd) ? |
| 1630 | + xstrdup(busybox_path) : NULL; |
| 1631 | +} |
| 1632 | + |
1573 | 1633 | /*
|
1574 | 1634 | * Determines the absolute path of cmd using the split path in path.
|
1575 | 1635 | * If cmd contains a slash or backslash, no lookup is performed.
|
@@ -1598,6 +1658,9 @@ static char *path_lookup(const char *cmd, int exe_only)
|
1598 | 1658 | path = sep + 1;
|
1599 | 1659 | }
|
1600 | 1660 |
|
| 1661 | + if (!prog && !isexe) |
| 1662 | + prog = is_busybox_applet(cmd); |
| 1663 | + |
1601 | 1664 | return prog;
|
1602 | 1665 | }
|
1603 | 1666 |
|
|
0 commit comments