From: Holger Schemel Date: Tue, 20 Oct 2020 22:34:16 +0000 (+0200) Subject: renamed and moved function to sanitize player name X-Git-Tag: 4.2.1.0~17 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=e086ff916e148637704376bff8314a8f23c5f552;p=rocksndiamonds.git renamed and moved function to sanitize player name --- diff --git a/src/files.c b/src/files.c index 0a674b4c..8cb6ec6e 100644 --- a/src/files.c +++ b/src/files.c @@ -9175,27 +9175,12 @@ static struct TokenInfo options_setup_tokens[] = }, }; -static char *get_corrected_player_name(char *login_name) -{ - // needed because player name must be a fixed length string - char *login_name_new = checked_malloc(MAX_PLAYER_NAME_LEN + 1); - - strncpy(login_name_new, login_name, MAX_PLAYER_NAME_LEN); - login_name_new[MAX_PLAYER_NAME_LEN] = '\0'; - - if (strlen(login_name) > MAX_PLAYER_NAME_LEN) // name has been cut - if (strchr(login_name_new, ' ')) - *strchr(login_name_new, ' ') = '\0'; - - return login_name_new; -} - static void setSetupInfoToDefaults(struct SetupInfo *si) { char *player_name = (user.nr == 0 ? getLoginName() : EMPTY_PLAYER_NAME); int i; - si->player_name = get_corrected_player_name(player_name); + si->player_name = getFixedUserName(player_name); si->multiple_users = TRUE; @@ -9683,7 +9668,7 @@ void LoadUserNames(void) { char *player_name = getHashEntry(setup_file_hash, "player_name"); - global.user_names[i] = get_corrected_player_name(player_name); + global.user_names[i] = getFixedUserName(player_name); freeSetupFileHash(setup_file_hash); } @@ -9716,7 +9701,7 @@ static void LoadSetup_SpecialPostProcessing(void) char *player_name_new; // needed to work around problems with fixed length strings - player_name_new = get_corrected_player_name(setup.player_name); + player_name_new = getFixedUserName(setup.player_name); free(setup.player_name); setup.player_name = player_name_new; diff --git a/src/libgame/misc.c b/src/libgame/misc.c index 217895c0..a6bef9b8 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -888,6 +888,21 @@ char *getRealName(void) return real_name; } +char *getFixedUserName(char *name) +{ + // needed because player name must be a fixed length string + char *name_new = checked_malloc(MAX_PLAYER_NAME_LEN + 1); + + strncpy(name_new, name, MAX_PLAYER_NAME_LEN); + name_new[MAX_PLAYER_NAME_LEN] = '\0'; + + if (strlen(name) > MAX_PLAYER_NAME_LEN) // name has been cut + if (strchr(name_new, ' ')) + *strchr(name_new, ' ') = '\0'; + + return name_new; +} + time_t getFileTimestampEpochSeconds(char *filename) { struct stat file_status; diff --git a/src/libgame/misc.h b/src/libgame/misc.h index 5d6084f1..159b393a 100644 --- a/src/libgame/misc.h +++ b/src/libgame/misc.h @@ -143,6 +143,7 @@ char *getUnixHomeDir(void); char *getLoginName(void); char *getRealName(void); +char *getFixedUserName(char *); time_t getFileTimestampEpochSeconds(char *);