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