[USERENV]

- Use a better variable type for the 'Environment' parameter of SetUserEnvironmentVariable, AppendUserEnvironmentVariable, SetUserEnvironment, SetSystemEnvironment, so as to avoid useless casts.
- Add old-style parameter annotations for exported functions (at least);
- Add an implementation comment about setting HOMEDRIVE/HOMEPATH;
- Use ARRAYSIZE() instead of hardcoded buffer sizes;
- Don't say that GetShortPathNameW() failed when it succeeds;
- Use standard capitalization for hardcoded environment variables.

svn path=/trunk/; revision=73440
This commit is contained in:
Hermès Bélusca-Maïto 2016-12-08 16:12:59 +00:00
parent b0107a6173
commit 74b479fa67

View file

@ -31,15 +31,14 @@
static static
BOOL BOOL
SetUserEnvironmentVariable(LPVOID *Environment, SetUserEnvironmentVariable(PWSTR* Environment,
LPWSTR lpName, LPWSTR lpName,
LPWSTR lpValue, LPWSTR lpValue,
BOOL bExpand) BOOL bExpand)
{ {
NTSTATUS Status; NTSTATUS Status;
UNICODE_STRING Name; UNICODE_STRING Name;
UNICODE_STRING SrcValue; UNICODE_STRING SrcValue, DstValue;
UNICODE_STRING DstValue;
ULONG Length; ULONG Length;
PVOID Buffer = NULL; PVOID Buffer = NULL;
WCHAR ShortName[MAX_PATH]; WCHAR ShortName[MAX_PATH];
@ -59,7 +58,7 @@ SetUserEnvironmentVariable(LPVOID *Environment,
return FALSE; return FALSE;
} }
Status = RtlExpandEnvironmentStrings_U((PWSTR)*Environment, Status = RtlExpandEnvironmentStrings_U(*Environment,
&SrcValue, &SrcValue,
&DstValue, &DstValue,
&Length); &Length);
@ -79,13 +78,15 @@ SetUserEnvironmentVariable(LPVOID *Environment,
RtlInitUnicodeString(&DstValue, lpValue); RtlInitUnicodeString(&DstValue, lpValue);
} }
if (!_wcsicmp(lpName, L"temp") || !_wcsicmp(lpName, L"tmp")) if (!_wcsicmp(lpName, L"TEMP") || !_wcsicmp(lpName, L"TMP"))
{ {
if (GetShortPathNameW(DstValue.Buffer, ShortName, MAX_PATH)) if (GetShortPathNameW(DstValue.Buffer, ShortName, ARRAYSIZE(ShortName)))
{
RtlInitUnicodeString(&DstValue, ShortName);
}
else
{ {
DPRINT("GetShortPathNameW() failed for %S (Error %lu)\n", DstValue.Buffer, GetLastError()); DPRINT("GetShortPathNameW() failed for %S (Error %lu)\n", DstValue.Buffer, GetLastError());
RtlInitUnicodeString(&DstValue, ShortName);
} }
DPRINT("Buffer: %S\n", ShortName); DPRINT("Buffer: %S\n", ShortName);
@ -95,7 +96,7 @@ SetUserEnvironmentVariable(LPVOID *Environment,
DPRINT("Value: %wZ\n", &DstValue); DPRINT("Value: %wZ\n", &DstValue);
Status = RtlSetEnvironmentVariable((PWSTR*)Environment, Status = RtlSetEnvironmentVariable(Environment,
&Name, &Name,
&DstValue); &DstValue);
@ -114,13 +115,12 @@ SetUserEnvironmentVariable(LPVOID *Environment,
static static
BOOL BOOL
AppendUserEnvironmentVariable(LPVOID *Environment, AppendUserEnvironmentVariable(PWSTR* Environment,
LPWSTR lpName, LPWSTR lpName,
LPWSTR lpValue) LPWSTR lpValue)
{ {
NTSTATUS Status; NTSTATUS Status;
UNICODE_STRING Name; UNICODE_STRING Name, Value;
UNICODE_STRING Value;
RtlInitUnicodeString(&Name, lpName); RtlInitUnicodeString(&Name, lpName);
@ -132,7 +132,7 @@ AppendUserEnvironmentVariable(LPVOID *Environment,
Value.Buffer[0] = UNICODE_NULL; Value.Buffer[0] = UNICODE_NULL;
Status = RtlQueryEnvironmentVariable_U((PWSTR)*Environment, Status = RtlQueryEnvironmentVariable_U(*Environment,
&Name, &Name,
&Value); &Value);
if (NT_SUCCESS(Status)) if (NT_SUCCESS(Status))
@ -140,7 +140,7 @@ AppendUserEnvironmentVariable(LPVOID *Environment,
RtlAppendUnicodeToString(&Value, lpValue); RtlAppendUnicodeToString(&Value, lpValue);
Status = RtlSetEnvironmentVariable((PWSTR*)Environment, Status = RtlSetEnvironmentVariable(Environment,
&Name, &Name,
&Value); &Value);
LocalFree(Value.Buffer); LocalFree(Value.Buffer);
@ -270,7 +270,7 @@ done:
static static
BOOL BOOL
SetUserEnvironment(LPVOID *lpEnvironment, SetUserEnvironment(PWSTR* Environment,
HKEY hKey, HKEY hKey,
LPWSTR lpSubKeyName) LPWSTR lpSubKeyName)
{ {
@ -357,17 +357,17 @@ SetUserEnvironment(LPVOID *lpEnvironment,
&dwValueDataLength); &dwValueDataLength);
if (Error == ERROR_SUCCESS) if (Error == ERROR_SUCCESS)
{ {
if (!_wcsicmp(lpValueName, L"path")) if (!_wcsicmp(lpValueName, L"PATH"))
{ {
/* Append 'Path' environment variable */ /* Append 'Path' environment variable */
AppendUserEnvironmentVariable(lpEnvironment, AppendUserEnvironmentVariable(Environment,
lpValueName, lpValueName,
lpValueData); lpValueData);
} }
else else
{ {
/* Set environment variable */ /* Set environment variable */
SetUserEnvironmentVariable(lpEnvironment, SetUserEnvironmentVariable(Environment,
lpValueName, lpValueName,
lpValueData, lpValueData,
(dwType == REG_EXPAND_SZ)); (dwType == REG_EXPAND_SZ));
@ -393,7 +393,7 @@ SetUserEnvironment(LPVOID *lpEnvironment,
static static
BOOL BOOL
SetSystemEnvironment(LPVOID *lpEnvironment) SetSystemEnvironment(PWSTR* Environment)
{ {
LONG Error; LONG Error;
HKEY hEnvKey; HKEY hEnvKey;
@ -477,7 +477,7 @@ SetSystemEnvironment(LPVOID *lpEnvironment)
if (Error == ERROR_SUCCESS) if (Error == ERROR_SUCCESS)
{ {
/* Set environment variable */ /* Set environment variable */
SetUserEnvironmentVariable(lpEnvironment, SetUserEnvironmentVariable(Environment,
lpValueName, lpValueName,
lpValueData, lpValueData,
(dwType == REG_EXPAND_SZ)); (dwType == REG_EXPAND_SZ));
@ -502,12 +502,13 @@ SetSystemEnvironment(LPVOID *lpEnvironment)
BOOL BOOL
WINAPI WINAPI
CreateEnvironmentBlock(LPVOID *lpEnvironment, CreateEnvironmentBlock(OUT LPVOID *lpEnvironment,
HANDLE hToken, IN HANDLE hToken,
BOOL bInherit) IN BOOL bInherit)
{ {
NTSTATUS Status; NTSTATUS Status;
LONG lError; LONG lError;
PWSTR* Environment = (PWSTR*)lpEnvironment;
DWORD Length; DWORD Length;
DWORD dwType; DWORD dwType;
HKEY hKey; HKEY hKey;
@ -525,8 +526,7 @@ CreateEnvironmentBlock(LPVOID *lpEnvironment,
return FALSE; return FALSE;
} }
Status = RtlCreateEnvironment((BOOLEAN)bInherit, Status = RtlCreateEnvironment((BOOLEAN)bInherit, Environment);
(PWSTR*)lpEnvironment);
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
DPRINT1("RtlCreateEnvironment() failed (Status %lx)\n", Status); DPRINT1("RtlCreateEnvironment() failed (Status %lx)\n", Status);
@ -535,63 +535,56 @@ CreateEnvironmentBlock(LPVOID *lpEnvironment,
} }
/* Set 'SystemRoot' variable */ /* Set 'SystemRoot' variable */
Length = MAX_PATH; Length = ARRAYSIZE(Buffer);
if (GetEnvironmentVariableW(L"SystemRoot", if (GetEnvironmentVariableW(L"SystemRoot", Buffer, Length))
Buffer,
Length))
{ {
SetUserEnvironmentVariable(lpEnvironment, SetUserEnvironmentVariable(Environment,
L"SystemRoot", L"SystemRoot",
Buffer, Buffer,
FALSE); FALSE);
} }
/* Set 'SystemDrive' variable */ /* Set 'SystemDrive' variable */
if (GetEnvironmentVariableW(L"SystemDrive", if (GetEnvironmentVariableW(L"SystemDrive", Buffer, Length))
Buffer,
Length))
{ {
SetUserEnvironmentVariable(lpEnvironment, SetUserEnvironmentVariable(Environment,
L"SystemDrive", L"SystemDrive",
Buffer, Buffer,
FALSE); FALSE);
} }
/* Set variables from Session Manager */ /* Set variables from Session Manager */
if (!SetSystemEnvironment(lpEnvironment)) if (!SetSystemEnvironment(Environment))
{ {
RtlDestroyEnvironment(*lpEnvironment); RtlDestroyEnvironment(*Environment);
return FALSE; return FALSE;
} }
/* Set 'COMPUTERNAME' variable */ /* Set 'COMPUTERNAME' variable */
Length = MAX_PATH; Length = ARRAYSIZE(Buffer);
if (GetComputerNameW(Buffer, if (GetComputerNameW(Buffer, &Length))
&Length))
{ {
SetUserEnvironmentVariable(lpEnvironment, SetUserEnvironmentVariable(Environment,
L"COMPUTERNAME", L"COMPUTERNAME",
Buffer, Buffer,
FALSE); FALSE);
} }
/* Set 'ALLUSERSPROFILE' variable */ /* Set 'ALLUSERSPROFILE' variable */
Length = MAX_PATH; Length = ARRAYSIZE(Buffer);
if (GetAllUsersProfileDirectoryW(Buffer, if (GetAllUsersProfileDirectoryW(Buffer, &Length))
&Length))
{ {
SetUserEnvironmentVariable(lpEnvironment, SetUserEnvironmentVariable(Environment,
L"ALLUSERSPROFILE", L"ALLUSERSPROFILE",
Buffer, Buffer,
FALSE); FALSE);
} }
/* Set 'USERSPROFILE' variable to the default users profile */ /* Set 'USERPROFILE' variable to the default users profile */
Length = MAX_PATH; Length = ARRAYSIZE(Buffer);
if (GetDefaultUserProfileDirectory(Buffer, if (GetDefaultUserProfileDirectoryW(Buffer, &Length))
&Length))
{ {
SetUserEnvironmentVariable(lpEnvironment, SetUserEnvironmentVariable(Environment,
L"USERPROFILE", L"USERPROFILE",
Buffer, Buffer,
TRUE); TRUE);
@ -604,7 +597,7 @@ CreateEnvironmentBlock(LPVOID *lpEnvironment,
&hKey); &hKey);
if (lError == ERROR_SUCCESS) if (lError == ERROR_SUCCESS)
{ {
Length = 1024 * sizeof(WCHAR); Length = sizeof(szValue);
lError = RegQueryValueExW(hKey, lError = RegQueryValueExW(hKey,
L"ProgramFilesDir", L"ProgramFilesDir",
NULL, NULL,
@ -613,13 +606,13 @@ CreateEnvironmentBlock(LPVOID *lpEnvironment,
&Length); &Length);
if (lError == ERROR_SUCCESS) if (lError == ERROR_SUCCESS)
{ {
SetUserEnvironmentVariable(lpEnvironment, SetUserEnvironmentVariable(Environment,
L"ProgramFiles", L"ProgramFiles",
szValue, szValue,
FALSE); FALSE);
} }
Length = 1024 * sizeof(WCHAR); Length = sizeof(szValue);
lError = RegQueryValueExW(hKey, lError = RegQueryValueExW(hKey,
L"CommonFilesDir", L"CommonFilesDir",
NULL, NULL,
@ -628,7 +621,7 @@ CreateEnvironmentBlock(LPVOID *lpEnvironment,
&Length); &Length);
if (lError == ERROR_SUCCESS) if (lError == ERROR_SUCCESS)
{ {
SetUserEnvironmentVariable(lpEnvironment, SetUserEnvironmentVariable(Environment,
L"CommonProgramFiles", L"CommonProgramFiles",
szValue, szValue,
FALSE); FALSE);
@ -637,6 +630,10 @@ CreateEnvironmentBlock(LPVOID *lpEnvironment,
RegCloseKey(hKey); RegCloseKey(hKey);
} }
/*
* If no user token is specified, the system environment variables are set
* and we stop here, otherwise continue setting the user-specific variables.
*/
if (hToken == NULL) if (hToken == NULL)
return TRUE; return TRUE;
@ -644,34 +641,38 @@ CreateEnvironmentBlock(LPVOID *lpEnvironment,
if (hKeyUser == NULL) if (hKeyUser == NULL)
{ {
DPRINT1("GetCurrentUserKey() failed\n"); DPRINT1("GetCurrentUserKey() failed\n");
RtlDestroyEnvironment(*lpEnvironment); RtlDestroyEnvironment(*Environment);
return FALSE; return FALSE;
} }
/* Set 'USERPROFILE' variable */ /* Set 'USERPROFILE' variable */
Length = MAX_PATH; Length = ARRAYSIZE(Buffer);
if (GetUserProfileDirectoryW(hToken, Buffer, &Length)) if (GetUserProfileDirectoryW(hToken, Buffer, &Length))
{ {
DWORD MinLen = 2; DWORD MinLen = 2;
SetUserEnvironmentVariable(lpEnvironment, SetUserEnvironmentVariable(Environment,
L"USERPROFILE", L"USERPROFILE",
Buffer, Buffer,
FALSE); FALSE);
// FIXME: Strangely enough the following two environment variables
// are not set by userenv.dll in Windows... See r68284 / CORE-9875
// FIXME2: This is done by msgina.dll !!
/* At least <drive letter>:<path> */ /* At least <drive letter>:<path> */
if (Length > MinLen) if (Length > MinLen)
{ {
/* Set 'HOMEDRIVE' variable */ /* Set 'HOMEDRIVE' variable */
StringCchCopyNW(szValue, MAX_PATH, Buffer, MinLen); StringCchCopyNW(szValue, ARRAYSIZE(Buffer), Buffer, MinLen);
SetUserEnvironmentVariable(lpEnvironment, SetUserEnvironmentVariable(Environment,
L"HOMEDRIVE", L"HOMEDRIVE",
szValue, szValue,
FALSE); FALSE);
/* Set 'HOMEPATH' variable */ /* Set 'HOMEPATH' variable */
StringCchCopyNW(szValue, MAX_PATH, Buffer + MinLen, Length - MinLen); StringCchCopyNW(szValue, ARRAYSIZE(Buffer), Buffer + MinLen, Length - MinLen);
SetUserEnvironmentVariable(lpEnvironment, SetUserEnvironmentVariable(Environment,
L"HOMEPATH", L"HOMEPATH",
szValue, szValue,
FALSE); FALSE);
@ -686,44 +687,44 @@ CreateEnvironmentBlock(LPVOID *lpEnvironment,
&lpUserName, &lpUserName,
&lpDomainName)) &lpDomainName))
{ {
/* Set 'USERNAME' variable */
SetUserEnvironmentVariable(Environment,
L"USERNAME",
lpUserName,
FALSE);
/* Set 'USERDOMAIN' variable */ /* Set 'USERDOMAIN' variable */
SetUserEnvironmentVariable(lpEnvironment, SetUserEnvironmentVariable(Environment,
L"USERDOMAIN", L"USERDOMAIN",
lpDomainName, lpDomainName,
FALSE); FALSE);
/* Set 'USERNAME' variable */ if (lpUserName != NULL)
SetUserEnvironmentVariable(lpEnvironment, LocalFree(lpUserName);
L"USERNAME",
lpUserName, if (lpDomainName != NULL)
FALSE); LocalFree(lpDomainName);
} }
/* Set user environment variables */ /* Set user environment variables */
SetUserEnvironment(lpEnvironment, SetUserEnvironment(Environment,
hKeyUser, hKeyUser,
L"Environment"); L"Environment");
/* Set user volatile environment variables */ /* Set user volatile environment variables */
SetUserEnvironment(lpEnvironment, SetUserEnvironment(Environment,
hKeyUser, hKeyUser,
L"Volatile Environment"); L"Volatile Environment");
RegCloseKey(hKeyUser); RegCloseKey(hKeyUser);
if (lpUserName != NULL)
LocalFree(lpUserName);
if (lpDomainName != NULL)
LocalFree(lpDomainName);
return TRUE; return TRUE;
} }
BOOL BOOL
WINAPI WINAPI
DestroyEnvironmentBlock(LPVOID lpEnvironment) DestroyEnvironmentBlock(IN LPVOID lpEnvironment)
{ {
DPRINT("DestroyEnvironmentBlock() called\n"); DPRINT("DestroyEnvironmentBlock() called\n");