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