First public contribution.
1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // connectors for shared environment variables
29 Get string from environment.
30 @return A null-terminated string with the value of the requested environment
31 variable or NULL if that environment variable does not exist.
32 @param name Null-terminated string containing the name of the requested variable.
34 EXPORT_C char* getenv (const char* name)
36 return _getenv_r (_REENT, name);
40 A reentrant version of getenv().
42 EXPORT_C char* _getenv_r (struct _reent *r, const char* name)
44 // wchar_t tmpbuf[KMaxFullName+1]; //use the max path length possible
47 wchar_t * tmpbuf = (wchar_t*)malloc(2*(strlen(name)+1));
52 if (-1 != mbstowcs(tmpbuf, name, strlen(name)+1))
54 MSystemInterface& sysIf=Interface(r);
55 wchar_t * wideval = sysIf.getenv(tmpbuf);
61 int size = 1 + 2* wcslen(wideval);
63 if (size < _MINNARROWBUFSIZE)
64 size = _MINNARROWBUFSIZE;
66 if (size > r->_NEBSize)
68 //this string could be longer
69 char * p = (char*)realloc(r->_pNarrowEnvBuffer, size);
72 r->_pNarrowEnvBuffer = p;
78 if (wcstombs(r->_pNarrowEnvBuffer, wideval, r->_NEBSize) >= 0)
79 rval = r->_pNarrowEnvBuffer;
88 MapError(err, r->_errno);
93 EXPORT_C wchar_t* wgetenv (const wchar_t* name)
95 return _wgetenv_r (_REENT, name);
99 A reentrant version of wgetenv().
101 EXPORT_C wchar_t* _wgetenv_r (struct _reent *r, const wchar_t* name)
103 MSystemInterface& sysIf=Interface(r);
104 return sysIf.getenv(name);
108 Removes an environment variable.
110 EXPORT_C void unsetenv (const char* name)
112 _unsetenv_r (_REENT, name);
116 A reentrant version of unsetenv().
118 EXPORT_C void _unsetenv_r (struct _reent *r, const char* name)
121 wchar_t* tmpbuf = (wchar_t*)malloc(2*(strlen(name)+1));
125 if (-1 != mbstowcs(tmpbuf, name, strlen(name)+1))
127 MSystemInterface& sysIf=Interface(r);
128 sysIf.unsetenv(tmpbuf);
137 MapError(rval, r->_errno);
141 A wide-character version of a unsetenv()
143 EXPORT_C void wunsetenv (const wchar_t* name)
145 _wunsetenv_r (_REENT, name);
149 A reentrant version of wunsetenv().
151 EXPORT_C void _wunsetenv_r (struct _reent *r, const wchar_t* name)
153 MSystemInterface& sysIf=Interface(r);
154 sysIf.unsetenv(name);
158 Adds or changes an environment variable.
160 @return On Success, returns 0.
161 On Failure, returns -1, errno may be set and the environment shall be unchanged.
163 EXPORT_C int setenv (const char *name, const char *value, int rewrite)
165 struct _reent *r = _REENT2;
167 return -1; // Memory for library globals is not allocated (errno not set).
168 return _setenv_r(r, name, value, rewrite);
172 A reentrant version of setenv().
174 EXPORT_C int _setenv_r (struct _reent *r, const char *name, const char *value, int rewrite)
177 wchar_t* wname = (wchar_t*)malloc(2*(strlen(name)+1));
178 wchar_t* wvalue = (wchar_t*)malloc(2*(strlen(value)+1));
180 if (wname && wvalue) //the allocs were OK
182 if ((-1 != mbstowcs(wname, name, strlen(name)+1)) &&
183 (-1 != mbstowcs(wvalue, value, strlen(value)+1)))
185 MSystemInterface& sysIf=Interface(r);
186 rval = sysIf.setenv(wname, wvalue, rewrite, r->_errno);
189 rval = MapError(EILSEQ, r->_errno);
192 rval = MapError(ENOMEM, r->_errno);
200 A wide-character version of a setenv()
202 EXPORT_C int wsetenv (const wchar_t *name, const wchar_t *value, int rewrite)
204 struct _reent *r = _REENT2;
206 return -1; // Memory for library globals is not allocated (errno not set).
207 return _wsetenv_r(r, name, value, rewrite);
211 A reentrant version of wsetenv().
213 EXPORT_C int _wsetenv_r (struct _reent *r, const wchar_t *name, const wchar_t *value, int rewrite)
215 MSystemInterface& sysIf=Interface(r);
216 return sysIf.setenv(name,value,rewrite,r->_errno);