merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / osl / os2 / process_impl.cxx
blobbe6bb1898e6576e8a014105ca4d3c46416dbcdce
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
29 #define INCL_DOS
30 #include <os2.h>
32 #include "osl/process.h"
33 #include <osl/mutex.h>
35 #ifndef INCLUDED_LIMITS_H
36 #include <limits.h>
37 #define INCLUDED_LIMITS_H
38 #endif
40 #ifndef INCLUDED_PTHREAD_H
41 #include <pthread.h>
42 #define INCLUDED_PTHREAD_H
43 #endif
45 #ifndef INCLUDED_STDLIB_H
46 #include <stdlib.h>
47 #define INCLUDED_STDLIB_H
48 #endif
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"
60 #ifndef _OSL_FILE_PATH_HELPER_H_
61 #include "file_path_helper.h"
62 #endif
64 #ifndef _OSL_UUNXAPI_H_
65 #include "uunxapi.h"
66 #endif
68 /***************************************
69 osl_bootstrap_getExecutableFile_Impl().
71 @internal
72 @see rtl_bootstrap
73 @see #i37371#
75 **************************************/
77 extern "C" oslProcessError SAL_CALL osl_bootstrap_getExecutableFile_Impl (
78 rtl_uString ** ppFileURL
79 ) SAL_THROW_EXTERN_C();
82 oslProcessError SAL_CALL osl_bootstrap_getExecutableFile_Impl (
83 rtl_uString ** ppFileURL
84 ) SAL_THROW_EXTERN_C()
86 oslProcessError result = osl_Process_E_NotFound;
87 CHAR szName[CCHMAXPATH];
88 PPIB ppib;
89 PTIB ptib;
90 APIRET rc;
92 rc = DosGetInfoBlocks(&ptib, &ppib);
93 rc = DosQueryModuleName(ppib->pib_hmte, sizeof(szName), szName);
95 if (rc == 0)
97 rtl_uString * pAbsPath = 0;
99 rtl_string2UString(
100 &pAbsPath,
101 szName, strlen(szName), osl_getThreadTextEncoding(),
102 OSTRING_TO_OUSTRING_CVTFLAGS);
104 if (pAbsPath)
106 /* Convert from path to url. */
107 if (osl_getFileURLFromSystemPath (pAbsPath, ppFileURL) == osl_File_E_None)
109 /* Success. */
110 result = osl_Process_E_None;
112 rtl_uString_release (pAbsPath);
116 return (result);
119 /***************************************
120 CommandArgs_Impl.
121 **************************************/
122 struct CommandArgs_Impl
124 oslMutex m_mutex;
125 sal_uInt32 m_nCount;
126 rtl_uString ** m_ppArgs;
129 static struct CommandArgs_Impl g_command_args =
131 osl_createMutex(),
136 /***************************************
137 osl_getExecutableFile().
138 **************************************/
139 oslProcessError SAL_CALL osl_getExecutableFile (rtl_uString ** ppustrFile)
141 oslProcessError result = osl_Process_E_NotFound;
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)
147 /* CommandArgs set. Obtain argv[0]. */
148 rtl_uString_assign (ppustrFile, g_command_args.m_ppArgs[0]);
149 result = osl_Process_E_None;
151 osl_releaseMutex(g_command_args.m_mutex);
153 return (result);
156 /***************************************
157 osl_getCommandArgCount().
158 **************************************/
159 sal_uInt32 SAL_CALL osl_getCommandArgCount (void)
161 sal_uInt32 result = 0;
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);
169 return (result);
172 /***************************************
173 osl_getCommandArg().
174 **************************************/
175 oslProcessError SAL_CALL osl_getCommandArg (sal_uInt32 nArg, rtl_uString ** strCommandArg)
177 oslProcessError result = osl_Process_E_NotFound;
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))
183 rtl_uString_assign (strCommandArg, g_command_args.m_ppArgs[nArg + 1]);
184 result = osl_Process_E_None;
186 osl_releaseMutex(g_command_args.m_mutex);
188 return (result);
191 /***************************************
192 osl_setCommandArgs().
193 **************************************/
194 void SAL_CALL osl_setCommandArgs (int argc, char ** argv)
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)
201 rtl_uString** ppArgs = (rtl_uString**)rtl_allocateZeroMemory (argc * sizeof(rtl_uString*));
202 if (ppArgs != 0)
204 rtl_TextEncoding encoding = osl_getThreadTextEncoding();
205 for (int i = 0; i < argc; i++)
207 rtl_string2UString (
208 &(ppArgs[i]),
209 argv[i], rtl_str_getLength (argv[i]), encoding,
210 OSTRING_TO_OUSTRING_CVTFLAGS);
212 if (ppArgs[0] != 0)
214 /* see @ osl_getExecutableFile(). */
215 if (rtl_ustr_indexOfChar (rtl_uString_getStr(ppArgs[0]), sal_Unicode('/')) == -1)
217 const rtl::OUString PATH (RTL_CONSTASCII_USTRINGPARAM("PATH"));
219 rtl_uString * pSearchPath = 0;
220 osl_getEnvironment (PATH.pData, &pSearchPath);
221 if (pSearchPath)
223 rtl_uString * pSearchResult = 0;
224 osl_searchPath (ppArgs[0], pSearchPath, &pSearchResult);
225 if (pSearchResult)
227 rtl_uString_assign (&(ppArgs[0]), pSearchResult);
228 rtl_uString_release (pSearchResult);
230 rtl_uString_release (pSearchPath);
234 rtl_uString * pArg0 = 0;
235 if (realpath_u (ppArgs[0], &pArg0))
237 osl_getFileURLFromSystemPath (pArg0, &(ppArgs[0]));
238 rtl_uString_release (pArg0);
241 g_command_args.m_nCount = argc;
242 g_command_args.m_ppArgs = ppArgs;
245 osl_releaseMutex(g_command_args.m_mutex);
249 /***************************************
250 osl_getEnvironment().
251 **************************************/
252 oslProcessError SAL_CALL osl_getEnvironment(rtl_uString* pustrEnvVar, rtl_uString** ppustrValue)
254 oslProcessError result = osl_Process_E_NotFound;
255 rtl_TextEncoding encoding = osl_getThreadTextEncoding();
256 rtl_String* pstr_env_var = 0;
258 OSL_PRECOND(pustrEnvVar, "osl_getEnvironment(): Invalid parameter");
259 OSL_PRECOND(ppustrValue, "osl_getEnvironment(): Invalid parameter");
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)
267 const char* p_env_var = getenv (rtl_string_getStr (pstr_env_var));
268 if (p_env_var != 0)
270 rtl_string2UString(
271 ppustrValue,
272 p_env_var, strlen(p_env_var), encoding,
273 OSTRING_TO_OUSTRING_CVTFLAGS);
274 OSL_ASSERT(*ppustrValue != NULL);
276 result = osl_Process_E_None;
278 rtl_string_release(pstr_env_var);
281 return (result);
284 /***************************************
285 osl_setEnvironment().
286 **************************************/
287 oslProcessError SAL_CALL osl_setEnvironment(rtl_uString* pustrEnvVar, rtl_uString* pustrValue)
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;
294 OSL_PRECOND(pustrEnvVar, "osl_setEnvironment(): Invalid parameter");
295 OSL_PRECOND(pustrValue, "osl_setEnvironment(): Invalid parameter");
297 rtl_uString2String(
298 &pstr_env_var,
299 rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
300 OUSTRING_TO_OSTRING_CVTFLAGS);
302 rtl_uString2String(
303 &pstr_val,
304 rtl_uString_getStr(pustrValue), rtl_uString_getLength(pustrValue), encoding,
305 OUSTRING_TO_OSTRING_CVTFLAGS);
307 if (pstr_env_var != 0 && pstr_val != 0)
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;
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) );
320 rtl_string_acquire(pBuffer); // argument to putenv must leak on success
322 if (putenv(rtl_string_getStr(pBuffer)) == 0)
323 result = osl_Process_E_None;
324 else
325 rtl_string_release(pBuffer);
328 if (pstr_val)
329 rtl_string_release(pstr_val);
331 if (pstr_env_var != 0)
332 rtl_string_release(pstr_env_var);
334 return (result);
337 /***************************************
338 osl_clearEnvironment().
339 **************************************/
340 oslProcessError SAL_CALL osl_clearEnvironment(rtl_uString* pustrEnvVar)
342 oslProcessError result = osl_Process_E_Unknown;
343 rtl_TextEncoding encoding = osl_getThreadTextEncoding();
344 rtl_String* pstr_env_var = 0;
346 OSL_PRECOND(pustrEnvVar, "osl_setEnvironment(): Invalid parameter");
348 rtl_uString2String(
349 &pstr_env_var,
350 rtl_uString_getStr(pustrEnvVar), rtl_uString_getLength(pustrEnvVar), encoding,
351 OUSTRING_TO_OSTRING_CVTFLAGS);
353 if (pstr_env_var)
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;
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);
363 rtl_string_acquire(pBuffer); // argument to putenv must leak on success
365 if (putenv(rtl_string_getStr(pBuffer)) == 0)
366 result = osl_Process_E_None;
367 else
368 rtl_string_release(pBuffer);
370 rtl_string_release(pstr_env_var);
373 return (result);
376 /***************************************
377 osl_getProcessWorkingDir().
378 **************************************/
379 oslProcessError SAL_CALL osl_getProcessWorkingDir(rtl_uString **ppustrWorkingDir)
381 oslProcessError result = osl_Process_E_Unknown;
382 char buffer[PATH_MAX];
384 OSL_PRECOND(ppustrWorkingDir, "osl_getProcessWorkingDir(): Invalid parameter");
386 if (getcwd (buffer, sizeof(buffer)) != 0)
388 rtl_uString* ustrTmp = 0;
390 rtl_string2UString(
391 &ustrTmp,
392 buffer, strlen(buffer), osl_getThreadTextEncoding(),
393 OSTRING_TO_OUSTRING_CVTFLAGS);
394 if (ustrTmp != 0)
396 if (osl_getFileURLFromSystemPath (ustrTmp, ppustrWorkingDir) == osl_File_E_None)
397 result = osl_Process_E_None;
398 rtl_uString_release (ustrTmp);
402 return (result);
405 /******************************************************************************
407 * new functions to set/return the current process locale
409 *****************************************************************************/
411 struct ProcessLocale_Impl
413 oslMutex m_mutex;
414 rtl_Locale * m_pLocale;
417 static struct ProcessLocale_Impl g_process_locale =
419 osl_createMutex(),
423 extern "C" void _imp_getProcessLocale( rtl_Locale ** );
424 extern "C" int _imp_setProcessLocale( rtl_Locale * );
426 /**********************************************
427 osl_getProcessLocale().
428 *********************************************/
429 oslProcessError SAL_CALL osl_getProcessLocale( rtl_Locale ** ppLocale )
431 OSL_PRECOND(ppLocale, "osl_getProcessLocale(): Invalid parameter.");
433 osl_acquireMutex(g_process_locale.m_mutex);
435 if (g_process_locale.m_pLocale == 0)
436 _imp_getProcessLocale (&(g_process_locale.m_pLocale));
437 *ppLocale = g_process_locale.m_pLocale;
439 osl_releaseMutex(g_process_locale.m_mutex);
441 return (osl_Process_E_None);
444 /**********************************************
445 osl_setProcessLocale().
446 *********************************************/
447 oslProcessError SAL_CALL osl_setProcessLocale( rtl_Locale * pLocale )
449 oslProcessError result = osl_Process_E_Unknown;
451 OSL_PRECOND(pLocale, "osl_setProcessLocale(): Invalid parameter.");
453 osl_acquireMutex(g_process_locale.m_mutex);
454 if (_imp_setProcessLocale (pLocale) == 0)
456 g_process_locale.m_pLocale = pLocale;
457 result = osl_Process_E_None;
459 osl_releaseMutex(g_process_locale.m_mutex);
461 return (result);