xref: /trunk/main/sal/osl/os2/process_impl.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 
29 #define INCL_DOS
30 #include <os2.h>
31 
32 #include "osl/process.h"
33 #include <osl/mutex.h>
34 
35 #ifndef INCLUDED_LIMITS_H
36 #include <limits.h>
37 #define INCLUDED_LIMITS_H
38 #endif
39 
40 #ifndef INCLUDED_PTHREAD_H
41 #include <pthread.h>
42 #define INCLUDED_PTHREAD_H
43 #endif
44 
45 #ifndef INCLUDED_STDLIB_H
46 #include <stdlib.h>
47 #define INCLUDED_STDLIB_H
48 #endif
49 
50 #ifndef INCLUDED_STRING_H
51 #include <string.h>
52 #define INCLUDED_STRING_H
53 #endif
54 #include "osl/diagnose.h"
55 #include <osl/file.h>
56 #include "osl/module.h"
57 #include "osl/thread.h"
58 #include "rtl/ustring.hxx"
59 
60 #ifndef _OSL_FILE_PATH_HELPER_H_
61 #include "file_path_helper.h"
62 #endif
63 
64 #ifndef _OSL_UUNXAPI_H_
65 #include "uunxapi.h"
66 #endif
67 
68 /***************************************
69   osl_bootstrap_getExecutableFile_Impl().
70 
71   @internal
72   @see rtl_bootstrap
73   @see #i37371#
74 
75  **************************************/
76 
77 extern "C" oslProcessError SAL_CALL osl_bootstrap_getExecutableFile_Impl (
78     rtl_uString ** ppFileURL
79 ) SAL_THROW_EXTERN_C();
80 
81 
82 oslProcessError SAL_CALL osl_bootstrap_getExecutableFile_Impl (
83     rtl_uString ** ppFileURL
84 ) SAL_THROW_EXTERN_C()
85 {
86     oslProcessError result = osl_Process_E_NotFound;
87     CHAR    szName[CCHMAXPATH];
88     PPIB    ppib;
89     PTIB    ptib;
90     APIRET  rc;
91 
92     rc = DosGetInfoBlocks(&ptib, &ppib);
93     rc = DosQueryModuleName(ppib->pib_hmte, sizeof(szName), szName);
94 
95     if (rc == 0)
96     {
97         rtl_uString * pAbsPath = 0;
98 
99         rtl_string2UString(
100             &pAbsPath,
101             szName, strlen(szName), osl_getThreadTextEncoding(),
102             OSTRING_TO_OUSTRING_CVTFLAGS);
103 
104         if (pAbsPath)
105         {
106             /* Convert from path to url. */
107             if (osl_getFileURLFromSystemPath (pAbsPath, ppFileURL) == osl_File_E_None)
108             {
109                 /* Success. */
110                 result = osl_Process_E_None;
111             }
112             rtl_uString_release (pAbsPath);
113         }
114     }
115 
116     return (result);
117 }
118 
119 /***************************************
120  CommandArgs_Impl.
121  **************************************/
122 struct CommandArgs_Impl
123 {
124     oslMutex        m_mutex;
125     sal_uInt32      m_nCount;
126     rtl_uString **  m_ppArgs;
127 };
128 
129 static struct CommandArgs_Impl g_command_args =
130 {
131     osl_createMutex(),
132     0,
133     0
134 };
135 
136 /***************************************
137   osl_getExecutableFile().
138  **************************************/
139 oslProcessError SAL_CALL osl_getExecutableFile (rtl_uString ** ppustrFile)
140 {
141     oslProcessError result = osl_Process_E_NotFound;
142 
143     osl_acquireMutex(g_command_args.m_mutex);
144     OSL_ASSERT(g_command_args.m_nCount > 0);
145     if (g_command_args.m_nCount > 0)
146     {
147         /* CommandArgs set. Obtain argv[0]. */
148         rtl_uString_assign (ppustrFile, g_command_args.m_ppArgs[0]);
149         result = osl_Process_E_None;
150     }
151     osl_releaseMutex(g_command_args.m_mutex);
152 
153     return (result);
154 }
155 
156 /***************************************
157  osl_getCommandArgCount().
158  **************************************/
159 sal_uInt32 SAL_CALL osl_getCommandArgCount (void)
160 {
161     sal_uInt32 result = 0;
162 
163     osl_acquireMutex(g_command_args.m_mutex);
164     OSL_ASSERT(g_command_args.m_nCount > 0);
165     if (g_command_args.m_nCount > 0)
166         result = g_command_args.m_nCount - 1;
167     osl_releaseMutex(g_command_args.m_mutex);
168 
169     return (result);
170 }
171 
172 /***************************************
173  osl_getCommandArg().
174  **************************************/
175 oslProcessError SAL_CALL osl_getCommandArg (sal_uInt32 nArg, rtl_uString ** strCommandArg)
176 {
177     oslProcessError result = osl_Process_E_NotFound;
178 
179     osl_acquireMutex(g_command_args.m_mutex);
180     OSL_ASSERT(g_command_args.m_nCount > 0);
181     if (g_command_args.m_nCount > (nArg + 1))
182     {
183         rtl_uString_assign (strCommandArg, g_command_args.m_ppArgs[nArg + 1]);
184         result = osl_Process_E_None;
185     }
186     osl_releaseMutex(g_command_args.m_mutex);
187 
188     return (result);
189 }
190 
191 /***************************************
192  osl_setCommandArgs().
193  **************************************/
194 void SAL_CALL osl_setCommandArgs (int argc, char ** argv)
195 {
196     OSL_ASSERT(argc > 0);
197     osl_acquireMutex(g_command_args.m_mutex);
198     OSL_ENSURE (g_command_args.m_nCount == 0, "osl_setCommandArgs(): CommandArgs already set.");
199     if (g_command_args.m_nCount == 0)
200     {
201         rtl_uString** ppArgs = (rtl_uString**)rtl_allocateZeroMemory (argc * sizeof(rtl_uString*));
202         if (ppArgs != 0)
203         {
204             rtl_TextEncoding encoding = osl_getThreadTextEncoding();
205             for (int i = 0; i < argc; i++)
206             {
207                 rtl_string2UString (
208                     &(ppArgs[i]),
209                     argv[i], rtl_str_getLength (argv[i]), encoding,
210                     OSTRING_TO_OUSTRING_CVTFLAGS);
211             }
212             if (ppArgs[0] != 0)
213             {
214                 /* see @ osl_getExecutableFile(). */
215                 if (rtl_ustr_indexOfChar (rtl_uString_getStr(ppArgs[0]), sal_Unicode('/')) == -1)
216                 {
217                     const rtl::OUString PATH (RTL_CONSTASCII_USTRINGPARAM("PATH"));
218 
219                     rtl_uString * pSearchPath = 0;
220                     osl_getEnvironment (PATH.pData, &pSearchPath);
221                     if (pSearchPath)
222                     {
223                         rtl_uString * pSearchResult = 0;
224                         osl_searchPath (ppArgs[0], pSearchPath, &pSearchResult);
225                         if (pSearchResult)
226                         {
227                             rtl_uString_assign (&(ppArgs[0]), pSearchResult);
228                             rtl_uString_release (pSearchResult);
229                         }
230                         rtl_uString_release (pSearchPath);
231                     }
232                 }
233 
234                 rtl_uString * pArg0 = 0;
235                 if (realpath_u (ppArgs[0], &pArg0))
236                 {
237                     osl_getFileURLFromSystemPath (pArg0, &(ppArgs[0]));
238                     rtl_uString_release (pArg0);
239                 }
240             }
241             g_command_args.m_nCount = argc;
242             g_command_args.m_ppArgs = ppArgs;
243         }
244     }
245     osl_releaseMutex(g_command_args.m_mutex);
246 
247 }
248 
249 /***************************************
250  osl_getEnvironment().
251  **************************************/
252 oslProcessError SAL_CALL osl_getEnvironment(rtl_uString* pustrEnvVar, rtl_uString** ppustrValue)
253 {
254     oslProcessError  result   = osl_Process_E_NotFound;
255     rtl_TextEncoding encoding = osl_getThreadTextEncoding();
256     rtl_String* pstr_env_var  = 0;
257 
258     OSL_PRECOND(pustrEnvVar, "osl_getEnvironment(): Invalid parameter");
259     OSL_PRECOND(ppustrValue, "osl_getEnvironment(): Invalid parameter");
260 
261     rtl_uString2String(
262         &pstr_env_var,
263         rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
264         OUSTRING_TO_OSTRING_CVTFLAGS);
265     if (pstr_env_var != 0)
266     {
267         const char* p_env_var = getenv (rtl_string_getStr (pstr_env_var));
268         if (p_env_var != 0)
269         {
270             rtl_string2UString(
271                 ppustrValue,
272                 p_env_var, strlen(p_env_var), encoding,
273                 OSTRING_TO_OUSTRING_CVTFLAGS);
274             OSL_ASSERT(*ppustrValue != NULL);
275 
276             result = osl_Process_E_None;
277         }
278         rtl_string_release(pstr_env_var);
279     }
280 
281     return (result);
282 }
283 
284 /***************************************
285  osl_setEnvironment().
286  **************************************/
287 oslProcessError SAL_CALL osl_setEnvironment(rtl_uString* pustrEnvVar, rtl_uString* pustrValue)
288 {
289     oslProcessError  result   = osl_Process_E_Unknown;
290     rtl_TextEncoding encoding = osl_getThreadTextEncoding();
291     rtl_String* pstr_env_var  = 0;
292     rtl_String* pstr_val  = 0;
293 
294     OSL_PRECOND(pustrEnvVar, "osl_setEnvironment(): Invalid parameter");
295     OSL_PRECOND(pustrValue, "osl_setEnvironment(): Invalid parameter");
296 
297     rtl_uString2String(
298         &pstr_env_var,
299         rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
300         OUSTRING_TO_OSTRING_CVTFLAGS);
301 
302     rtl_uString2String(
303         &pstr_val,
304         rtl_uString_getStr(pustrValue), rtl_uString_getLength(pustrValue), encoding,
305         OUSTRING_TO_OSTRING_CVTFLAGS);
306 
307     if (pstr_env_var != 0 && pstr_val != 0)
308     {
309         //Can't determine if OS/2 EMX has a working setenv or not, so use putenv,
310         //feel free to use setenv here if its available and works
311         rtl_String * pBuffer = NULL;
312 
313         sal_Int32 nCapacity = rtl_stringbuffer_newFromStringBuffer( &pBuffer,
314             rtl_string_getLength(pstr_env_var) + rtl_string_getLength(pstr_val) + 1,
315             pstr_env_var );
316         rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, "=", 1);
317         rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length,
318             rtl_string_getStr(pstr_val), rtl_string_getLength(pstr_val) );
319 
320         rtl_string_acquire(pBuffer); // argument to putenv must leak on success
321 
322         if (putenv(rtl_string_getStr(pBuffer)) == 0)
323             result = osl_Process_E_None;
324         else
325             rtl_string_release(pBuffer);
326     }
327 
328     if (pstr_val)
329         rtl_string_release(pstr_val);
330 
331     if (pstr_env_var != 0)
332         rtl_string_release(pstr_env_var);
333 
334     return (result);
335 }
336 
337 /***************************************
338  osl_clearEnvironment().
339  **************************************/
340 oslProcessError SAL_CALL osl_clearEnvironment(rtl_uString* pustrEnvVar)
341 {
342     oslProcessError  result   = osl_Process_E_Unknown;
343     rtl_TextEncoding encoding = osl_getThreadTextEncoding();
344     rtl_String* pstr_env_var  = 0;
345 
346     OSL_PRECOND(pustrEnvVar, "osl_setEnvironment(): Invalid parameter");
347 
348     rtl_uString2String(
349         &pstr_env_var,
350         rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
351         OUSTRING_TO_OSTRING_CVTFLAGS);
352 
353     if (pstr_env_var)
354     {
355         //Can't determine if OS/2 EMX has a working unsetenv or not, so use putenv,
356         //feel free to use unsetenv here if its available and works
357         rtl_String * pBuffer = NULL;
358 
359         sal_Int32 nCapacity = rtl_stringbuffer_newFromStringBuffer( &pBuffer,
360             rtl_string_getLength(pstr_env_var) + 1, pstr_env_var );
361         rtl_stringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, "=", 1);
362 
363         rtl_string_acquire(pBuffer); // argument to putenv must leak on success
364 
365         if (putenv(rtl_string_getStr(pBuffer)) == 0)
366             result = osl_Process_E_None;
367         else
368             rtl_string_release(pBuffer);
369 
370         rtl_string_release(pstr_env_var);
371     }
372 
373     return (result);
374 }
375 
376 /***************************************
377  osl_getProcessWorkingDir().
378  **************************************/
379 oslProcessError SAL_CALL osl_getProcessWorkingDir(rtl_uString **ppustrWorkingDir)
380 {
381     oslProcessError result = osl_Process_E_Unknown;
382     char buffer[PATH_MAX];
383 
384     OSL_PRECOND(ppustrWorkingDir, "osl_getProcessWorkingDir(): Invalid parameter");
385 
386     if (getcwd (buffer, sizeof(buffer)) != 0)
387     {
388         rtl_uString* ustrTmp = 0;
389 
390         rtl_string2UString(
391             &ustrTmp,
392             buffer, strlen(buffer), osl_getThreadTextEncoding(),
393             OSTRING_TO_OUSTRING_CVTFLAGS);
394         if (ustrTmp != 0)
395         {
396             if (osl_getFileURLFromSystemPath (ustrTmp, ppustrWorkingDir) == osl_File_E_None)
397                 result = osl_Process_E_None;
398             rtl_uString_release (ustrTmp);
399         }
400     }
401 
402     return (result);
403 }
404 
405 /******************************************************************************
406  *
407  *              new functions to set/return the current process locale
408  *
409  *****************************************************************************/
410 
411 struct ProcessLocale_Impl
412 {
413     oslMutex        m_mutex;
414     rtl_Locale *    m_pLocale;
415 };
416 
417 static struct ProcessLocale_Impl g_process_locale =
418 {
419     osl_createMutex(),
420     0
421 };
422 
423 extern "C" void _imp_getProcessLocale( rtl_Locale ** );
424 extern "C" int  _imp_setProcessLocale( rtl_Locale * );
425 
426 /**********************************************
427  osl_getProcessLocale().
428  *********************************************/
429 oslProcessError SAL_CALL osl_getProcessLocale( rtl_Locale ** ppLocale )
430 {
431     OSL_PRECOND(ppLocale, "osl_getProcessLocale(): Invalid parameter.");
432 
433     osl_acquireMutex(g_process_locale.m_mutex);
434 
435     if (g_process_locale.m_pLocale == 0)
436         _imp_getProcessLocale (&(g_process_locale.m_pLocale));
437     *ppLocale = g_process_locale.m_pLocale;
438 
439     osl_releaseMutex(g_process_locale.m_mutex);
440 
441     return (osl_Process_E_None);
442 }
443 
444 /**********************************************
445  osl_setProcessLocale().
446  *********************************************/
447 oslProcessError SAL_CALL osl_setProcessLocale( rtl_Locale * pLocale )
448 {
449     oslProcessError result = osl_Process_E_Unknown;
450 
451     OSL_PRECOND(pLocale, "osl_setProcessLocale(): Invalid parameter.");
452 
453     osl_acquireMutex(g_process_locale.m_mutex);
454     if (_imp_setProcessLocale (pLocale) == 0)
455     {
456         g_process_locale.m_pLocale = pLocale;
457         result = osl_Process_E_None;
458     }
459     osl_releaseMutex(g_process_locale.m_mutex);
460 
461     return (result);
462 }
463 
464