|
11 | 11 | #include "dir.h"
|
12 | 12 | #include "win32/fscache.h"
|
13 | 13 | #include "../attr.h"
|
| 14 | +#include "../string-list.h" |
14 | 15 |
|
15 | 16 | #define HCAST(type, handle) ((type)(intptr_t)handle)
|
16 | 17 |
|
@@ -1437,6 +1438,65 @@ static char *lookup_prog(const char *dir, int dirlen, const char *cmd,
|
1437 | 1438 | return NULL;
|
1438 | 1439 | }
|
1439 | 1440 |
|
| 1441 | +static char *path_lookup(const char *cmd, int exe_only); |
| 1442 | + |
| 1443 | +static char *is_busybox_applet(const char *cmd) |
| 1444 | +{ |
| 1445 | + static struct string_list applets = STRING_LIST_INIT_DUP; |
| 1446 | + static char *busybox_path; |
| 1447 | + static int busybox_path_initialized; |
| 1448 | + |
| 1449 | + /* Avoid infinite loop */ |
| 1450 | + if (!strncasecmp(cmd, "busybox", 7) && |
| 1451 | + (!cmd[7] || !strcasecmp(cmd + 7, ".exe"))) |
| 1452 | + return NULL; |
| 1453 | + |
| 1454 | + if (!busybox_path_initialized) { |
| 1455 | + busybox_path = path_lookup("busybox.exe", 1); |
| 1456 | + busybox_path_initialized = 1; |
| 1457 | + } |
| 1458 | + |
| 1459 | + /* Assume that sh is compiled in... */ |
| 1460 | + if (!busybox_path || !strcasecmp(cmd, "sh")) |
| 1461 | + return xstrdup_or_null(busybox_path); |
| 1462 | + |
| 1463 | + if (!applets.nr) { |
| 1464 | + struct child_process cp = CHILD_PROCESS_INIT; |
| 1465 | + struct strbuf buf = STRBUF_INIT; |
| 1466 | + char *p; |
| 1467 | + |
| 1468 | + argv_array_pushl(&cp.args, busybox_path, "--help", NULL); |
| 1469 | + |
| 1470 | + if (capture_command(&cp, &buf, 2048)) { |
| 1471 | + string_list_append(&applets, ""); |
| 1472 | + return NULL; |
| 1473 | + } |
| 1474 | + |
| 1475 | + /* parse output */ |
| 1476 | + p = strstr(buf.buf, "Currently defined functions:\n"); |
| 1477 | + if (!p) { |
| 1478 | + warning("Could not parse output of busybox --help"); |
| 1479 | + string_list_append(&applets, ""); |
| 1480 | + return NULL; |
| 1481 | + } |
| 1482 | + p = strchrnul(p, '\n'); |
| 1483 | + for (;;) { |
| 1484 | + size_t len; |
| 1485 | + |
| 1486 | + p += strspn(p, "\n\t ,"); |
| 1487 | + len = strcspn(p, "\n\t ,"); |
| 1488 | + if (!len) |
| 1489 | + break; |
| 1490 | + p[len] = '\0'; |
| 1491 | + string_list_insert(&applets, p); |
| 1492 | + p = p + len + 1; |
| 1493 | + } |
| 1494 | + } |
| 1495 | + |
| 1496 | + return string_list_has_string(&applets, cmd) ? |
| 1497 | + xstrdup(busybox_path) : NULL; |
| 1498 | +} |
| 1499 | + |
1440 | 1500 | /*
|
1441 | 1501 | * Determines the absolute path of cmd using the split path in path.
|
1442 | 1502 | * If cmd contains a slash or backslash, no lookup is performed.
|
@@ -1465,6 +1525,9 @@ static char *path_lookup(const char *cmd, int exe_only)
|
1465 | 1525 | path = sep + 1;
|
1466 | 1526 | }
|
1467 | 1527 |
|
| 1528 | + if (!prog && !isexe) |
| 1529 | + prog = is_busybox_applet(cmd); |
| 1530 | + |
1468 | 1531 | return prog;
|
1469 | 1532 | }
|
1470 | 1533 |
|
|
0 commit comments