advapi32: Use WINEUSERNAME variable to implement GetUserNameA/W.

Signed-off-by: Alexandre Julliard <julliard@winehq.org>
feature/deterministic
Alexandre Julliard 2020-04-16 15:15:21 +02:00
parent 52bf0fbd47
commit f8fb43aaba
1 changed files with 13 additions and 64 deletions

View File

@ -32,7 +32,6 @@
#include "wincred.h"
#include "wct.h"
#include "wine/library.h"
#include "wine/unicode.h"
#include "wine/debug.h"
@ -42,81 +41,31 @@ WINE_DEFAULT_DEBUG_CHANNEL(advapi);
/******************************************************************************
* GetUserNameA [ADVAPI32.@]
*
* Get the current user name.
*
* PARAMS
* lpszName [O] Destination for the user name.
* lpSize [I/O] Size of lpszName.
*
* RETURNS
* Success: The length of the user name, including terminating NUL.
* Failure: ERROR_MORE_DATA if *lpSize is too small.
*/
BOOL WINAPI
GetUserNameA( LPSTR lpszName, LPDWORD lpSize )
BOOL WINAPI GetUserNameA( LPSTR name, LPDWORD size )
{
WCHAR *buffer;
DWORD len = GetEnvironmentVariableA( "WINEUSERNAME", name, *size );
BOOL ret;
DWORD sizeW = *lpSize;
if (!(buffer = heap_alloc( sizeW * sizeof(WCHAR) )))
{
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
return FALSE;
}
ret = GetUserNameW( buffer, &sizeW );
if (ret)
*lpSize = WideCharToMultiByte( CP_ACP, 0, buffer, -1, lpszName, *lpSize, NULL, NULL );
else
*lpSize = sizeW;
heap_free( buffer );
if (!len) return FALSE;
if ((ret = (len < *size))) len++;
*size = len;
return ret;
}
/******************************************************************************
* GetUserNameW [ADVAPI32.@]
*
* See GetUserNameA.
*/
BOOL WINAPI
GetUserNameW( LPWSTR lpszName, LPDWORD lpSize )
BOOL WINAPI GetUserNameW( LPWSTR name, LPDWORD size )
{
const char *name = wine_get_user_name();
DWORD i, len = MultiByteToWideChar( CP_UNIXCP, 0, name, -1, NULL, 0 );
LPWSTR backslash;
static const WCHAR wineusernameW[] = {'W','I','N','E','U','S','E','R','N','A','M','E',0};
DWORD len = GetEnvironmentVariableW( wineusernameW, name, *size );
BOOL ret;
if (len > *lpSize)
{
SetLastError( ERROR_INSUFFICIENT_BUFFER );
*lpSize = len;
return FALSE;
}
*lpSize = len;
MultiByteToWideChar( CP_UNIXCP, 0, name, -1, lpszName, len );
/* Word uses the user name to create named mutexes and file mappings,
* and backslashes in the name cause the creation to fail.
* Also, Windows doesn't return the domain name in the user name even when
* joined to a domain. A Unix box joined to a domain using winbindd will
* contain the domain name in the username. So we need to cut this off.
* FIXME: Only replaces forward and backslashes for now, should get the
* winbind separator char from winbindd and replace that.
*/
for (i = 0; lpszName[i]; i++)
if (lpszName[i] == '/') lpszName[i] = '\\';
backslash = strrchrW(lpszName, '\\');
if (backslash == NULL)
return TRUE;
len = lstrlenW(backslash);
memmove(lpszName, backslash + 1, len * sizeof(WCHAR));
*lpSize = len;
return TRUE;
if (!len) return FALSE;
if ((ret = (len < *size))) len++;
*size = len;
return ret;
}
/******************************************************************************