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