Commit dba83c8b authored by Alexandre Julliard's avatar Alexandre Julliard

Added wine_get_user_name function and got rid of some of the getpwuid

portability stuff. More portable printf formats for 64-bit types.
parent ef0e2af7
......@@ -24,15 +24,13 @@
#include <errno.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_PWD_H
# include <pwd.h>
#endif
#include "winbase.h"
#include "windef.h"
#include "winnls.h"
#include "winerror.h"
#include "wine/library.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(advapi);
......@@ -47,18 +45,7 @@ BOOL WINAPI
GetUserNameA( LPSTR lpszName, LPDWORD lpSize )
{
size_t len;
char *name;
#ifdef HAVE_GETPWUID
struct passwd *pwd = getpwuid( getuid() );
name = pwd ? pwd->pw_name : NULL;
#else
name = getenv("USER");
#endif
if (!name) {
ERR("Username lookup failed: %s\n", strerror(errno));
return 0;
}
const char *name = wine_get_user_name();
/* We need to include the null character when determining the size of the buffer. */
len = strlen(name) + 1;
......@@ -84,15 +71,19 @@ GetUserNameA( LPSTR lpszName, LPDWORD lpSize )
BOOL WINAPI
GetUserNameW( LPWSTR lpszName, LPDWORD lpSize )
{
LPSTR name = (LPSTR)HeapAlloc( GetProcessHeap(), 0, *lpSize );
DWORD size = *lpSize;
BOOL res = GetUserNameA(name,lpSize);
const char *name = wine_get_user_name();
DWORD len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
/* FIXME: should set lpSize in WCHARs */
if (size && !MultiByteToWideChar( CP_ACP, 0, name, -1, lpszName, size ))
lpszName[size-1] = 0;
HeapFree( GetProcessHeap(), 0, name );
return res;
if (len > *lpSize)
{
SetLastError(ERROR_MORE_DATA);
*lpSize = len;
return FALSE;
}
*lpSize = len;
MultiByteToWideChar( CP_ACP, 0, name, -1, lpszName, len );
return TRUE;
}
/******************************************************************************
......
......@@ -25,9 +25,6 @@
#include <time.h>
#include <ctype.h>
#include <math.h>
#ifdef HAVE_PWD_H
# include <pwd.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
......@@ -741,18 +738,10 @@ NTSTATUS WINAPI RtlConvertSidToUnicodeString(
PSID Sid,
BOOLEAN AllocateString)
{
const char *p;
const char *p = wine_get_user_name();
NTSTATUS status;
ANSI_STRING AnsiStr;
#ifdef HAVE_GETPWUID
struct passwd *pwd = getpwuid( getuid() );
p = pwd ? pwd->pw_name : NULL;
#else
p = getenv("USER");
#endif
p = p ? p : ".Default";
FIXME("(%p %p %u)\n", String, Sid, AllocateString);
RtlInitAnsiString(&AnsiStr, p);
......
......@@ -29,9 +29,6 @@
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#ifdef HAVE_PWD_H
# include <pwd.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
......
......@@ -28,6 +28,7 @@
extern const char *wine_get_config_dir(void);
extern const char *wine_get_server_dir(void);
extern const char *wine_get_user_name(void);
/* dll loading */
......
......@@ -39,6 +39,7 @@ static const char * const server_dir_prefix = "/server-"; /* prefix for ser
static char *config_dir;
static char *server_dir;
static char *user_name;
#ifdef __GNUC__
static void fatal_error( const char *err, ... ) __attribute__((noreturn,format(printf,1,2)));
......@@ -80,6 +81,15 @@ static void *xmalloc( size_t size )
return res;
}
/* strdup wrapper */
static char *xstrdup( const char *str )
{
size_t len = strlen(str) + 1;
char *res = xmalloc( len );
memcpy( res, str, len );
return res;
}
/* remove all trailing slashes from a path name */
inline static void remove_trailing_slashes( char *path )
{
......@@ -115,6 +125,7 @@ static void init_paths(void)
if (!(user = getenv( "USER" )))
fatal_error( "cannot determine your user name, set the USER environment variable\n" );
#endif /* HAVE_GETPWUID */
user_name = xstrdup( user );
/* build config_dir */
......@@ -154,13 +165,15 @@ static void init_paths(void)
}
strcpy( p, server_dir_prefix );
if (sizeof(st.st_dev) > sizeof(unsigned long))
sprintf( server_dir + strlen(server_dir), "%llx-", (unsigned long long)st.st_dev );
if (sizeof(st.st_dev) > sizeof(unsigned long) && st.st_dev > ~0UL)
sprintf( server_dir + strlen(server_dir), "%lx%08lx-",
(unsigned long)(st.st_dev >> 32), (unsigned long)st.st_dev );
else
sprintf( server_dir + strlen(server_dir), "%lx-", (unsigned long)st.st_dev );
if (sizeof(st.st_ino) > sizeof(unsigned long))
sprintf( server_dir + strlen(server_dir), "%llx", (unsigned long long)st.st_ino );
if (sizeof(st.st_ino) > sizeof(unsigned long) && st.st_ino > ~0UL)
sprintf( server_dir + strlen(server_dir), "%lx%08lx",
(unsigned long)(st.st_ino >> 32), (unsigned long)st.st_ino );
else
sprintf( server_dir + strlen(server_dir), "%lx", (unsigned long)st.st_ino );
}
......@@ -178,3 +191,10 @@ const char *wine_get_server_dir(void)
if (!server_dir) init_paths();
return server_dir;
}
/* return the current user name */
const char *wine_get_user_name(void)
{
if (!user_name) init_paths();
return user_name;
}
......@@ -25,9 +25,6 @@
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#ifdef HAVE_PWD_H
# include <pwd.h>
#endif
#include <signal.h>
#include <stdio.h>
#include <string.h>
......
......@@ -37,7 +37,6 @@
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include "object.h"
#include "handle.h"
#include "request.h"
......@@ -942,15 +941,7 @@ static struct key *create_root_key( obj_handle_t hkey )
if (hkey == (obj_handle_t)HKEY_CURRENT_USER) /* this one is special */
{
/* get the current user name */
char buffer[10];
struct passwd *pwd = getpwuid( getuid() );
if (pwd) p = pwd->pw_name;
else
{
sprintf( buffer, "%ld", (long) getuid() );
p = buffer;
}
p = wine_get_user_name();
while (*p && i < sizeof(keyname)/sizeof(WCHAR)-1) keyname[i++] = *p++;
}
keyname[i++] = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment