diff --git a/dlls/advapi32/advapi.c b/dlls/advapi32/advapi.c index f4dea242bca..d3bac201585 100644 --- a/dlls/advapi32/advapi.c +++ b/dlls/advapi32/advapi.c @@ -24,15 +24,13 @@ #include #include #include -#ifdef HAVE_PWD_H -# include -#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; } /****************************************************************************** diff --git a/dlls/ntdll/sec.c b/dlls/ntdll/sec.c index 614768a8935..12abd126fed 100644 --- a/dlls/ntdll/sec.c +++ b/dlls/ntdll/sec.c @@ -25,9 +25,6 @@ #include #include #include -#ifdef HAVE_PWD_H -# include -#endif #ifdef HAVE_UNISTD_H # include #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); diff --git a/files/profile.c b/files/profile.c index 2580cf4b8f4..883d5ff3900 100644 --- a/files/profile.c +++ b/files/profile.c @@ -29,9 +29,6 @@ #include #include #include -#ifdef HAVE_PWD_H -# include -#endif #ifdef HAVE_UNISTD_H # include #endif diff --git a/include/wine/library.h b/include/wine/library.h index 87dce8c2f88..3005ea5e958 100644 --- a/include/wine/library.h +++ b/include/wine/library.h @@ -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 */ diff --git a/library/config.c b/library/config.c index 54a255de6f4..350c1e05576 100644 --- a/library/config.c +++ b/library/config.c @@ -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; +} diff --git a/scheduler/client.c b/scheduler/client.c index 7ceb8ca0819..df281db91fa 100644 --- a/scheduler/client.c +++ b/scheduler/client.c @@ -25,9 +25,6 @@ #include #include #include -#ifdef HAVE_PWD_H -# include -#endif #include #include #include diff --git a/server/registry.c b/server/registry.c index 1a54f126efc..29502ac38a9 100644 --- a/server/registry.c +++ b/server/registry.c @@ -37,7 +37,6 @@ #include #include #include -#include #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;