-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbusybox-w32.patch
90 lines (86 loc) · 2.74 KB
/
busybox-w32.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
diff --git a/src/w32/subproc/sub_proc.c b/src/w32/subproc/sub_proc.c
index 4331973a..eace0150 100644
--- a/src/w32/subproc/sub_proc.c
+++ b/src/w32/subproc/sub_proc.c
@@ -546,6 +546,58 @@ batch_file_with_spaces(const char *fname)
&& strpbrk(fname, " \t") != NULL);
}
+char*
+find_busybox_w32(const char* path_var)
+{
+ const static char busybox[] = "busybox.exe";
+
+ char* fullname = xmalloc(MAX_PATH);
+ HANDLE handle = find_file(busybox, path_var,
+ fullname, MAX_PATH);
+
+ return handle != INVALID_HANDLE_VALUE ? fullname : NULL;
+}
+
+int
+busybox_w32_has_command(const char* busybox, const char* cmd)
+{
+ int found = 0;
+
+ char* query = NULL;
+ if (asprintf(&query, "%s %s", busybox, "--list") == -1)
+ return 0;
+
+ FILE* list = popen(query, "r");
+ if (list) {
+ char line[81];
+ while (fgets(line, sizeof(line), list)) {
+ line[strcspn(line, "\n")] = 0;
+
+ if (!strncmp(line, cmd, sizeof(line))) {
+ found = 1;
+ break;
+ }
+ }
+ pclose(list);
+ }
+
+ free(query);
+ return found;
+}
+
+char**
+copy_and_shift_argv(const char** argv, size_t shift_by)
+{
+ // argv is terminated by a NULL element.
+ size_t argv_elems = 0;
+ while (argv[argv_elems++]);
+
+ size_t argv_size = argv_elems * sizeof(char*);
+ char** new_argv = xcalloc(shift_by * sizeof(char*) + argv_size);
+ memcpy(new_argv + shift_by, argv, argv_size);
+
+ return new_argv;
+}
/*
* Description: Create the child process to be helped
@@ -686,9 +738,24 @@ process_begin(
flags = 0;
- if (file_not_found)
+ if (file_not_found) {
+ char* busybox = find_busybox_w32(path_var);
+ int from_busybox = busybox &&
+ busybox_w32_has_command(busybox, exec_path);
+
+ if (from_busybox) {
+ // The original argv is freed higher in the stack.
+ argv = copy_and_shift_argv(argv, 1);
+ exec_path = argv[0] = busybox;
+ }
+
command_line = make_command_line( shell_name, exec_path, argv);
- else {
+
+ if (from_busybox) {
+ free(argv);
+ free(busybox);
+ }
+ } else {
/* If exec_fname includes whitespace, CreateProcess
behaves erratically and unreliably, and often fails
if argv[0] also includes whitespace (and thus will