1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: process_impl.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
35 #include "osl/process.h"
36 #include <osl/mutex.h>
38 #ifndef INCLUDED_LIMITS_H
40 #define INCLUDED_LIMITS_H
43 #ifndef INCLUDED_PTHREAD_H
45 #define INCLUDED_PTHREAD_H
48 #ifndef INCLUDED_STDLIB_H
50 #define INCLUDED_STDLIB_H
53 #ifndef INCLUDED_STRING_H
55 #define INCLUDED_STRING_H
57 #include "osl/diagnose.h"
59 #include "osl/module.h"
60 #include "osl/thread.h"
61 #include "rtl/ustring.hxx"
63 #ifndef _OSL_FILE_PATH_HELPER_H_
64 #include "file_path_helper.h"
67 #ifndef _OSL_UUNXAPI_H_
71 /***************************************
72 osl_bootstrap_getExecutableFile_Impl().
78 **************************************/
80 extern "C" oslProcessError SAL_CALL
osl_bootstrap_getExecutableFile_Impl (
81 rtl_uString
** ppFileURL
82 ) SAL_THROW_EXTERN_C();
85 oslProcessError SAL_CALL
osl_bootstrap_getExecutableFile_Impl (
86 rtl_uString
** ppFileURL
87 ) SAL_THROW_EXTERN_C()
89 oslProcessError result
= osl_Process_E_NotFound
;
90 CHAR szName
[CCHMAXPATH
];
95 rc
= DosGetInfoBlocks(&ptib
, &ppib
);
96 rc
= DosQueryModuleName(ppib
->pib_hmte
, sizeof(szName
), szName
);
100 rtl_uString
* pAbsPath
= 0;
104 szName
, strlen(szName
), osl_getThreadTextEncoding(),
105 OSTRING_TO_OUSTRING_CVTFLAGS
);
109 /* Convert from path to url. */
110 if (osl_getFileURLFromSystemPath (pAbsPath
, ppFileURL
) == osl_File_E_None
)
113 result
= osl_Process_E_None
;
115 rtl_uString_release (pAbsPath
);
122 /***************************************
124 **************************************/
125 struct CommandArgs_Impl
129 rtl_uString
** m_ppArgs
;
132 static struct CommandArgs_Impl g_command_args
=
139 /***************************************
140 osl_getExecutableFile().
141 **************************************/
142 oslProcessError SAL_CALL
osl_getExecutableFile (rtl_uString
** ppustrFile
)
144 oslProcessError result
= osl_Process_E_NotFound
;
146 osl_acquireMutex(g_command_args
.m_mutex
);
147 if (g_command_args
.m_nCount
> 0)
149 /* CommandArgs set. Obtain argv[0]. */
150 rtl_uString_assign (ppustrFile
, g_command_args
.m_ppArgs
[0]);
151 result
= osl_Process_E_None
;
153 osl_releaseMutex(g_command_args
.m_mutex
);
158 /***************************************
159 osl_getCommandArgCount().
160 **************************************/
161 sal_uInt32 SAL_CALL
osl_getCommandArgCount (void)
163 sal_uInt32 result
= 0;
165 osl_acquireMutex(g_command_args
.m_mutex
);
166 if (g_command_args
.m_nCount
> 0)
167 result
= g_command_args
.m_nCount
- 1;
168 osl_releaseMutex(g_command_args
.m_mutex
);
173 /***************************************
175 **************************************/
176 oslProcessError SAL_CALL
osl_getCommandArg (sal_uInt32 nArg
, rtl_uString
** strCommandArg
)
178 oslProcessError result
= osl_Process_E_NotFound
;
180 osl_acquireMutex(g_command_args
.m_mutex
);
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
)
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_getProcessWorkingDir().
286 **************************************/
287 oslProcessError SAL_CALL
osl_getProcessWorkingDir(rtl_uString
**ppustrWorkingDir
)
289 oslProcessError result
= osl_Process_E_Unknown
;
290 char buffer
[PATH_MAX
];
292 OSL_PRECOND(ppustrWorkingDir
, "osl_getProcessWorkingDir(): Invalid parameter");
294 if (getcwd (buffer
, sizeof(buffer
)) != 0)
296 rtl_uString
* ustrTmp
= 0;
300 buffer
, strlen(buffer
), osl_getThreadTextEncoding(),
301 OSTRING_TO_OUSTRING_CVTFLAGS
);
304 if (osl_getFileURLFromSystemPath (ustrTmp
, ppustrWorkingDir
) == osl_File_E_None
)
305 result
= osl_Process_E_None
;
306 rtl_uString_release (ustrTmp
);
313 /******************************************************************************
315 * new functions to set/return the current process locale
317 *****************************************************************************/
319 struct ProcessLocale_Impl
322 rtl_Locale
* m_pLocale
;
325 static struct ProcessLocale_Impl g_process_locale
=
331 extern "C" void _imp_getProcessLocale( rtl_Locale
** );
332 extern "C" int _imp_setProcessLocale( rtl_Locale
* );
334 /**********************************************
335 osl_getProcessLocale().
336 *********************************************/
337 oslProcessError SAL_CALL
osl_getProcessLocale( rtl_Locale
** ppLocale
)
339 OSL_PRECOND(ppLocale
, "osl_getProcessLocale(): Invalid parameter.");
341 osl_acquireMutex(g_process_locale
.m_mutex
);
343 if (g_process_locale
.m_pLocale
== 0)
344 _imp_getProcessLocale (&(g_process_locale
.m_pLocale
));
345 *ppLocale
= g_process_locale
.m_pLocale
;
347 osl_releaseMutex(g_process_locale
.m_mutex
);
349 return (osl_Process_E_None
);
352 /**********************************************
353 osl_setProcessLocale().
354 *********************************************/
355 oslProcessError SAL_CALL
osl_setProcessLocale( rtl_Locale
* pLocale
)
357 oslProcessError result
= osl_Process_E_Unknown
;
359 OSL_PRECOND(pLocale
, "osl_setProcessLocale(): Invalid parameter.");
361 osl_acquireMutex(g_process_locale
.m_mutex
);
362 if (_imp_setProcessLocale (pLocale
) == 0)
364 g_process_locale
.m_pLocale
= pLocale
;
365 result
= osl_Process_E_None
;
367 osl_releaseMutex(g_process_locale
.m_mutex
);