From: Holger Schemel Date: Thu, 26 Nov 2020 16:46:02 +0000 (+0100) Subject: added abstractions for using getpwuid() X-Git-Tag: 4.2.0.5~11 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=466240bdd432835656962afbc3ab9ed9f9d5797a;p=rocksndiamonds.git added abstractions for using getpwuid() --- diff --git a/src/libgame/misc.c b/src/libgame/misc.c index 7863159b..b50abca7 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -21,7 +21,7 @@ #include "platform.h" -#if !defined(PLATFORM_WIN32) +#if defined(PLATFORM_UNIX) #include #include #endif @@ -783,6 +783,43 @@ static char *get_corrected_real_name(char *real_name) } #endif +#if defined(PLATFORM_UNIX) +static struct passwd *getPasswdEntry(void) +{ + return getpwuid(getuid()); +} + +char *getUnixLoginName(void) +{ + struct passwd *pwd = getPasswdEntry(); + + if (pwd != NULL && strlen(pwd->pw_name) != 0) + return pwd->pw_name; + + return NULL; +} + +char *getUnixRealName(void) +{ + struct passwd *pwd = getPasswdEntry(); + + if (pwd != NULL && strlen(pwd->pw_gecos) != 0) + return pwd->pw_gecos; + + return NULL; +} + +char *getUnixHomeDir(void) +{ + struct passwd *pwd = getPasswdEntry(); + + if (pwd != NULL && strlen(pwd->pw_dir) != 0) + return pwd->pw_dir; + + return NULL; +} +#endif + char *getLoginName(void) { static char *login_name = NULL; @@ -791,6 +828,7 @@ char *getLoginName(void) if (login_name == NULL) { unsigned long buffer_size = MAX_USERNAME_LEN + 1; + login_name = checked_malloc(buffer_size); if (GetUserName(login_name, &buffer_size) == 0) @@ -799,12 +837,12 @@ char *getLoginName(void) #elif defined(PLATFORM_UNIX) && !defined(PLATFORM_ANDROID) if (login_name == NULL) { - struct passwd *pwd; + char *name = getUnixLoginName(); - if ((pwd = getpwuid(getuid())) == NULL) - login_name = ANONYMOUS_NAME; + if (name != NULL) + login_name = getStringCopy(name); else - login_name = getStringCopy(pwd->pw_name); + login_name = ANONYMOUS_NAME; } #else login_name = ANONYMOUS_NAME; @@ -831,10 +869,10 @@ char *getRealName(void) #elif defined(PLATFORM_UNIX) && !defined(PLATFORM_ANDROID) if (real_name == NULL) { - struct passwd *pwd; + char *name = getUnixRealName(); - if ((pwd = getpwuid(getuid())) != NULL && strlen(pwd->pw_gecos) != 0) - real_name = get_corrected_real_name(pwd->pw_gecos); + if (name != NULL) + real_name = get_corrected_real_name(name); else real_name = ANONYMOUS_NAME; } diff --git a/src/libgame/misc.h b/src/libgame/misc.h index d9cc4e7b..5d6084f1 100644 --- a/src/libgame/misc.h +++ b/src/libgame/misc.h @@ -135,6 +135,12 @@ void SkipUntilDelayReached(unsigned int *, unsigned int, int *, int); unsigned int init_random_number(int, int); unsigned int get_random_number(int, int); +#if defined(PLATFORM_UNIX) +char *getUnixLoginName(void); +char *getUnixRealName(void); +char *getUnixHomeDir(void); +#endif + char *getLoginName(void); char *getRealName(void); diff --git a/src/libgame/setup.c b/src/libgame/setup.c index 9f82d3a0..f1b87e17 100644 --- a/src/libgame/setup.c +++ b/src/libgame/setup.c @@ -18,11 +18,6 @@ #include "platform.h" -#if !defined(PLATFORM_WIN32) -#include -#include -#endif - #include "setup.h" #include "joystick.h" #include "text.h" @@ -1504,10 +1499,10 @@ char *getHomeDir(void) { if ((dir = getenv("HOME")) == NULL) { - struct passwd *pwd; + dir = getUnixHomeDir(); - if ((pwd = getpwuid(getuid())) != NULL) - dir = getStringCopy(pwd->pw_dir); + if (dir != NULL) + dir = getStringCopy(dir); else dir = "."; }