#include <9pm/windows.h>
#include <9pm/u.h>
#include <9pm/libc.h>

DWORD WINAPI
GetEnvironmentVariable(LPCWSTR lpName, LPWSTR lpBuffer, DWORD nSize)
{
	char *name, *buf;
	int n, n2;

	if(win_useunicode)
		return GetEnvironmentVariableW(lpName, lpBuffer, nSize);

	name = win_wstr2utf(lpName);
	n = nSize*UTFmax;
	buf = win_malloc(n);
	n2 = GetEnvironmentVariableA(name, buf, n);
	win_free(name);
	if(n2 == 0){
		win_free(buf);
		return 0;
	}
	if(n2 >= n){
		win_free(buf);
		lpBuffer[0] = 0;
		return n2;
	}
	n2 = win_utf2wstrn(lpBuffer, nSize, buf);
	win_free(buf);
	return n2;
}

