sl@0: // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // connectors for shared environment variables sl@0: // sl@0: // sl@0: sl@0: #include "SYSIF.H" sl@0: #include "LPOSIX.H" sl@0: #include sl@0: #include sl@0: sl@0: sl@0: #include sl@0: sl@0: extern "C" { sl@0: sl@0: /** sl@0: Get string from environment. sl@0: @return A null-terminated string with the value of the requested environment sl@0: variable or NULL if that environment variable does not exist. sl@0: @param name Null-terminated string containing the name of the requested variable. sl@0: */ sl@0: EXPORT_C char* getenv (const char* name) sl@0: { sl@0: return _getenv_r (_REENT, name); sl@0: } sl@0: sl@0: /** sl@0: A reentrant version of getenv(). sl@0: */ sl@0: EXPORT_C char* _getenv_r (struct _reent *r, const char* name) sl@0: { sl@0: // wchar_t tmpbuf[KMaxFullName+1]; //use the max path length possible sl@0: char * rval = NULL; sl@0: int err = 0; sl@0: wchar_t * tmpbuf = (wchar_t*)malloc(2*(strlen(name)+1)); sl@0: sl@0: if (tmpbuf) sl@0: { sl@0: sl@0: if (-1 != mbstowcs(tmpbuf, name, strlen(name)+1)) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: wchar_t * wideval = sysIf.getenv(tmpbuf); sl@0: sl@0: if (wideval) sl@0: { sl@0: sl@0: //get the max size sl@0: int size = 1 + 2* wcslen(wideval); sl@0: sl@0: if (size < _MINNARROWBUFSIZE) sl@0: size = _MINNARROWBUFSIZE; sl@0: sl@0: if (size > r->_NEBSize) sl@0: { sl@0: //this string could be longer sl@0: char * p = (char*)realloc(r->_pNarrowEnvBuffer, size); sl@0: if (p) sl@0: { sl@0: r->_pNarrowEnvBuffer = p; sl@0: r->_NEBSize = size; sl@0: } sl@0: else sl@0: err = ENOMEM; sl@0: } sl@0: if (wcstombs(r->_pNarrowEnvBuffer, wideval, r->_NEBSize) >= 0) sl@0: rval = r->_pNarrowEnvBuffer; sl@0: sl@0: } sl@0: } sl@0: else sl@0: err = EILSEQ; sl@0: sl@0: } sl@0: sl@0: MapError(err, r->_errno); sl@0: free(tmpbuf); sl@0: return rval; sl@0: } sl@0: sl@0: EXPORT_C wchar_t* wgetenv (const wchar_t* name) sl@0: { sl@0: return _wgetenv_r (_REENT, name); sl@0: } sl@0: sl@0: /** sl@0: A reentrant version of wgetenv(). sl@0: */ sl@0: EXPORT_C wchar_t* _wgetenv_r (struct _reent *r, const wchar_t* name) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.getenv(name); sl@0: } sl@0: sl@0: /** sl@0: Removes an environment variable. sl@0: */ sl@0: EXPORT_C void unsetenv (const char* name) sl@0: { sl@0: _unsetenv_r (_REENT, name); sl@0: } sl@0: sl@0: /** sl@0: A reentrant version of unsetenv(). sl@0: */ sl@0: EXPORT_C void _unsetenv_r (struct _reent *r, const char* name) sl@0: { sl@0: int rval = 0; sl@0: wchar_t* tmpbuf = (wchar_t*)malloc(2*(strlen(name)+1)); sl@0: sl@0: if (tmpbuf) sl@0: { sl@0: if (-1 != mbstowcs(tmpbuf, name, strlen(name)+1)) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: sysIf.unsetenv(tmpbuf); sl@0: } sl@0: else sl@0: rval = EILSEQ; sl@0: } sl@0: else sl@0: rval = ENOMEM; sl@0: sl@0: free(tmpbuf); sl@0: MapError(rval, r->_errno); sl@0: } sl@0: sl@0: /** sl@0: A wide-character version of a unsetenv() sl@0: */ sl@0: EXPORT_C void wunsetenv (const wchar_t* name) sl@0: { sl@0: _wunsetenv_r (_REENT, name); sl@0: } sl@0: sl@0: /** sl@0: A reentrant version of wunsetenv(). sl@0: */ sl@0: EXPORT_C void _wunsetenv_r (struct _reent *r, const wchar_t* name) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: sysIf.unsetenv(name); sl@0: } sl@0: sl@0: /** sl@0: Adds or changes an environment variable. sl@0: sl@0: @return On Success, returns 0. sl@0: On Failure, returns -1, errno may be set and the environment shall be unchanged. sl@0: */ sl@0: EXPORT_C int setenv (const char *name, const char *value, int rewrite) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _setenv_r(r, name, value, rewrite); sl@0: } sl@0: sl@0: /** sl@0: A reentrant version of setenv(). sl@0: */ sl@0: EXPORT_C int _setenv_r (struct _reent *r, const char *name, const char *value, int rewrite) sl@0: { sl@0: int rval = 0; sl@0: wchar_t* wname = (wchar_t*)malloc(2*(strlen(name)+1)); sl@0: wchar_t* wvalue = (wchar_t*)malloc(2*(strlen(value)+1)); sl@0: sl@0: if (wname && wvalue) //the allocs were OK sl@0: { sl@0: if ((-1 != mbstowcs(wname, name, strlen(name)+1)) && sl@0: (-1 != mbstowcs(wvalue, value, strlen(value)+1))) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: rval = sysIf.setenv(wname, wvalue, rewrite, r->_errno); sl@0: } sl@0: else sl@0: rval = MapError(EILSEQ, r->_errno); sl@0: } sl@0: else sl@0: rval = MapError(ENOMEM, r->_errno); sl@0: sl@0: free (wname); sl@0: free (wvalue); sl@0: return rval; sl@0: } sl@0: sl@0: /** sl@0: A wide-character version of a setenv() sl@0: */ sl@0: EXPORT_C int wsetenv (const wchar_t *name, const wchar_t *value, int rewrite) sl@0: { sl@0: struct _reent *r = _REENT2; sl@0: if (!r) sl@0: return -1; // Memory for library globals is not allocated (errno not set). sl@0: return _wsetenv_r(r, name, value, rewrite); sl@0: } sl@0: sl@0: /** sl@0: A reentrant version of wsetenv(). sl@0: */ sl@0: EXPORT_C int _wsetenv_r (struct _reent *r, const wchar_t *name, const wchar_t *value, int rewrite) sl@0: { sl@0: MSystemInterface& sysIf=Interface(r); sl@0: return sysIf.setenv(name,value,rewrite,r->_errno); sl@0: } sl@0: sl@0: } // extern "C"