|
10 | 10 | #include "../config.h"
|
11 | 11 | #include "dir.h"
|
12 | 12 | #include "../attr.h"
|
| 13 | +#include "../string-list.h" |
13 | 14 |
|
14 | 15 | #define HCAST(type, handle) ((type)(intptr_t)handle)
|
15 | 16 |
|
@@ -1387,6 +1388,65 @@ static char *lookup_prog(const char *dir, int dirlen, const char *cmd,
|
1387 | 1388 | return NULL;
|
1388 | 1389 | }
|
1389 | 1390 |
|
| 1391 | +static char *path_lookup(const char *cmd, int exe_only); |
| 1392 | + |
| 1393 | +static char *is_busybox_applet(const char *cmd) |
| 1394 | +{ |
| 1395 | + static struct string_list applets = STRING_LIST_INIT_DUP; |
| 1396 | + static char *busybox_path; |
| 1397 | + static int busybox_path_initialized; |
| 1398 | + |
| 1399 | + /* Avoid infinite loop */ |
| 1400 | + if (!strncasecmp(cmd, "busybox", 7) && |
| 1401 | + (!cmd[7] || !strcasecmp(cmd + 7, ".exe"))) |
| 1402 | + return NULL; |
| 1403 | + |
| 1404 | + if (!busybox_path_initialized) { |
| 1405 | + busybox_path = path_lookup("busybox.exe", 1); |
| 1406 | + busybox_path_initialized = 1; |
| 1407 | + } |
| 1408 | + |
| 1409 | + /* Assume that sh is compiled in... */ |
| 1410 | + if (!busybox_path || !strcasecmp(cmd, "sh")) |
| 1411 | + return xstrdup_or_null(busybox_path); |
| 1412 | + |
| 1413 | + if (!applets.nr) { |
| 1414 | + struct child_process cp = CHILD_PROCESS_INIT; |
| 1415 | + struct strbuf buf = STRBUF_INIT; |
| 1416 | + char *p; |
| 1417 | + |
| 1418 | + argv_array_pushl(&cp.args, busybox_path, "--help", NULL); |
| 1419 | + |
| 1420 | + if (capture_command(&cp, &buf, 2048)) { |
| 1421 | + string_list_append(&applets, ""); |
| 1422 | + return NULL; |
| 1423 | + } |
| 1424 | + |
| 1425 | + /* parse output */ |
| 1426 | + p = strstr(buf.buf, "Currently defined functions:\n"); |
| 1427 | + if (!p) { |
| 1428 | + warning("Could not parse output of busybox --help"); |
| 1429 | + string_list_append(&applets, ""); |
| 1430 | + return NULL; |
| 1431 | + } |
| 1432 | + p = strchrnul(p, '\n'); |
| 1433 | + for (;;) { |
| 1434 | + size_t len; |
| 1435 | + |
| 1436 | + p += strspn(p, "\n\t ,"); |
| 1437 | + len = strcspn(p, "\n\t ,"); |
| 1438 | + if (!len) |
| 1439 | + break; |
| 1440 | + p[len] = '\0'; |
| 1441 | + string_list_insert(&applets, p); |
| 1442 | + p = p + len + 1; |
| 1443 | + } |
| 1444 | + } |
| 1445 | + |
| 1446 | + return string_list_has_string(&applets, cmd) ? |
| 1447 | + xstrdup(busybox_path) : NULL; |
| 1448 | +} |
| 1449 | + |
1390 | 1450 | /*
|
1391 | 1451 | * Determines the absolute path of cmd using the split path in path.
|
1392 | 1452 | * If cmd contains a slash or backslash, no lookup is performed.
|
@@ -1415,6 +1475,9 @@ static char *path_lookup(const char *cmd, int exe_only)
|
1415 | 1475 | path = sep + 1;
|
1416 | 1476 | }
|
1417 | 1477 |
|
| 1478 | + if (!prog && !isexe) |
| 1479 | + prog = is_busybox_applet(cmd); |
| 1480 | + |
1418 | 1481 | return prog;
|
1419 | 1482 | }
|
1420 | 1483 |
|
|
0 commit comments