1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LSTDLIB/ENVCALLS.CPP Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,219 @@
1.4 +// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// connectors for shared environment variables
1.18 +//
1.19 +//
1.20 +
1.21 +#include "SYSIF.H"
1.22 +#include "LPOSIX.H"
1.23 +#include <errno.h>
1.24 +#include <string.h>
1.25 +
1.26 +
1.27 +#include <stdlib_r.h>
1.28 +
1.29 +extern "C" {
1.30 +
1.31 +/**
1.32 +Get string from environment.
1.33 +@return A null-terminated string with the value of the requested environment
1.34 +variable or NULL if that environment variable does not exist.
1.35 +@param name Null-terminated string containing the name of the requested variable.
1.36 +*/
1.37 +EXPORT_C char* getenv (const char* name)
1.38 + {
1.39 + return _getenv_r (_REENT, name);
1.40 + }
1.41 +
1.42 +/**
1.43 +A reentrant version of getenv().
1.44 +*/
1.45 +EXPORT_C char* _getenv_r (struct _reent *r, const char* name)
1.46 + {
1.47 +// wchar_t tmpbuf[KMaxFullName+1]; //use the max path length possible
1.48 + char * rval = NULL;
1.49 + int err = 0;
1.50 + wchar_t * tmpbuf = (wchar_t*)malloc(2*(strlen(name)+1));
1.51 +
1.52 + if (tmpbuf)
1.53 + {
1.54 +
1.55 + if (-1 != mbstowcs(tmpbuf, name, strlen(name)+1))
1.56 + {
1.57 + MSystemInterface& sysIf=Interface(r);
1.58 + wchar_t * wideval = sysIf.getenv(tmpbuf);
1.59 +
1.60 + if (wideval)
1.61 + {
1.62 +
1.63 + //get the max size
1.64 + int size = 1 + 2* wcslen(wideval);
1.65 +
1.66 + if (size < _MINNARROWBUFSIZE)
1.67 + size = _MINNARROWBUFSIZE;
1.68 +
1.69 + if (size > r->_NEBSize)
1.70 + {
1.71 + //this string could be longer
1.72 + char * p = (char*)realloc(r->_pNarrowEnvBuffer, size);
1.73 + if (p)
1.74 + {
1.75 + r->_pNarrowEnvBuffer = p;
1.76 + r->_NEBSize = size;
1.77 + }
1.78 + else
1.79 + err = ENOMEM;
1.80 + }
1.81 + if (wcstombs(r->_pNarrowEnvBuffer, wideval, r->_NEBSize) >= 0)
1.82 + rval = r->_pNarrowEnvBuffer;
1.83 +
1.84 + }
1.85 + }
1.86 + else
1.87 + err = EILSEQ;
1.88 +
1.89 + }
1.90 +
1.91 + MapError(err, r->_errno);
1.92 + free(tmpbuf);
1.93 + return rval;
1.94 + }
1.95 +
1.96 +EXPORT_C wchar_t* wgetenv (const wchar_t* name)
1.97 + {
1.98 + return _wgetenv_r (_REENT, name);
1.99 + }
1.100 +
1.101 +/**
1.102 +A reentrant version of wgetenv().
1.103 +*/
1.104 +EXPORT_C wchar_t* _wgetenv_r (struct _reent *r, const wchar_t* name)
1.105 + {
1.106 + MSystemInterface& sysIf=Interface(r);
1.107 + return sysIf.getenv(name);
1.108 + }
1.109 +
1.110 +/**
1.111 +Removes an environment variable.
1.112 +*/
1.113 +EXPORT_C void unsetenv (const char* name)
1.114 + {
1.115 + _unsetenv_r (_REENT, name);
1.116 + }
1.117 +
1.118 +/**
1.119 +A reentrant version of unsetenv().
1.120 +*/
1.121 +EXPORT_C void _unsetenv_r (struct _reent *r, const char* name)
1.122 + {
1.123 + int rval = 0;
1.124 + wchar_t* tmpbuf = (wchar_t*)malloc(2*(strlen(name)+1));
1.125 +
1.126 + if (tmpbuf)
1.127 + {
1.128 + if (-1 != mbstowcs(tmpbuf, name, strlen(name)+1))
1.129 + {
1.130 + MSystemInterface& sysIf=Interface(r);
1.131 + sysIf.unsetenv(tmpbuf);
1.132 + }
1.133 + else
1.134 + rval = EILSEQ;
1.135 + }
1.136 + else
1.137 + rval = ENOMEM;
1.138 +
1.139 + free(tmpbuf);
1.140 + MapError(rval, r->_errno);
1.141 + }
1.142 +
1.143 +/**
1.144 +A wide-character version of a unsetenv()
1.145 +*/
1.146 +EXPORT_C void wunsetenv (const wchar_t* name)
1.147 + {
1.148 + _wunsetenv_r (_REENT, name);
1.149 + }
1.150 +
1.151 +/**
1.152 +A reentrant version of wunsetenv().
1.153 +*/
1.154 +EXPORT_C void _wunsetenv_r (struct _reent *r, const wchar_t* name)
1.155 + {
1.156 + MSystemInterface& sysIf=Interface(r);
1.157 + sysIf.unsetenv(name);
1.158 + }
1.159 +
1.160 +/**
1.161 +Adds or changes an environment variable.
1.162 +
1.163 +@return On Success, returns 0.
1.164 + On Failure, returns -1, errno may be set and the environment shall be unchanged.
1.165 +*/
1.166 +EXPORT_C int setenv (const char *name, const char *value, int rewrite)
1.167 + {
1.168 + struct _reent *r = _REENT2;
1.169 + if (!r)
1.170 + return -1; // Memory for library globals is not allocated (errno not set).
1.171 + return _setenv_r(r, name, value, rewrite);
1.172 + }
1.173 +
1.174 +/**
1.175 +A reentrant version of setenv().
1.176 +*/
1.177 +EXPORT_C int _setenv_r (struct _reent *r, const char *name, const char *value, int rewrite)
1.178 + {
1.179 + int rval = 0;
1.180 + wchar_t* wname = (wchar_t*)malloc(2*(strlen(name)+1));
1.181 + wchar_t* wvalue = (wchar_t*)malloc(2*(strlen(value)+1));
1.182 +
1.183 + if (wname && wvalue) //the allocs were OK
1.184 + {
1.185 + if ((-1 != mbstowcs(wname, name, strlen(name)+1)) &&
1.186 + (-1 != mbstowcs(wvalue, value, strlen(value)+1)))
1.187 + {
1.188 + MSystemInterface& sysIf=Interface(r);
1.189 + rval = sysIf.setenv(wname, wvalue, rewrite, r->_errno);
1.190 + }
1.191 + else
1.192 + rval = MapError(EILSEQ, r->_errno);
1.193 + }
1.194 + else
1.195 + rval = MapError(ENOMEM, r->_errno);
1.196 +
1.197 + free (wname);
1.198 + free (wvalue);
1.199 + return rval;
1.200 + }
1.201 +
1.202 +/**
1.203 +A wide-character version of a setenv()
1.204 +*/
1.205 +EXPORT_C int wsetenv (const wchar_t *name, const wchar_t *value, int rewrite)
1.206 + {
1.207 + struct _reent *r = _REENT2;
1.208 + if (!r)
1.209 + return -1; // Memory for library globals is not allocated (errno not set).
1.210 + return _wsetenv_r(r, name, value, rewrite);
1.211 + }
1.212 +
1.213 +/**
1.214 +A reentrant version of wsetenv().
1.215 +*/
1.216 +EXPORT_C int _wsetenv_r (struct _reent *r, const wchar_t *name, const wchar_t *value, int rewrite)
1.217 + {
1.218 + MSystemInterface& sysIf=Interface(r);
1.219 + return sysIf.setenv(name,value,rewrite,r->_errno);
1.220 + }
1.221 +
1.222 +} // extern "C"