12e82054bSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
32e82054bSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
42e82054bSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
52e82054bSAndrew Rist  * distributed with this work for additional information
62e82054bSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
72e82054bSAndrew Rist  * to you under the Apache License, Version 2.0 (the
82e82054bSAndrew Rist  * "License"); you may not use this file except in compliance
92e82054bSAndrew Rist  * with the License.  You may obtain a copy of the License at
102e82054bSAndrew Rist  *
112e82054bSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
122e82054bSAndrew Rist  *
132e82054bSAndrew Rist  * Unless required by applicable law or agreed to in writing,
142e82054bSAndrew Rist  * software distributed under the License is distributed on an
152e82054bSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
162e82054bSAndrew Rist  * KIND, either express or implied.  See the License for the
172e82054bSAndrew Rist  * specific language governing permissions and limitations
182e82054bSAndrew Rist  * under the License.
192e82054bSAndrew Rist  *
202e82054bSAndrew Rist  *************************************************************/
212e82054bSAndrew Rist 
222e82054bSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <stdlib.h>
25cdf0e10cSrcweir #include <stdio.h>
26cdf0e10cSrcweir #include <string.h>
27cdf0e10cSrcweir #include <process.h>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #if defined _MSC_VER
30cdf0e10cSrcweir #pragma warning(push, 1)
31cdf0e10cSrcweir #endif
32cdf0e10cSrcweir #include <windows.h>
33cdf0e10cSrcweir #if defined _MSC_VER
34cdf0e10cSrcweir #pragma warning(pop)
35cdf0e10cSrcweir #endif
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include "cppuhelper/findsofficepath.h"
38cdf0e10cSrcweir #include "sal/types.h"
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #define MY_LENGTH(s) (sizeof (s) / sizeof *(s) - 1)
41cdf0e10cSrcweir 
42cdf0e10cSrcweir char const* getPath();
43cdf0e10cSrcweir char* createCommandLine( char* lpCmdLine );
44cdf0e10cSrcweir FILE* getErrorFile( int create );
45cdf0e10cSrcweir void writeError( const char* errstr );
46cdf0e10cSrcweir void closeErrorFile();
47cdf0e10cSrcweir 
48cdf0e10cSrcweir /*
49cdf0e10cSrcweir  * The main function implements a loader for applications which use UNO.
50cdf0e10cSrcweir  *
51cdf0e10cSrcweir  * <p>This code runs on the Windows platform only.</p>
52cdf0e10cSrcweir  *
53cdf0e10cSrcweir  * <p>The main function detects a UNO installation on the system and adds the
54cdf0e10cSrcweir  * program directory of the UNO installation to the PATH environment variable.
55cdf0e10cSrcweir  * After that, the application process is loaded and started, whereby the
56cdf0e10cSrcweir  * new process inherits the environment of the calling process, including
57cdf0e10cSrcweir  * the modified PATH environment variable. The application's executable name
58cdf0e10cSrcweir  * must be the same as the name of this executable, prefixed by '_'.</p>
59cdf0e10cSrcweir  *
60cdf0e10cSrcweir  * <p>A UNO installation can be specified by the user by setting the UNO_PATH
61cdf0e10cSrcweir  * environment variable to the program directory of the UNO installation.
62cdf0e10cSrcweir  * If no installation is specified by the user, the default installation on
63cdf0e10cSrcweir  * the system will be taken. The default installation is read from the
64*599cc5b4SOliver-Rainer Wittmann  * default value of the key "Software\OpenOffice\UNO\InstallPath" from the
65cdf0e10cSrcweir  * root key HKEY_CURRENT_USER in the Windows Registry. If this key is missing,
66cdf0e10cSrcweir  * the key is read from the root key HKEY_LOCAL_MACHINE.</p>
67cdf0e10cSrcweir  */
68cdf0e10cSrcweir int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
69cdf0e10cSrcweir                     LPSTR lpCmdLine, int nCmdShow )
70cdf0e10cSrcweir {
71cdf0e10cSrcweir     const char* ENVVARNAME = "PATH";
72cdf0e10cSrcweir     const char* PATHSEPARATOR = ";";
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     char const* path = NULL;
75cdf0e10cSrcweir     char path2[MAX_PATH];
76cdf0e10cSrcweir     char* value = NULL;
77cdf0e10cSrcweir     char* envstr = NULL;
78cdf0e10cSrcweir     char* cmdline = NULL;
79cdf0e10cSrcweir     int size;
80cdf0e10cSrcweir     STARTUPINFO startup_info;
81cdf0e10cSrcweir     PROCESS_INFORMATION process_info;
82cdf0e10cSrcweir     BOOL bCreate;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     (void) hInstance; /* unused */
85cdf0e10cSrcweir     (void) hPrevInstance; /* unused */
86cdf0e10cSrcweir     (void) nCmdShow; /* unused */
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     /* get the path of the UNO installation */
89cdf0e10cSrcweir     path = getPath();
90cdf0e10cSrcweir 
91cdf0e10cSrcweir     if ( path != NULL )
92cdf0e10cSrcweir     {
93cdf0e10cSrcweir         wchar_t cmd[
94cdf0e10cSrcweir             MY_LENGTH(L"\"") + MAX_PATH +
95cdf0e10cSrcweir             MY_LENGTH(L"\\unoinfo.exe\" c++")];
96cdf0e10cSrcweir             /* hopefully does not overflow */
97cdf0e10cSrcweir         int pathsize;
98cdf0e10cSrcweir         SECURITY_ATTRIBUTES sec;
99cdf0e10cSrcweir         HANDLE temp;
100cdf0e10cSrcweir         HANDLE stdoutRead;
101cdf0e10cSrcweir         HANDLE stdoutWrite;
102cdf0e10cSrcweir         STARTUPINFOW startinfo;
103cdf0e10cSrcweir         PROCESS_INFORMATION procinfo;
104cdf0e10cSrcweir         int ret;
105cdf0e10cSrcweir         cmd[0] = L'"';
106cdf0e10cSrcweir         pathsize = MultiByteToWideChar(CP_ACP, 0, path, -1, cmd + 1, MAX_PATH);
107cdf0e10cSrcweir         if (pathsize == 0) {
108cdf0e10cSrcweir             writeError("Error: MultiByteToWideChar failed!\n");
109cdf0e10cSrcweir             closeErrorFile();
110cdf0e10cSrcweir             return 1;
111cdf0e10cSrcweir         }
112cdf0e10cSrcweir         if (wcschr(cmd + 1, L'"') != NULL) {
113cdf0e10cSrcweir             writeError("Error: bad characters in UNO installation path!\n");
114cdf0e10cSrcweir             closeErrorFile();
115cdf0e10cSrcweir             return 1;
116cdf0e10cSrcweir         }
117cdf0e10cSrcweir         wcscpy(
118cdf0e10cSrcweir             cmd + pathsize,
119cdf0e10cSrcweir             (L"\\unoinfo.exe\" c++" +
120cdf0e10cSrcweir              (pathsize == 1 || cmd[pathsize - 1] != L'\\' ? 0 : 1)));
121cdf0e10cSrcweir         sec.nLength = sizeof (SECURITY_ATTRIBUTES);
122cdf0e10cSrcweir         sec.lpSecurityDescriptor = NULL;
123cdf0e10cSrcweir         sec.bInheritHandle = TRUE;
124cdf0e10cSrcweir         if (CreatePipe(&temp, &stdoutWrite, &sec, 0) == 0 ||
125cdf0e10cSrcweir             DuplicateHandle(
126cdf0e10cSrcweir                 GetCurrentProcess(), temp, GetCurrentProcess(), &stdoutRead, 0,
127cdf0e10cSrcweir                 FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS) == 0)
128cdf0e10cSrcweir         {
129cdf0e10cSrcweir             writeError("Error: CreatePipe/DuplicateHandle failed!\n");
130cdf0e10cSrcweir             closeErrorFile();
131cdf0e10cSrcweir             return 1;
132cdf0e10cSrcweir         }
133cdf0e10cSrcweir         memset(&startinfo, 0, sizeof (STARTUPINFOW));
134cdf0e10cSrcweir         startinfo.cb = sizeof (STARTUPINFOW);
135cdf0e10cSrcweir         startinfo.lpDesktop = L"";
136cdf0e10cSrcweir         startinfo.dwFlags = STARTF_USESTDHANDLES;
137cdf0e10cSrcweir         startinfo.hStdOutput = stdoutWrite;
138cdf0e10cSrcweir         ret = CreateProcessW(
139cdf0e10cSrcweir             NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &startinfo, &procinfo);
140cdf0e10cSrcweir         if (ret != 0) {
141cdf0e10cSrcweir             char * buf = NULL;
142cdf0e10cSrcweir             size_t n = 1000;
143cdf0e10cSrcweir             size_t k = 0;
144cdf0e10cSrcweir             DWORD exitcode;
145cdf0e10cSrcweir             int path2size;
146cdf0e10cSrcweir             CloseHandle(stdoutWrite);
147cdf0e10cSrcweir             CloseHandle(procinfo.hThread);
148cdf0e10cSrcweir             for (;;) {
149cdf0e10cSrcweir                 DWORD m;
150cdf0e10cSrcweir                 buf = realloc(buf, n);
151cdf0e10cSrcweir                 if (buf == NULL) {
152cdf0e10cSrcweir                     writeError(
153cdf0e10cSrcweir                         "Error: out of memory reading unoinfo output!\n");
154cdf0e10cSrcweir                     closeErrorFile();
155cdf0e10cSrcweir                     return 1;
156cdf0e10cSrcweir                 }
157cdf0e10cSrcweir                 if (!ReadFile(stdoutRead, buf + k, n - k, &m, NULL))
158cdf0e10cSrcweir                 {
159cdf0e10cSrcweir                     DWORD err = GetLastError();
160cdf0e10cSrcweir                     if (err == ERROR_HANDLE_EOF || err == ERROR_BROKEN_PIPE) {
161cdf0e10cSrcweir                         break;
162cdf0e10cSrcweir                     }
163cdf0e10cSrcweir                     writeError("Error: cannot read unoinfo output!\n");
164cdf0e10cSrcweir                     closeErrorFile();
165cdf0e10cSrcweir                     return 1;
166cdf0e10cSrcweir                 }
167cdf0e10cSrcweir                 if (m == 0) {
168cdf0e10cSrcweir                     break;
169cdf0e10cSrcweir                 }
170cdf0e10cSrcweir                 k += m;
171cdf0e10cSrcweir                 if (k >= n) {
172cdf0e10cSrcweir                     if (n >= SAL_MAX_SIZE / 2) {
173cdf0e10cSrcweir                         writeError(
174cdf0e10cSrcweir                             "Error: out of memory reading unoinfo output!\n");
175cdf0e10cSrcweir                         closeErrorFile();
176cdf0e10cSrcweir                         return 1;
177cdf0e10cSrcweir                     }
178cdf0e10cSrcweir                     n *= 2;
179cdf0e10cSrcweir                 }
180cdf0e10cSrcweir             }
181cdf0e10cSrcweir             if ((k & 1) == 1) {
182cdf0e10cSrcweir                 writeError("Error: bad unoinfo output!\n");
183cdf0e10cSrcweir                 closeErrorFile();
184cdf0e10cSrcweir                 return 1;
185cdf0e10cSrcweir             }
186cdf0e10cSrcweir             CloseHandle(stdoutRead);
187cdf0e10cSrcweir             if (!GetExitCodeProcess(procinfo.hProcess, &exitcode) ||
188cdf0e10cSrcweir                 exitcode != 0)
189cdf0e10cSrcweir             {
190cdf0e10cSrcweir                 writeError("Error: executing unoinfo failed!\n");
191cdf0e10cSrcweir                 closeErrorFile();
192cdf0e10cSrcweir                 return 1;
193cdf0e10cSrcweir             }
194cdf0e10cSrcweir             if (k == 0) {
195cdf0e10cSrcweir                 path2size = 0;
196cdf0e10cSrcweir             } else {
197cdf0e10cSrcweir                 path2size = WideCharToMultiByte(
198cdf0e10cSrcweir                     CP_ACP, 0, (wchar_t *) buf, k / 2, path2, MAX_PATH - 1,
199cdf0e10cSrcweir                     NULL, NULL);
200cdf0e10cSrcweir                 if (path2size == 0) {
201cdf0e10cSrcweir                     writeError("Error: converting unoinfo output failed!\n");
202cdf0e10cSrcweir                     closeErrorFile();
203cdf0e10cSrcweir                     return 1;
204cdf0e10cSrcweir                 }
205cdf0e10cSrcweir             }
206cdf0e10cSrcweir             path2[path2size] = '\0';
207cdf0e10cSrcweir             path = path2;
208cdf0e10cSrcweir         } else {
209cdf0e10cSrcweir             if (GetLastError() != ERROR_FILE_NOT_FOUND) {
210cdf0e10cSrcweir                 writeError("Error: calling unoinfo failed!\n");
211cdf0e10cSrcweir                 closeErrorFile();
212cdf0e10cSrcweir                 return 1;
213cdf0e10cSrcweir             }
214cdf0e10cSrcweir             CloseHandle(stdoutRead);
215cdf0e10cSrcweir             CloseHandle(stdoutWrite);
216cdf0e10cSrcweir         }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir         /* get the value of the PATH environment variable */
219cdf0e10cSrcweir         value = getenv( ENVVARNAME );
220cdf0e10cSrcweir 
221cdf0e10cSrcweir         /*
222cdf0e10cSrcweir          * add the UNO installation path to the PATH environment variable;
223cdf0e10cSrcweir          * note that this only affects the environment variable of the current
224cdf0e10cSrcweir          * process, the command processor's environment is not changed
225cdf0e10cSrcweir          */
226cdf0e10cSrcweir         size = strlen( ENVVARNAME ) + strlen( "=" ) + strlen( path ) + 1;
227cdf0e10cSrcweir 		if ( value != NULL )
228cdf0e10cSrcweir             size += strlen( PATHSEPARATOR ) + strlen( value );
229cdf0e10cSrcweir 		envstr = (char*) malloc( size );
230cdf0e10cSrcweir         strcpy( envstr, ENVVARNAME );
231cdf0e10cSrcweir         strcat( envstr, "=" );
232cdf0e10cSrcweir         strcat( envstr, path );
233cdf0e10cSrcweir 		if ( value != NULL )
234cdf0e10cSrcweir 		{
235cdf0e10cSrcweir             strcat( envstr, PATHSEPARATOR );
236cdf0e10cSrcweir             strcat( envstr, value );
237cdf0e10cSrcweir 		}
238cdf0e10cSrcweir         _putenv( envstr );
239cdf0e10cSrcweir         free( envstr );
240cdf0e10cSrcweir     }
241cdf0e10cSrcweir     else
242cdf0e10cSrcweir     {
243cdf0e10cSrcweir         writeError( "Warning: no UNO installation found!\n" );
244cdf0e10cSrcweir     }
245cdf0e10cSrcweir 
246cdf0e10cSrcweir     /* create the command line for the application process */
247cdf0e10cSrcweir     cmdline = createCommandLine( lpCmdLine );
248cdf0e10cSrcweir     if ( cmdline == NULL )
249cdf0e10cSrcweir     {
250cdf0e10cSrcweir         writeError( "Error: cannot create command line!\n" );
251cdf0e10cSrcweir         closeErrorFile();
252cdf0e10cSrcweir         return 1;
253cdf0e10cSrcweir     }
254cdf0e10cSrcweir 
255cdf0e10cSrcweir     /* create the application process */
256cdf0e10cSrcweir 	memset( &startup_info, 0, sizeof( STARTUPINFO ) );
257cdf0e10cSrcweir 	startup_info.cb = sizeof( STARTUPINFO );
258cdf0e10cSrcweir     bCreate = CreateProcess( NULL, cmdline, NULL,  NULL, FALSE, 0, NULL, NULL,
259cdf0e10cSrcweir                              &startup_info, &process_info );
260cdf0e10cSrcweir     free( cmdline );
261cdf0e10cSrcweir     if ( !bCreate )
262cdf0e10cSrcweir     {
263cdf0e10cSrcweir         writeError( "Error: cannot create process!\n" );
264cdf0e10cSrcweir         closeErrorFile();
265cdf0e10cSrcweir         return 1;
266cdf0e10cSrcweir     }
267cdf0e10cSrcweir 
268cdf0e10cSrcweir     /* close the error file */
269cdf0e10cSrcweir     closeErrorFile();
270cdf0e10cSrcweir 
271cdf0e10cSrcweir     return 0;
272cdf0e10cSrcweir }
273cdf0e10cSrcweir 
274cdf0e10cSrcweir /*
275cdf0e10cSrcweir  * Gets the path of a UNO installation.
276cdf0e10cSrcweir  *
277cdf0e10cSrcweir  * @return the installation path or NULL, if no installation was specified or
278cdf0e10cSrcweir  *         found, or if an error occured
279cdf0e10cSrcweir  */
280cdf0e10cSrcweir char const* getPath()
281cdf0e10cSrcweir {
282cdf0e10cSrcweir     char const* path = cppuhelper_detail_findSofficePath();
283cdf0e10cSrcweir 
284cdf0e10cSrcweir     if ( path == NULL )
285cdf0e10cSrcweir         writeError( "Warning: getting path from Windows Registry failed!\n" );
286cdf0e10cSrcweir 
287cdf0e10cSrcweir     return path;
288cdf0e10cSrcweir }
289cdf0e10cSrcweir 
290cdf0e10cSrcweir /*
291cdf0e10cSrcweir  * Creates the command line for the application process including the absolute
292cdf0e10cSrcweir  * path of the executable.
293cdf0e10cSrcweir  *
294cdf0e10cSrcweir  * <p>The application's executable file name is the name of this executable
295cdf0e10cSrcweir  * prefixed by '_'.</p>
296cdf0e10cSrcweir  *
297cdf0e10cSrcweir  * @param appendix specifies the command line for the application excluding
298cdf0e10cSrcweir  *                 the executable name
299cdf0e10cSrcweir  *
300cdf0e10cSrcweir  * @return the command line for the application process or NULL, if an error
301cdf0e10cSrcweir  *         occured
302cdf0e10cSrcweir  */
303cdf0e10cSrcweir char* createCommandLine( char* appendix )
304cdf0e10cSrcweir {
305cdf0e10cSrcweir     const char* CMDPREFIX = "_";
306cdf0e10cSrcweir     const char* DQUOTE = "\"";
307cdf0e10cSrcweir     const char* SPACE = " ";
308cdf0e10cSrcweir 
309cdf0e10cSrcweir     char* cmdline = NULL;
310cdf0e10cSrcweir 
311cdf0e10cSrcweir     char cmdname[ _MAX_PATH ];
312cdf0e10cSrcweir     char drive[ _MAX_DRIVE ];
313cdf0e10cSrcweir     char dir[ _MAX_PATH ];
314cdf0e10cSrcweir     char base[ _MAX_FNAME ];
315cdf0e10cSrcweir     char newbase[ _MAX_FNAME ];
316cdf0e10cSrcweir     char ext[ _MAX_EXT ];
317cdf0e10cSrcweir 
318cdf0e10cSrcweir     /* get the absolute path of the executable file */
319cdf0e10cSrcweir     if ( GetModuleFileName( NULL, cmdname, sizeof( cmdname ) ) )
320cdf0e10cSrcweir     {
321cdf0e10cSrcweir         /* prefix the executable file name by '_' */
322cdf0e10cSrcweir         _splitpath( cmdname, drive, dir, base, ext );
323cdf0e10cSrcweir         strcpy( newbase, CMDPREFIX );
324cdf0e10cSrcweir         strcat( newbase, base );
325cdf0e10cSrcweir         _makepath( cmdname, drive, dir, newbase, ext );
326cdf0e10cSrcweir 
327cdf0e10cSrcweir         /* create the command line */
328cdf0e10cSrcweir         cmdline = (char*) malloc( strlen( DQUOTE ) + strlen( cmdname ) +
329cdf0e10cSrcweir             strlen ( DQUOTE ) + strlen( SPACE ) + strlen( appendix ) + 1 );
330cdf0e10cSrcweir         strcpy( cmdline, DQUOTE );
331cdf0e10cSrcweir         strcat( cmdline, cmdname );
332cdf0e10cSrcweir         strcat( cmdline, DQUOTE );
333cdf0e10cSrcweir         strcat( cmdline, SPACE );
334cdf0e10cSrcweir         strcat( cmdline, appendix );
335cdf0e10cSrcweir     }
336cdf0e10cSrcweir 
337cdf0e10cSrcweir     return cmdline;
338cdf0e10cSrcweir }
339cdf0e10cSrcweir 
340cdf0e10cSrcweir /*
341cdf0e10cSrcweir  * Gets the pointer to the error file.
342cdf0e10cSrcweir  *
343cdf0e10cSrcweir  * <p>The error file will only be created, if create != 0.</p>
344cdf0e10cSrcweir  *
345cdf0e10cSrcweir  * <p>The error file has the name <executable file name>-error.log and is
346cdf0e10cSrcweir  * created in the same directory as the executable file. If this fails,
347cdf0e10cSrcweir  * the error file is created in the directory designated for temporary files.
348cdf0e10cSrcweir  * </p>
349cdf0e10cSrcweir 
350cdf0e10cSrcweir  * @param create specifies, if the error file should be created (create != 0)
351cdf0e10cSrcweir  *
352cdf0e10cSrcweir  * @return the pointer to the open error file or NULL, if no error file is
353cdf0e10cSrcweir  *         open or can be created
354cdf0e10cSrcweir  */
355cdf0e10cSrcweir FILE* getErrorFile( int create )
356cdf0e10cSrcweir {
357cdf0e10cSrcweir     const char* MODE = "w";
358cdf0e10cSrcweir     const char* BASEPOSTFIX = "-error";
359cdf0e10cSrcweir     const char* EXTENSION = ".log";
360cdf0e10cSrcweir 
361cdf0e10cSrcweir     static FILE* ferr = NULL;
362cdf0e10cSrcweir 
363cdf0e10cSrcweir     char fname[ _MAX_PATH ];
364cdf0e10cSrcweir     char drive[ _MAX_DRIVE ];
365cdf0e10cSrcweir     char dir[ _MAX_PATH ];
366cdf0e10cSrcweir     char base[ _MAX_FNAME ];
367cdf0e10cSrcweir     char newbase[ _MAX_FNAME ];
368cdf0e10cSrcweir     char ext[ _MAX_EXT ];
369cdf0e10cSrcweir 
370cdf0e10cSrcweir     if ( ferr == NULL && create )
371cdf0e10cSrcweir     {
372cdf0e10cSrcweir         /* get the absolute path of the executable file */
373cdf0e10cSrcweir         if ( GetModuleFileName( NULL, fname, sizeof( fname ) ) )
374cdf0e10cSrcweir         {
375cdf0e10cSrcweir             /* create error file in the directory of the executable file */
376cdf0e10cSrcweir             _splitpath( fname, drive, dir, base, ext );
377cdf0e10cSrcweir             strcpy( newbase, base );
378cdf0e10cSrcweir             strcat( newbase, BASEPOSTFIX );
379cdf0e10cSrcweir             _makepath( fname, drive, dir, newbase, EXTENSION );
380cdf0e10cSrcweir             ferr = fopen( fname, MODE );
381cdf0e10cSrcweir 
382cdf0e10cSrcweir             if ( ferr == NULL )
383cdf0e10cSrcweir             {
384cdf0e10cSrcweir                 /* create error file in the temp directory */
385cdf0e10cSrcweir                 GetTempPath( sizeof( fname ), fname );
386cdf0e10cSrcweir                 strcat( fname, newbase );
387cdf0e10cSrcweir                 strcat( fname, EXTENSION );
388cdf0e10cSrcweir                 ferr = fopen( fname, MODE );
389cdf0e10cSrcweir             }
390cdf0e10cSrcweir         }
391cdf0e10cSrcweir     }
392cdf0e10cSrcweir 
393cdf0e10cSrcweir     return ferr;
394cdf0e10cSrcweir }
395cdf0e10cSrcweir 
396cdf0e10cSrcweir /*
397cdf0e10cSrcweir  * Writes an error message to the error file.
398cdf0e10cSrcweir  *
399cdf0e10cSrcweir  * @param errstr specifies the error message
400cdf0e10cSrcweir  */
401cdf0e10cSrcweir void writeError( const char* errstr )
402cdf0e10cSrcweir {
403cdf0e10cSrcweir     FILE* ferr = getErrorFile( 1 );
404cdf0e10cSrcweir     if ( ferr != NULL )
405cdf0e10cSrcweir     {
406cdf0e10cSrcweir         fprintf( ferr, errstr );
407cdf0e10cSrcweir         fflush( ferr );
408cdf0e10cSrcweir     }
409cdf0e10cSrcweir }
410cdf0e10cSrcweir 
411cdf0e10cSrcweir /*
412cdf0e10cSrcweir  * Closes the error file.
413cdf0e10cSrcweir  */
414cdf0e10cSrcweir void closeErrorFile()
415cdf0e10cSrcweir {
416cdf0e10cSrcweir     FILE* ferr = getErrorFile( 0 );
417cdf0e10cSrcweir     if ( ferr != NULL )
418cdf0e10cSrcweir         fclose( ferr );
419cdf0e10cSrcweir }
420