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 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sal.hxx"
34 #include "osl/process.h"
36 #ifndef INCLUDED_LIMITS_H
38 #define INCLUDED_LIMITS_H
41 #ifndef INCLUDED_PTHREAD_H
43 #define INCLUDED_PTHREAD_H
46 #ifndef INCLUDED_STDLIB_H
48 #define INCLUDED_STDLIB_H
51 #ifndef INCLUDED_STRING_H
53 #define INCLUDED_STRING_H
55 #include "osl/diagnose.h"
57 #include "osl/module.h"
58 #include "osl/thread.h"
59 #include "rtl/ustring.hxx"
61 #ifndef _OSL_FILE_PATH_HELPER_H_
62 #include "file_path_helper.h"
65 #ifndef _OSL_UUNXAPI_H_
69 /***************************************
70 osl_bootstrap_getExecutableFile_Impl().
76 **************************************/
78 extern "C" oslProcessError SAL_CALL
osl_bootstrap_getExecutableFile_Impl (
79 rtl_uString
** ppFileURL
80 ) SAL_THROW_EXTERN_C();
84 #include <mach-o/dyld.h>
86 oslProcessError SAL_CALL
osl_bootstrap_getExecutableFile_Impl (
87 rtl_uString
** ppFileURL
88 ) SAL_THROW_EXTERN_C()
90 oslProcessError result
= osl_Process_E_NotFound
;
92 char buffer
[PATH_MAX
];
93 size_t buflen
= sizeof(buffer
);
95 #if __GNUC__ >= 4 && defined(MACOSX)
96 if (_NSGetExecutablePath (buffer
, (uint32_t*)&buflen
) == 0)
98 if (_NSGetExecutablePath (buffer
, &buflen
) == 0)
101 /* Determine absolute path. */
102 char abspath
[PATH_MAX
];
103 if (realpath (buffer
, abspath
) != 0)
105 /* Convert from utf8 to unicode. */
106 rtl_uString
* pAbsPath
= 0;
109 abspath
, rtl_str_getLength (abspath
),
110 RTL_TEXTENCODING_UTF8
,
111 OSTRING_TO_OUSTRING_CVTFLAGS
);
115 /* Convert from path to url. */
116 if (osl_getFileURLFromSystemPath (pAbsPath
, ppFileURL
) == osl_File_E_None
)
119 result
= osl_Process_E_None
;
121 rtl_uString_release (pAbsPath
);
129 #elif !defined(NO_DL_FUNCTIONS)
132 oslProcessError SAL_CALL
osl_bootstrap_getExecutableFile_Impl (
133 rtl_uString
** ppFileURL
134 ) SAL_THROW_EXTERN_C()
136 oslProcessError result
= osl_Process_E_NotFound
;
138 /* Determine address of "main()" function. */
139 void * addr
= dlsym (RTLD_DEFAULT
, "main");
142 /* Determine module URL. */
143 if (osl_getModuleURLFromAddress (addr
, ppFileURL
))
146 result
= osl_Process_E_None
;
153 #else /* NO_DL_FUNCTIONS */
155 oslProcessError SAL_CALL
osl_bootstrap_getExecutableFile_Impl (
156 rtl_uString
** ppFileURL
157 ) SAL_THROW_EXTERN_C()
159 /* Fallback to ordinary osl_getExecutableFile(). */
160 return osl_getExecutableFile (ppFileURL
);
163 #endif /* NO_DL_FUNCTIONS */
165 /***************************************
167 **************************************/
168 struct CommandArgs_Impl
170 pthread_mutex_t m_mutex
;
172 rtl_uString
** m_ppArgs
;
175 static struct CommandArgs_Impl g_command_args
=
177 PTHREAD_MUTEX_INITIALIZER
,
182 /***************************************
183 osl_getExecutableFile().
184 **************************************/
185 oslProcessError SAL_CALL
osl_getExecutableFile (rtl_uString
** ppustrFile
)
187 oslProcessError result
= osl_Process_E_NotFound
;
189 pthread_mutex_lock (&(g_command_args
.m_mutex
));
190 if (g_command_args
.m_nCount
> 0)
192 /* CommandArgs set. Obtain argv[0]. */
193 rtl_uString_assign (ppustrFile
, g_command_args
.m_ppArgs
[0]);
194 result
= osl_Process_E_None
;
196 pthread_mutex_unlock (&(g_command_args
.m_mutex
));
201 /***************************************
202 osl_getCommandArgCount().
203 **************************************/
204 sal_uInt32 SAL_CALL
osl_getCommandArgCount (void)
206 sal_uInt32 result
= 0;
208 pthread_mutex_lock (&(g_command_args
.m_mutex
));
209 if (g_command_args
.m_nCount
> 0)
210 result
= g_command_args
.m_nCount
- 1;
211 pthread_mutex_unlock (&(g_command_args
.m_mutex
));
216 /***************************************
218 **************************************/
219 oslProcessError SAL_CALL
osl_getCommandArg (sal_uInt32 nArg
, rtl_uString
** strCommandArg
)
221 oslProcessError result
= osl_Process_E_NotFound
;
223 pthread_mutex_lock (&(g_command_args
.m_mutex
));
224 if (g_command_args
.m_nCount
> (nArg
+ 1))
226 rtl_uString_assign (strCommandArg
, g_command_args
.m_ppArgs
[nArg
+ 1]);
227 result
= osl_Process_E_None
;
229 pthread_mutex_unlock (&(g_command_args
.m_mutex
));
234 /***************************************
235 osl_setCommandArgs().
236 **************************************/
237 void SAL_CALL
osl_setCommandArgs (int argc
, char ** argv
)
239 pthread_mutex_lock (&(g_command_args
.m_mutex
));
240 OSL_ENSURE (g_command_args
.m_nCount
== 0, "osl_setCommandArgs(): CommandArgs already set.");
241 if (g_command_args
.m_nCount
== 0)
243 rtl_uString
** ppArgs
= (rtl_uString
**)rtl_allocateZeroMemory (argc
* sizeof(rtl_uString
*));
246 rtl_TextEncoding encoding
= osl_getThreadTextEncoding();
247 for (int i
= 0; i
< argc
; i
++)
251 argv
[i
], rtl_str_getLength (argv
[i
]), encoding
,
252 OSTRING_TO_OUSTRING_CVTFLAGS
);
256 /* see @ osl_getExecutableFile(). */
257 if (rtl_ustr_indexOfChar (rtl_uString_getStr(ppArgs
[0]), sal_Unicode('/')) == -1)
259 const rtl::OUString
PATH (RTL_CONSTASCII_USTRINGPARAM("PATH"));
261 rtl_uString
* pSearchPath
= 0;
262 osl_getEnvironment (PATH
.pData
, &pSearchPath
);
265 rtl_uString
* pSearchResult
= 0;
266 osl_searchPath (ppArgs
[0], pSearchPath
, &pSearchResult
);
269 rtl_uString_assign (&(ppArgs
[0]), pSearchResult
);
270 rtl_uString_release (pSearchResult
);
272 rtl_uString_release (pSearchPath
);
276 rtl_uString
* pArg0
= 0;
277 if (realpath_u (ppArgs
[0], &pArg0
))
279 osl_getFileURLFromSystemPath (pArg0
, &(ppArgs
[0]));
280 rtl_uString_release (pArg0
);
283 g_command_args
.m_nCount
= argc
;
284 g_command_args
.m_ppArgs
= ppArgs
;
287 pthread_mutex_unlock (&(g_command_args
.m_mutex
));
290 /***************************************
291 osl_getEnvironment().
292 **************************************/
293 oslProcessError SAL_CALL
osl_getEnvironment(rtl_uString
* pustrEnvVar
, rtl_uString
** ppustrValue
)
295 oslProcessError result
= osl_Process_E_NotFound
;
296 rtl_TextEncoding encoding
= osl_getThreadTextEncoding();
297 rtl_String
* pstr_env_var
= 0;
299 OSL_PRECOND(pustrEnvVar
, "osl_getEnvironment(): Invalid parameter");
300 OSL_PRECOND(ppustrValue
, "osl_getEnvironment(): Invalid parameter");
304 rtl_uString_getStr(pustrEnvVar
), rtl_uString_getLength(pustrEnvVar
), encoding
,
305 OUSTRING_TO_OSTRING_CVTFLAGS
);
306 if (pstr_env_var
!= 0)
308 const char* p_env_var
= getenv (rtl_string_getStr (pstr_env_var
));
313 p_env_var
, strlen(p_env_var
), encoding
,
314 OSTRING_TO_OUSTRING_CVTFLAGS
);
315 OSL_ASSERT(*ppustrValue
!= NULL
);
317 result
= osl_Process_E_None
;
319 rtl_string_release(pstr_env_var
);
325 /***************************************
326 osl_getProcessWorkingDir().
327 **************************************/
328 oslProcessError SAL_CALL
osl_getProcessWorkingDir(rtl_uString
**ppustrWorkingDir
)
330 oslProcessError result
= osl_Process_E_Unknown
;
331 char buffer
[PATH_MAX
];
333 OSL_PRECOND(ppustrWorkingDir
, "osl_getProcessWorkingDir(): Invalid parameter");
335 if (getcwd (buffer
, sizeof(buffer
)) != 0)
337 rtl_uString
* ustrTmp
= 0;
341 buffer
, strlen(buffer
), osl_getThreadTextEncoding(),
342 OSTRING_TO_OUSTRING_CVTFLAGS
);
345 if (osl_getFileURLFromSystemPath (ustrTmp
, ppustrWorkingDir
) == osl_File_E_None
)
346 result
= osl_Process_E_None
;
347 rtl_uString_release (ustrTmp
);
354 /******************************************************************************
356 * new functions to set/return the current process locale
358 *****************************************************************************/
360 struct ProcessLocale_Impl
362 pthread_mutex_t m_mutex
;
363 rtl_Locale
* m_pLocale
;
366 static struct ProcessLocale_Impl g_process_locale
=
368 PTHREAD_MUTEX_INITIALIZER
,
372 extern "C" void _imp_getProcessLocale( rtl_Locale
** );
373 extern "C" int _imp_setProcessLocale( rtl_Locale
* );
375 /**********************************************
376 osl_getProcessLocale().
377 *********************************************/
378 oslProcessError SAL_CALL
osl_getProcessLocale( rtl_Locale
** ppLocale
)
380 OSL_PRECOND(ppLocale
, "osl_getProcessLocale(): Invalid parameter.");
382 pthread_mutex_lock(&(g_process_locale
.m_mutex
));
384 if (g_process_locale
.m_pLocale
== 0)
385 _imp_getProcessLocale (&(g_process_locale
.m_pLocale
));
386 *ppLocale
= g_process_locale
.m_pLocale
;
388 pthread_mutex_unlock (&(g_process_locale
.m_mutex
));
390 return (osl_Process_E_None
);
393 /**********************************************
394 osl_setProcessLocale().
395 *********************************************/
396 oslProcessError SAL_CALL
osl_setProcessLocale( rtl_Locale
* pLocale
)
398 oslProcessError result
= osl_Process_E_Unknown
;
400 OSL_PRECOND(pLocale
, "osl_setProcessLocale(): Invalid parameter.");
402 pthread_mutex_lock(&(g_process_locale
.m_mutex
));
403 if (_imp_setProcessLocale (pLocale
) == 0)
405 g_process_locale
.m_pLocale
= pLocale
;
406 result
= osl_Process_E_None
;
408 pthread_mutex_unlock (&(g_process_locale
.m_mutex
));