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