trap_retval ReqEnv_setvar( void ) { env_set_var_req *req; env_set_var_ret *ret; char *var; char *value; int handle; req = GetInPtr( 0 ); var = GetInPtr( sizeof( *req ) ); value = GetInPtr( sizeof( *req ) + strlen( var ) + 1 ); ret = GetOutPtr( 0 ); ret->err = 0; if( value[0] == '\0' ) value = NULL; handle = RdosOpenProcessEnv(); RdosDeleteEnvVar( handle, var ); if( value ) RdosAddEnvVar( handle, var, value ); RdosCloseEnv( handle ); return( sizeof( *ret ) ); }
int EnvLkup( char *src, char *dst, int max_len ) { int handle; int size; handle = RdosOpenProcessEnv(); size = RdosFindEnvVar( handle, src, dst ); RdosCloseEnv( handle ); return( size ); }
unsigned EnvLkup( const char *name, char *buff, unsigned buff_len ) { int handle; int size; handle = RdosOpenProcessEnv(); size = RdosFindEnvVar( handle, name, buff ); RdosCloseEnv( handle ); return( size ); }
trap_retval ReqEnv_getvar( void ) { env_get_var_req *req; env_get_var_ret *ret; char *var; char *value; int handle; req = GetInPtr( 0 ); var = GetInPtr( sizeof( *req ) ); ret = GetOutPtr( 0 ); value = GetOutPtr( sizeof( *ret ) ); ret->err = 0; handle = RdosOpenProcessEnv(); RdosDeleteEnvVar( handle, var ); if( !RdosFindEnvVar( handle, var, value ) ) { ret->err = MSG_NOT_FOUND; return( sizeof( *ret ) ); } return( sizeof( *ret ) + strlen( value ) + 1 ); }
// _wsetenv and setenv are implemented this way so that each can call the // other without having the other call it, which would call the other, and // so on, making bad things happen. This inter-calling is necessary to keep // the wide and MBCS environments consistent. _WCRTLINK int __F_NAME(setenv,_wsetenv)( const CHAR_TYPE *name, const CHAR_TYPE *newvalue, int overwrite ) { #if !defined( __UNIX__ ) && !defined( __RDOS__ ) && !defined( __RDOSDEV__ ) #ifdef __WIDECHAR__ char *otherName; char *otherNewval; const size_t charsize = sizeof( wchar_t ); const size_t fact = MB_CUR_MAX; #else wchar_t *otherName; wchar_t *otherNewval; const size_t charsize = MB_CUR_MAX; const size_t fact = 1; #endif size_t otherNameLen; size_t otherNewvalLen; #endif int rc; /*** Ensure variable is deleted if newvalue == "" ***/ #ifndef __UNIX__ if( (newvalue != NULL) && (*newvalue == NULLCHAR) ) { if( overwrite || (__F_NAME(getenv,_wgetenv)( name ) == NULL) ) { newvalue = NULL; } } #endif #ifdef __NT__ /*** Update the process environment if using Win32 ***/ if( overwrite || __F_NAME(getenv,_wgetenv)( name ) == NULL ) { if( __F_NAME(SetEnvironmentVariableA,__lib_SetEnvironmentVariableW)( name, newvalue ) == FALSE ) { return( -1 ); } } #elif defined( __RDOS__ ) /*** Update the process environment if using RDOS ***/ if( overwrite || __F_NAME(getenv,_wgetenv)( name ) == NULL ) { int handle; handle = RdosOpenProcessEnv(); RdosDeleteEnvVar( handle, name ); if( *newvalue != NULLCHAR ) RdosAddEnvVar( handle, name, newvalue ); RdosCloseEnv( handle ); } #elif defined( __RDOSDEV__ ) /*** Update the process environment if using RDOSDEV ***/ if( overwrite || __F_NAME(getenv,_wgetenv)( name ) == NULL ) { int handle; handle = RdosOpenSysEnv(); RdosDeleteEnvVar( handle, name ); if( *newvalue != NULLCHAR ) RdosAddEnvVar( handle, name, newvalue ); RdosCloseEnv( handle ); } #endif /*** Update the (__WIDECHAR__ ? wide : MBCS) environment ***/ #ifdef __WIDECHAR__ if( _RWD_wenviron == NULL ) { __create_wide_environment(); } #endif rc = __F_NAME(__setenv,__wsetenv)( name, newvalue, overwrite ); if( rc == -1 ) { return( -1 ); } #if !defined( __UNIX__ ) && !defined( __RDOS__ ) && !defined( __RDOSDEV__ ) /*** Update the other environment ***/ #ifndef __WIDECHAR__ if( _RWD_wenviron == NULL ) { return( 0 ); // _wenviron uninitialized } #endif otherNameLen = __F_NAME(_mbslen,wcslen)( name ) + 1; otherName = lib_malloc( otherNameLen * charsize ); if( otherName == NULL ) { __set_errno( ENOMEM ); return( -1 ); } if( newvalue != NULL ) { otherNewvalLen = __F_NAME(_mbslen,wcslen)( newvalue ) + 1; otherNewval = lib_malloc( otherNewvalLen * charsize ); if( otherNewval == NULL ) { lib_free( otherName ); __set_errno( ENOMEM ); return( -1 ); } } else { otherNewval = NULL; } rc = __F_NAME(mbstowcs,wcstombs)( otherName, name, otherNameLen * fact ); if( rc != -1 ) { if( otherNewval != NULL ) { rc = __F_NAME(mbstowcs,wcstombs)( otherNewval, newvalue, otherNewvalLen * fact ); } if( rc != -1 ) { rc = __F_NAME(__wsetenv,__setenv)( otherName, otherNewval, overwrite ); } } lib_free( otherName ); lib_free( otherNewval ); if( rc == -1 ) { return( -1 ); } #endif return( 0 ); }
// _wpetenv and putenv are implemented this way so that each can call the // other without having the other call it, which would call the other, and // so on, making bad things happen. This inter-calling is necessary to keep // the wide and MBCS environments consistent. Also, with this method // __create_wide_environment can call __wputenv, without having the function // it calls call __create_wide_environment, causing similar bad things. _WCRTLINK int __F_NAME(putenv,_wputenv)( const CHAR_TYPE *env_string ) { #if !defined( __UNIX__ ) && !defined( __RDOS__ ) && !defined( __RDOSDEV__ ) #ifdef __WIDECHAR__ char *otherStr; const size_t charsize = sizeof(wchar_t); const size_t fact = MB_CUR_MAX; #else wchar_t *otherStr; const size_t charsize = MB_CUR_MAX; const size_t fact = 1; #endif size_t otherStrLen; #endif #if defined( __NT__ ) || defined( __RDOS__ ) || defined( __RDOSDEV__ ) CHAR_TYPE *name; CHAR_TYPE *value; CHAR_TYPE *p; size_t len; #if defined( __NT__ ) BOOL osRc; #else int handle; #endif /*** Update the process environment if using Win32 ***/ /*** Validate the input string ***/ p = _TCSCHR( env_string, STRING( '=' ) ); if( p == NULL || p == env_string ) return( -1 ); /* must have form name=value */ /*** Extract the variable name ***/ len = p - env_string; name = lib_malloc( ( len + 1 ) * CHARSIZE ); if( name == NULL ) return( -1 ); memcpy( name, env_string, len * CHARSIZE ); name[len] = NULLCHAR; /*** Extract the new value, if any ***/ p++; /* point past the '=' */ len = _TCSLEN( p ); if( len != 0 ) { value = lib_malloc( ( len + 1 ) * CHARSIZE ); if( value == NULL ) { lib_free( name ); return( -1 ); } memcpy( value, p, len * CHARSIZE ); value[len] = NULLCHAR; } else { value = NULL; /* don't need a buffer to delete */ } #ifdef __NT__ /*** Tell the OS about the change ***/ osRc = __lib_SetEnvironmentVariable( name, value ); #elif defined( __RDOS__ ) handle = RdosOpenProcessEnv(); RdosDeleteEnvVar( handle, name ); RdosAddEnvVar( handle, name, value ); RdosCloseEnv( handle ); #elif defined( __RDOSDEV__ ) handle = RdosOpenSysEnv(); RdosDeleteEnvVar( handle, name ); RdosAddEnvVar( handle, name, value ); RdosCloseEnv( handle ); #endif lib_free( name ); lib_free( value ); #ifdef __NT__ if( osRc == FALSE ) { if( value == NULL ) { // we couldn't find the envvar but since we are deleting it, // the putenv() is successful return( 0 ); } return( -1 ); } #endif #endif /*** Update the (__WIDECHAR__ ? wide : MBCS) environment ***/ #ifdef __WIDECHAR__ if( _RWD_wenviron == NULL ) { __create_wide_environment(); } #endif #if defined( __UNIX__ ) || defined( __RDOS__ ) || defined( __RDOSDEV__ ) return( __F_NAME(__putenv,__wputenv)( env_string ) ); #else if( __F_NAME(__putenv,__wputenv)( env_string ) != 0 ) return( -1 ); /*** Update the other environment ***/ #ifndef __WIDECHAR__ if( _RWD_wenviron == NULL ) return( 0 ); // _wenviron uninitialized #endif otherStrLen = _TCSLEN( env_string ) + 1; otherStr = lib_malloc( otherStrLen * charsize ); if( otherStr == NULL ) { _RWD_errno = ENOMEM; return( -1 ); } if( __F_NAME(mbstowcs,wcstombs)( otherStr, env_string, otherStrLen * fact ) == -1 ) { lib_free( otherStr ); _RWD_errno = ERANGE; return( -1 ); } return( __F_NAME(__wputenv,__putenv)( otherStr ) ); #endif }