1 /* libc replacement functions for win32.
3 Copyright (C) 1998, 99 Free Software Foundation, Inc.
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 This does make sense only under WIN32.
22 - win32_system() rewritten
27 #include <kpathsea/config.h>
28 #include <kpathsea/c-proto.h>
29 #include <kpathsea/win32lib.h>
30 #include <kpathsea/lib.h>
34 It has been proven that system() fails to retrieve exit codes
35 under Win9x. This is a workaround for this bug.
38 int __cdecl win32_system(const char *cmd, int async)
41 PROCESS_INFORMATION pi;
43 HANDLE hIn, hOut, hPipeIn, hPipeOut;
44 SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
47 char *app_name, *new_cmd;
48 char *red_input, *red_output, ***cmd_pipe;
53 /* Admittedly, the command interpreter will allways be found. */
57 fprintf(stderr, "system: (null) command.\n");
62 if (look_for_cmd(cmd, &app_name, &new_cmd) == FALSE) {
63 /* Failed to find the command or malformed cmd */
66 fprintf(stderr, "system: failed to find command.\n");
71 cmd_pipe = parse_cmdline(new_cmd, &red_input, &red_output);
73 for (i = 0; cmd_pipe[i]; i++) {
75 /* free the cmd and build the current one */
76 if (new_cmd) free(new_cmd);
78 new_cmd = build_cmdline(&cmd_pipe[i], NULL, NULL);
80 /* First time, use red_input if available */
83 hIn = CreateFile(red_input,
85 FILE_SHARE_READ | FILE_SHARE_WRITE,
88 FILE_ATTRIBUTE_NORMAL,
90 if (hIn == INVALID_HANDLE_VALUE) {
92 fprintf(stderr, "system: failed to open hIn (%s) with error %d.\n", red_input, GetLastError());
99 hIn = GetStdHandle(STD_INPUT_HANDLE);
102 /* Last time, use red_output if available */
103 if (cmd_pipe[i+1] == NULL) {
105 hOut = CreateFile(red_output,
107 FILE_SHARE_READ | FILE_SHARE_WRITE,
110 FILE_ATTRIBUTE_NORMAL,
112 if (hOut == INVALID_HANDLE_VALUE) {
114 fprintf(stderr, "system: failed to open hOut (%s) with error %d.\n", red_output, GetLastError());
121 hOut = GetStdHandle(STD_OUTPUT_HANDLE);
126 /* FIXME : implement pipes !!! */
129 ZeroMemory( &si, sizeof(STARTUPINFO) );
130 si.cb = sizeof(STARTUPINFO);
131 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
132 si.wShowWindow = SW_SHOW;
134 si.hStdOutput = hOut;
135 si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
138 fprintf(stderr, "Executing: %s\n", new_cmd);
140 if (CreateProcess(app_name,
150 fprintf(stderr, "win32_system(%s) call failed (Error %d).\n", cmd, GetLastError());
154 /* Only the process handle is needed */
155 CloseHandle(pi.hThread);
158 if (WaitForSingleObject(pi.hProcess, INFINITE) == WAIT_OBJECT_0) {
159 if (GetExitCodeProcess(pi.hProcess, &ret) == 0) {
160 fprintf(stderr, "Failed to retrieve exit code: %s (Error %d)\n", cmd, GetLastError());
165 fprintf(stderr, "Failed to wait for process termination: %s (Error %d)\n", cmd, GetLastError());
170 CloseHandle(pi.hProcess);
172 if (red_input) CloseHandle(hIn);
173 if (red_output) CloseHandle(hOut);
176 if (new_cmd) free(new_cmd);
177 if (app_name) free(app_name);