tentative fix for issue 3 (ex 53)
[mplib] / src / texk / kpathsea / system.c
1 /* libc replacement functions for win32.
2
3 Copyright (C) 1998, 99 Free Software Foundation, Inc.
4
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.
9
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.
14
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.  */
18
19 /*
20   This does make sense only under WIN32.
21   Functions:
22     - win32_system() rewritten
23   */
24
25 #ifdef __MINGW32__
26
27 #include <kpathsea/config.h>
28 #include <kpathsea/c-proto.h>
29 #include <kpathsea/win32lib.h>
30 #include <kpathsea/lib.h>
31 #include <errno.h>
32
33 /*
34   It has been proven that system() fails to retrieve exit codes
35   under Win9x. This is a workaround for this bug.
36 */
37
38 int __cdecl win32_system(const char *cmd, int async)
39 {
40   STARTUPINFO si;
41   PROCESS_INFORMATION pi;
42   DWORD ret = 0;
43   HANDLE hIn, hOut, hPipeIn, hPipeOut;
44   SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
45   int i;
46
47   char  *app_name, *new_cmd;
48   char  *red_input, *red_output, ***cmd_pipe;
49
50   /* Reset errno ??? */
51   errno = 0;
52
53   /* Admittedly, the command interpreter will allways be found. */
54   if (! cmd) {
55     errno = 0;
56 #ifdef _TRACE
57     fprintf(stderr, "system: (null) command.\n");
58 #endif
59     return 1;
60   }
61
62   if (look_for_cmd(cmd, &app_name, &new_cmd) == FALSE) {
63     /* Failed to find the command or malformed cmd */
64     errno = ENOEXEC;
65 #ifdef _TRACE
66     fprintf(stderr, "system: failed to find command.\n");
67 #endif
68     return -1;
69   }
70
71   cmd_pipe = parse_cmdline(new_cmd, &red_input, &red_output);
72
73   for (i = 0; cmd_pipe[i]; i++) {
74
75     /* free the cmd and build the current one */
76     if (new_cmd) free(new_cmd);
77
78     new_cmd = build_cmdline(&cmd_pipe[i], NULL, NULL);
79
80     /* First time, use red_input if available */
81     if (i == 0) {
82       if (red_input) {
83         hIn = CreateFile(red_input,
84                          GENERIC_READ,
85                          FILE_SHARE_READ | FILE_SHARE_WRITE,
86                          &sa,
87                          OPEN_EXISTING,
88                          FILE_ATTRIBUTE_NORMAL,
89                          NULL);
90         if (hIn == INVALID_HANDLE_VALUE) {
91 #ifdef _TRACE
92           fprintf(stderr, "system: failed to open hIn (%s) with error %d.\n", red_input, GetLastError());
93 #endif
94           errno = EIO;
95           return -1;
96         }
97       }
98       else {
99         hIn = GetStdHandle(STD_INPUT_HANDLE);
100       }
101     }
102     /* Last time, use red_output if available */
103     if (cmd_pipe[i+1] == NULL) {
104       if (red_output) {
105         hOut = CreateFile(red_output,
106                           GENERIC_WRITE,
107                           FILE_SHARE_READ | FILE_SHARE_WRITE,
108                           &sa,
109                           OPEN_ALWAYS,
110                           FILE_ATTRIBUTE_NORMAL,
111                           NULL);
112         if (hOut == INVALID_HANDLE_VALUE) {
113 #ifdef _TRACE
114           fprintf(stderr, "system: failed to open hOut (%s) with error %d.\n", red_output, GetLastError());
115 #endif
116           errno = EIO;
117           return -1;
118         }
119       }
120       else {
121         hOut = GetStdHandle(STD_OUTPUT_HANDLE);
122       }
123     }
124
125 #if 0
126     /* FIXME : implement pipes !!! */
127 #endif
128
129     ZeroMemory( &si, sizeof(STARTUPINFO) );
130     si.cb = sizeof(STARTUPINFO);
131     si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
132     si.wShowWindow = SW_SHOW;
133     si.hStdInput = hIn;
134     si.hStdOutput = hOut;
135     si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
136
137 #ifdef _TRACE
138     fprintf(stderr, "Executing: %s\n", new_cmd);
139 #endif
140     if (CreateProcess(app_name,
141                       new_cmd,
142                       NULL,
143                       NULL,
144                       TRUE,
145                       0,
146                       NULL,
147                       NULL,
148                       &si,
149                       &pi) == 0) {
150       fprintf(stderr, "win32_system(%s) call failed (Error %d).\n", cmd, GetLastError());
151       return -1;
152     }
153     
154     /* Only the process handle is needed */
155     CloseHandle(pi.hThread);
156     
157     if (async == 0) {
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());
161           ret = -1;
162         }
163       }
164       else {
165         fprintf(stderr, "Failed to wait for process termination: %s (Error %d)\n", cmd, GetLastError());
166         ret = -1;
167       }
168     }
169
170     CloseHandle(pi.hProcess);
171
172     if (red_input) CloseHandle(hIn);
173     if (red_output) CloseHandle(hOut);
174   }
175
176   if (new_cmd) free(new_cmd);
177   if (app_name) free(app_name);
178     
179   return ret;
180 }
181
182 #endif