update dev300-m58
[ooovba.git] / sal / osl / os2 / process_impl.cxx
blobce1e97e9efd201f29ed97118ea0a6e0586c2b183
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: process_impl.cxx,v $
10 * $Revision: 1.6 $
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 ************************************************************************/
32 #define INCL_DOS
33 #include <os2.h>
35 #include "osl/process.h"
36 #include <osl/mutex.h>
38 #ifndef INCLUDED_LIMITS_H
39 #include <limits.h>
40 #define INCLUDED_LIMITS_H
41 #endif
43 #ifndef INCLUDED_PTHREAD_H
44 #include <pthread.h>
45 #define INCLUDED_PTHREAD_H
46 #endif
48 #ifndef INCLUDED_STDLIB_H
49 #include <stdlib.h>
50 #define INCLUDED_STDLIB_H
51 #endif
53 #ifndef INCLUDED_STRING_H
54 #include <string.h>
55 #define INCLUDED_STRING_H
56 #endif
57 #include "osl/diagnose.h"
58 #include <osl/file.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"
65 #endif
67 #ifndef _OSL_UUNXAPI_H_
68 #include "uunxapi.h"
69 #endif
71 /***************************************
72 osl_bootstrap_getExecutableFile_Impl().
74 @internal
75 @see rtl_bootstrap
76 @see #i37371#
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];
91 PPIB ppib;
92 PTIB ptib;
93 APIRET rc;
95 rc = DosGetInfoBlocks(&ptib, &ppib);
96 rc = DosQueryModuleName(ppib->pib_hmte, sizeof(szName), szName);
98 if (rc == 0)
100 rtl_uString * pAbsPath = 0;
102 rtl_string2UString(
103 &pAbsPath,
104 szName, strlen(szName), osl_getThreadTextEncoding(),
105 OSTRING_TO_OUSTRING_CVTFLAGS);
107 if (pAbsPath)
109 /* Convert from path to url. */
110 if (osl_getFileURLFromSystemPath (pAbsPath, ppFileURL) == osl_File_E_None)
112 /* Success. */
113 result = osl_Process_E_None;
115 rtl_uString_release (pAbsPath);
119 return (result);
122 /***************************************
123 CommandArgs_Impl.
124 **************************************/
125 struct CommandArgs_Impl
127 oslMutex m_mutex;
128 sal_uInt32 m_nCount;
129 rtl_uString ** m_ppArgs;
132 static struct CommandArgs_Impl g_command_args =
134 osl_createMutex(),
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);
155 return (result);
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);
170 return (result);
173 /***************************************
174 osl_getCommandArg().
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);
188 return (result);
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*));
202 if (ppArgs != 0)
204 rtl_TextEncoding encoding = osl_getThreadTextEncoding();
205 for (int i = 0; i < argc; i++)
207 rtl_string2UString (
208 &(ppArgs[i]),
209 argv[i], rtl_str_getLength (argv[i]), encoding,
210 OSTRING_TO_OUSTRING_CVTFLAGS);
212 if (ppArgs[0] != 0)
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);
221 if (pSearchPath)
223 rtl_uString * pSearchResult = 0;
224 osl_searchPath (ppArgs[0], pSearchPath, &pSearchResult);
225 if (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");
261 rtl_uString2String(
262 &pstr_env_var,
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));
268 if (p_env_var != 0)
270 rtl_string2UString(
271 ppustrValue,
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);
281 return (result);
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;
298 rtl_string2UString(
299 &ustrTmp,
300 buffer, strlen(buffer), osl_getThreadTextEncoding(),
301 OSTRING_TO_OUSTRING_CVTFLAGS);
302 if (ustrTmp != 0)
304 if (osl_getFileURLFromSystemPath (ustrTmp, ppustrWorkingDir) == osl_File_E_None)
305 result = osl_Process_E_None;
306 rtl_uString_release (ustrTmp);
310 return (result);
313 /******************************************************************************
315 * new functions to set/return the current process locale
317 *****************************************************************************/
319 struct ProcessLocale_Impl
321 oslMutex m_mutex;
322 rtl_Locale * m_pLocale;
325 static struct ProcessLocale_Impl g_process_locale =
327 osl_createMutex(),
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);
369 return (result);