diff options
author | Ron Yorston <rmy@pobox.com> | 2017-07-31 20:04:07 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2017-07-31 20:04:07 +0100 |
commit | 3269a81ce20fcdc4bfb196f45585a3c632896f05 (patch) | |
tree | 532c1296e5932b73bcfc933e7448f4883a268001 | |
parent | b0d1b94b6fffc2d472bc4a6f0c70104b6b16f097 (diff) | |
download | busybox-w32-3269a81ce20fcdc4bfb196f45585a3c632896f05.tar.gz busybox-w32-3269a81ce20fcdc4bfb196f45585a3c632896f05.tar.bz2 busybox-w32-3269a81ce20fcdc4bfb196f45585a3c632896f05.zip |
win32: rewrite implementation of system(3)
Rewrite mingw_system using mingw_spawn_proc. Also fix return values.
-rw-r--r-- | win32/system.c | 77 |
1 files changed, 11 insertions, 66 deletions
diff --git a/win32/system.c b/win32/system.c index c3e2c316e..02aaaa0a1 100644 --- a/win32/system.c +++ b/win32/system.c | |||
@@ -2,76 +2,21 @@ | |||
2 | 2 | ||
3 | int mingw_system(const char *cmd) | 3 | int mingw_system(const char *cmd) |
4 | { | 4 | { |
5 | STARTUPINFO siStartInfo; | 5 | const char *argv[4] = { "sh", "-c", cmd, NULL }; |
6 | PROCESS_INFORMATION piProcInfo; | 6 | intptr_t proc; |
7 | int success; | 7 | HANDLE h; |
8 | int len, count; | 8 | DWORD ret = 0; |
9 | char *cmd_buff = NULL; | ||
10 | const char *s; | ||
11 | char *t; | ||
12 | DWORD ret; | ||
13 | 9 | ||
14 | if ( cmd == NULL ) { | 10 | if (cmd == NULL) |
15 | return 1; | 11 | return 1; |
16 | } | ||
17 | 12 | ||
18 | /* count double quotes */ | 13 | if ((proc=mingw_spawn_proc((char **)argv)) == -1) |
19 | count = 0; | ||
20 | for ( s=cmd; *s; ++s ) { | ||
21 | if ( *s == '"' ) { | ||
22 | ++count; | ||
23 | } | ||
24 | } | ||
25 | |||
26 | len = strlen(cmd) + 10 + count; | ||
27 | if ( (cmd_buff=malloc(len)) == NULL ) { | ||
28 | return -1; | 14 | return -1; |
29 | } | ||
30 | |||
31 | /* escape double quotes */ | ||
32 | strcpy(cmd_buff, "sh -c \""); | ||
33 | for ( s=cmd,t=cmd_buff+strlen(cmd_buff); *s; ++s ) { | ||
34 | if ( *s == '"' ) { | ||
35 | *t++ = '\\'; | ||
36 | } | ||
37 | *t++ = *s; | ||
38 | } | ||
39 | *t++ = '"'; | ||
40 | *t = '\0'; | ||
41 | |||
42 | /* Now create the child process */ | ||
43 | ZeroMemory(&siStartInfo, sizeof(STARTUPINFO)); | ||
44 | siStartInfo.cb = sizeof(STARTUPINFO); | ||
45 | siStartInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE); | ||
46 | siStartInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); | ||
47 | siStartInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE); | ||
48 | siStartInfo.dwFlags = STARTF_USESTDHANDLES; | ||
49 | |||
50 | success = CreateProcess(NULL, | ||
51 | (LPTSTR)cmd_buff, /* command line */ | ||
52 | NULL, /* process security attributes */ | ||
53 | NULL, /* primary thread security attributes */ | ||
54 | TRUE, /* handles are inherited */ | ||
55 | 0, /* creation flags */ | ||
56 | NULL, /* use parent's environment */ | ||
57 | NULL, /* use parent's current directory */ | ||
58 | &siStartInfo, /* STARTUPINFO pointer */ | ||
59 | &piProcInfo); /* receives PROCESS_INFORMATION */ | ||
60 | |||
61 | if ( !success ) { | ||
62 | free(cmd_buff); | ||
63 | return 127; | ||
64 | } | ||
65 | |||
66 | free(cmd_buff); | ||
67 | |||
68 | WaitForSingleObject(piProcInfo.hProcess, INFINITE); | ||
69 | |||
70 | ret = 0; | ||
71 | GetExitCodeProcess(piProcInfo.hProcess, &ret); | ||
72 | 15 | ||
73 | CloseHandle(piProcInfo.hProcess); | 16 | h = (HANDLE)proc; |
74 | CloseHandle(piProcInfo.hThread); | 17 | WaitForSingleObject(h, INFINITE); |
18 | GetExitCodeProcess(h, &ret); | ||
19 | CloseHandle(h); | ||
75 | 20 | ||
76 | return ret; | 21 | return ret << 8; |
77 | } | 22 | } |