Skip to content

Commit

Permalink
winpr-env: update Get/SetEnvironmentVariableA
Browse files Browse the repository at this point in the history
* set last error to ERROR_ENVVAR_NOT_FOUND in GetEnvironmentVariableA
  if not found
* remove unused variables in SetEnvironmentVariableA
* add more test cases
  • Loading branch information
bmiklautz committed Apr 17, 2014
1 parent 291569d commit 02c4e26
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
6 changes: 6 additions & 0 deletions winpr/libwinpr/environment/CMakeLists.txt
Expand Up @@ -31,9 +31,15 @@ add_complex_library(MODULE ${MODULE_NAME} TYPE "OBJECT"

set_target_properties(${MODULE_NAME} PROPERTIES VERSION ${WINPR_VERSION_FULL} SOVERSION ${WINPR_VERSION} PREFIX "lib")

set_complex_link_libraries(VARIABLE ${MODULE_PREFIX}_LIBS
MONOLITHIC ${MONOLITHIC_BUILD} INTERNAL
MODULE winpr
MODULES winpr-error)

if(MONOLITHIC_BUILD)

else()
target_link_libraries(${MODULE_NAME} ${${MODULE_PREFIX}_LIBS})
install(TARGETS ${MODULE_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR} EXPORT WinPRTargets)
endif()

Expand Down
13 changes: 8 additions & 5 deletions winpr/libwinpr/environment/environment.c
Expand Up @@ -24,6 +24,7 @@
#endif

#include <winpr/environment.h>
#include <winpr/error.h>

#ifndef _WIN32

Expand Down Expand Up @@ -151,7 +152,10 @@ DWORD GetEnvironmentVariableA(LPCSTR lpName, LPSTR lpBuffer, DWORD nSize)
env = getenv(lpName);

if (!env)
{
SetLastError(ERROR_ENVVAR_NOT_FOUND);
return 0;
}

length = strlen(env);

Expand Down Expand Up @@ -227,19 +231,18 @@ DWORD GetEnvironmentVariableW(LPCWSTR lpName, LPWSTR lpBuffer, DWORD nSize)

BOOL SetEnvironmentVariableA(LPCSTR lpName, LPCSTR lpValue)
{
int length;
char* envstr;

if (!lpName)
return FALSE;

if (lpValue)
{
setenv(lpName,lpValue,1);
if (0 != setenv(lpName,lpValue,1))
return FALSE;
}
else
{
unsetenv(lpName);
if (0 != unsetenv(lpName))
return FALSE;
}

return TRUE;
Expand Down
Expand Up @@ -3,28 +3,59 @@
#include <winpr/crt.h>
#include <winpr/tchar.h>
#include <winpr/environment.h>
#include <winpr/error.h>

#define TEST_NAME "WINPR_TEST_VARIABLE"
#define TEST_VALUE "WINPR_TEST_VALUE"
int TestEnvironmentSetEnvironmentVariable(int argc, char* argv[])
{
DWORD nSize;
LPSTR lpBuffer;
DWORD error = 0;

SetEnvironmentVariableA("WINPR_TEST_VARIABLE", "WINPR_TEST_VALUE");
SetEnvironmentVariableA(TEST_NAME, TEST_VALUE);

nSize = GetEnvironmentVariableA("WINPR_TEST_VARIABLE", NULL, 0);
nSize = GetEnvironmentVariableA(TEST_NAME, NULL, 0);
/* check if value returned is len + 1 ) */
if (nSize != strlen(TEST_VALUE) + 1)
{
printf("GetEnvironmentVariableA not found error\n");
return -1;
}

lpBuffer = (LPSTR) malloc(nSize);
nSize = GetEnvironmentVariableA("WINPR_TEST_VARIABLE", lpBuffer, nSize);
nSize = GetEnvironmentVariableA(TEST_NAME, lpBuffer, nSize);

printf("GetEnvironmentVariableA(WINPR_TEST_VARIABLE) = %s\n" , lpBuffer);
if (nSize != strlen(TEST_VALUE))
{
printf("GetEnvironmentVariableA wrong size returned\n");
return -1;
}

if (strcmp(lpBuffer, "WINPR_TEST_VALUE") != 0)
if (strcmp(lpBuffer, TEST_VALUE) != 0)
{
printf("GetEnvironmentVariableA returned value doesn't match\n");
return -1;
}

nSize = GetEnvironmentVariableA("__xx__notset_",lpBuffer, nSize);
error = GetLastError();
if (0 != nSize || ERROR_ENVVAR_NOT_FOUND != error)
{
printf("GetEnvironmentVariableA not found error\n");
return -1;
}

free(lpBuffer);

/* clear variable */
SetEnvironmentVariableA(TEST_NAME, NULL);
nSize = GetEnvironmentVariableA(TEST_VALUE, NULL, 0);
if ( 0 != nSize)
{
printf("SetEnvironmentVariableA failed to clear variable\n");
return -1;
}
return 0;
}

0 comments on commit 02c4e26

Please sign in to comment.