genericopenlibs/cstdlib/LSTDLIB/ENVCALLS.CPP
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // connectors for shared environment variables
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "SYSIF.H"
       
    19 #include "LPOSIX.H"
       
    20 #include <errno.h>
       
    21 #include <string.h>
       
    22 
       
    23 
       
    24 #include <stdlib_r.h>
       
    25 
       
    26 extern "C" {
       
    27 
       
    28 /**
       
    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.
       
    33 */
       
    34 EXPORT_C char* getenv (const char* name)
       
    35 	{
       
    36 	return _getenv_r (_REENT, name);
       
    37 	}
       
    38 
       
    39 /**
       
    40 A reentrant version of getenv().
       
    41 */
       
    42 EXPORT_C char* _getenv_r (struct _reent *r, const char* name)
       
    43 	{
       
    44 //	wchar_t tmpbuf[KMaxFullName+1];		//use the max path length possible
       
    45 	char *  rval = NULL;
       
    46 	int err = 0;
       
    47 	wchar_t * tmpbuf = (wchar_t*)malloc(2*(strlen(name)+1));
       
    48 
       
    49 	if (tmpbuf)
       
    50 		{
       
    51 
       
    52 		if (-1 != mbstowcs(tmpbuf, name, strlen(name)+1))
       
    53 			{
       
    54 			MSystemInterface& sysIf=Interface(r);
       
    55 			wchar_t * wideval = sysIf.getenv(tmpbuf);
       
    56 
       
    57 			if (wideval)
       
    58 				{
       
    59 					
       
    60 				//get the max size
       
    61 				int size = 1 + 2* wcslen(wideval);
       
    62 				
       
    63 				if (size < _MINNARROWBUFSIZE)
       
    64 					size = _MINNARROWBUFSIZE;
       
    65 
       
    66 				if (size > r->_NEBSize)
       
    67 					{
       
    68 					//this string could be longer
       
    69 					char * p = (char*)realloc(r->_pNarrowEnvBuffer, size);
       
    70 					if (p)
       
    71 						{
       
    72 						r->_pNarrowEnvBuffer = p;
       
    73 						r->_NEBSize = size;
       
    74 						}
       
    75 					else
       
    76 						err = ENOMEM;
       
    77 					}
       
    78 				if (wcstombs(r->_pNarrowEnvBuffer, wideval, r->_NEBSize) >= 0)
       
    79 					rval =  r->_pNarrowEnvBuffer;
       
    80 
       
    81 				}
       
    82 			}
       
    83 		else
       
    84 			err = EILSEQ;
       
    85 
       
    86 		}
       
    87 
       
    88 	MapError(err, r->_errno);
       
    89 	free(tmpbuf);
       
    90 	return rval;
       
    91 	}
       
    92 
       
    93 EXPORT_C wchar_t* wgetenv (const wchar_t* name)
       
    94 	{
       
    95 	return _wgetenv_r (_REENT, name);
       
    96 	}
       
    97 
       
    98 /**
       
    99 A reentrant version of wgetenv().
       
   100 */
       
   101 EXPORT_C wchar_t* _wgetenv_r (struct _reent *r, const wchar_t* name)
       
   102 	{
       
   103 	MSystemInterface& sysIf=Interface(r);
       
   104 	return sysIf.getenv(name);
       
   105 	}
       
   106 
       
   107 /**
       
   108 Removes an environment variable.
       
   109 */
       
   110 EXPORT_C void unsetenv (const char* name)
       
   111 	{
       
   112 	_unsetenv_r (_REENT, name);
       
   113 	}
       
   114 
       
   115 /**
       
   116 A reentrant version of unsetenv().
       
   117 */
       
   118 EXPORT_C void _unsetenv_r (struct _reent *r, const char* name)
       
   119 	{
       
   120 	int rval = 0;
       
   121 	wchar_t* tmpbuf = (wchar_t*)malloc(2*(strlen(name)+1));
       
   122 
       
   123 	if (tmpbuf)
       
   124 		{
       
   125 		if (-1 != mbstowcs(tmpbuf, name, strlen(name)+1))
       
   126 			{
       
   127 			MSystemInterface& sysIf=Interface(r);
       
   128 			sysIf.unsetenv(tmpbuf);
       
   129 			}
       
   130 		else
       
   131 			rval = EILSEQ;
       
   132 		}
       
   133 	else
       
   134 		rval = ENOMEM;
       
   135 
       
   136 	free(tmpbuf);
       
   137 	MapError(rval, r->_errno);
       
   138 	}
       
   139 
       
   140 /**
       
   141 A wide-character version of a unsetenv()
       
   142 */
       
   143 EXPORT_C void wunsetenv (const wchar_t* name)
       
   144 	{
       
   145 	_wunsetenv_r (_REENT, name);
       
   146 	}
       
   147 
       
   148 /**
       
   149 A reentrant version of wunsetenv().
       
   150 */
       
   151 EXPORT_C void _wunsetenv_r (struct _reent *r, const wchar_t* name)
       
   152 	{
       
   153 	MSystemInterface& sysIf=Interface(r);
       
   154 	sysIf.unsetenv(name);
       
   155 	}
       
   156 
       
   157 /**
       
   158 Adds or changes an environment variable.
       
   159 
       
   160 @return On Success, returns 0.
       
   161 		On Failure, returns -1, errno may be set and the environment shall be unchanged.
       
   162 */
       
   163 EXPORT_C int setenv (const char *name, const char *value, int rewrite)
       
   164 	{
       
   165 	struct _reent *r = _REENT2;
       
   166 	if (!r)
       
   167 		return -1; // Memory for library globals is not allocated (errno not set).
       
   168 	return _setenv_r(r, name, value, rewrite);
       
   169 	}
       
   170 
       
   171 /**
       
   172 A reentrant version of setenv().
       
   173 */
       
   174 EXPORT_C int _setenv_r (struct _reent *r, const char *name, const char *value, int rewrite)
       
   175 	{
       
   176 	int rval = 0;
       
   177 	wchar_t* wname = (wchar_t*)malloc(2*(strlen(name)+1));
       
   178 	wchar_t* wvalue = (wchar_t*)malloc(2*(strlen(value)+1));
       
   179 
       
   180 	if (wname && wvalue)	//the allocs were OK
       
   181 		{
       
   182 		if ((-1 != mbstowcs(wname, name, strlen(name)+1)) &&
       
   183 			(-1 != mbstowcs(wvalue, value, strlen(value)+1)))
       
   184 			{
       
   185 			MSystemInterface& sysIf=Interface(r);
       
   186 			rval = sysIf.setenv(wname, wvalue, rewrite, r->_errno);
       
   187 			}
       
   188 		else
       
   189 			rval =  MapError(EILSEQ, r->_errno);
       
   190 		}
       
   191 	else
       
   192 		rval =  MapError(ENOMEM, r->_errno);
       
   193 
       
   194 	free (wname);
       
   195 	free (wvalue);
       
   196 	return rval;
       
   197 	}
       
   198 
       
   199 /**
       
   200 A wide-character version of a setenv()
       
   201 */
       
   202 EXPORT_C int wsetenv (const wchar_t *name, const wchar_t *value, int rewrite)
       
   203 	{
       
   204 	struct _reent *r = _REENT2;
       
   205 	if (!r)
       
   206 		return -1; // Memory for library globals is not allocated (errno not set).
       
   207 	return _wsetenv_r(r, name, value, rewrite);
       
   208 	}
       
   209 
       
   210 /**
       
   211 A reentrant version of wsetenv().
       
   212 */
       
   213 EXPORT_C int _wsetenv_r (struct _reent *r, const wchar_t *name, const wchar_t *value, int rewrite)
       
   214 	{
       
   215 	MSystemInterface& sysIf=Interface(r);
       
   216 	return sysIf.setenv(name,value,rewrite,r->_errno);
       
   217 	}
       
   218 
       
   219 } // extern "C"