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 ************************************************************************/
32 #include "osl/process.h"
33 #include <osl/mutex.h>
35 #ifndef INCLUDED_LIMITS_H
37 #define INCLUDED_LIMITS_H
40 #ifndef INCLUDED_PTHREAD_H
42 #define INCLUDED_PTHREAD_H
45 #ifndef INCLUDED_STDLIB_H
47 #define INCLUDED_STDLIB_H
50 #ifndef INCLUDED_STRING_H
52 #define INCLUDED_STRING_H
54 #include "osl/diagnose.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"
64 #ifndef _OSL_UUNXAPI_H_
68 /***************************************
69 osl_bootstrap_getExecutableFile_Impl().
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
];
92 rc
= DosGetInfoBlocks(&ptib
, &ppib
);
93 rc
= DosQueryModuleName(ppib
->pib_hmte
, sizeof(szName
), szName
);
97 rtl_uString
* pAbsPath
= 0;
101 szName
, strlen(szName
), osl_getThreadTextEncoding(),
102 OSTRING_TO_OUSTRING_CVTFLAGS
);
106 /* Convert from path to url. */
107 if (osl_getFileURLFromSystemPath (pAbsPath
, ppFileURL
) == osl_File_E_None
)
110 result
= osl_Process_E_None
;
112 rtl_uString_release (pAbsPath
);
119 /***************************************
121 **************************************/
122 struct CommandArgs_Impl
126 rtl_uString
** m_ppArgs
;
129 static struct CommandArgs_Impl g_command_args
=
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
);
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
);
172 /***************************************
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
);
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
*));
204 rtl_TextEncoding encoding
= osl_getThreadTextEncoding();
205 for (int i
= 0; i
< argc
; i
++)
209 argv
[i
], rtl_str_getLength (argv
[i
]), encoding
,
210 OSTRING_TO_OUSTRING_CVTFLAGS
);
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
);
223 rtl_uString
* pSearchResult
= 0;
224 osl_searchPath (ppArgs
[0], pSearchPath
, &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");
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
));
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
);
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");
299 rtl_uString_getStr(pustrEnvVar
), rtl_uString_getLength(pustrEnvVar
), encoding
,
300 OUSTRING_TO_OSTRING_CVTFLAGS
);
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,
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
;
325 rtl_string_release(pBuffer
);
329 rtl_string_release(pstr_val
);
331 if (pstr_env_var
!= 0)
332 rtl_string_release(pstr_env_var
);
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");
350 rtl_uString_getStr(pustrEnvVar
), rtl_uString_getLength(pustrEnvVar
), encoding
,
351 OUSTRING_TO_OSTRING_CVTFLAGS
);
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
;
368 rtl_string_release(pBuffer
);
370 rtl_string_release(pstr_env_var
);
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;
392 buffer
, strlen(buffer
), osl_getThreadTextEncoding(),
393 OSTRING_TO_OUSTRING_CVTFLAGS
);
396 if (osl_getFileURLFromSystemPath (ustrTmp
, ppustrWorkingDir
) == osl_File_E_None
)
397 result
= osl_Process_E_None
;
398 rtl_uString_release (ustrTmp
);
405 /******************************************************************************
407 * new functions to set/return the current process locale
409 *****************************************************************************/
411 struct ProcessLocale_Impl
414 rtl_Locale
* m_pLocale
;
417 static struct ProcessLocale_Impl g_process_locale
=
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
);