diff options
Diffstat (limited to 'src/common/utils.c')
-rw-r--r-- | src/common/utils.c | 56 |
1 files changed, 39 insertions, 17 deletions
diff --git a/src/common/utils.c b/src/common/utils.c index bcfc153e3..d4c838b56 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2016 Hercules Dev Team + * Copyright (C) 2012-2018 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify @@ -45,11 +45,11 @@ #include <stdlib.h> #include <sys/stat.h> // cache purposes [Ind/Hercules] -struct HCache_interface HCache_s; +static struct HCache_interface HCache_s; struct HCache_interface *HCache; /// Dumps given buffer into file pointed to by a handle. -void WriteDump(FILE* fp, const void* buffer, size_t length) +void WriteDump(FILE *fp, const void *buffer, size_t length) { size_t i; char hex[48+1], ascii[16+1]; @@ -109,7 +109,7 @@ void ShowDump(const void *buffer, size_t length) #ifdef WIN32 -static char* checkpath(char *path, const char *srcpath) +static char *checkpath(char *path, const char *srcpath) { // just make sure the char*path is not const char *p = path; @@ -128,7 +128,7 @@ static char* checkpath(char *path, const char *srcpath) return path; } -void findfile(const char *p, const char *pat, void (func)(const char*)) +void findfile(const char *p, const char *pat, void (func)(const char *)) { WIN32_FIND_DATAA FindFileData; HANDLE hFind; @@ -171,7 +171,7 @@ void findfile(const char *p, const char *pat, void (func)(const char*)) #define MAX_DIR_PATH 2048 -static char* checkpath(char *path, const char*srcpath) +static char *checkpath(char *path, const char *srcpath) { // just make sure the char*path is not const char *p=path; @@ -190,7 +190,7 @@ static char* checkpath(char *path, const char*srcpath) return path; } -void findfile(const char *p, const char *pat, void (func)(const char*)) +void findfile(const char *p, const char *pat, void (func)(const char *)) { DIR* dir; ///< pointer to the scanned directory. struct dirent* entry; ///< pointer to one directory entry. @@ -216,7 +216,7 @@ void findfile(const char *p, const char *pat, void (func)(const char*)) if (strcmp(entry->d_name, "..") == 0) continue; - sprintf(tmppath,"%s%c%s",path, PATHSEP, entry->d_name); + safesnprintf(tmppath, sizeof(tmppath), "%s%c%s", path, PATHSEP, entry->d_name); // check if the pattern matches. if (strstr(entry->d_name, pattern)) { @@ -238,7 +238,7 @@ void findfile(const char *p, const char *pat, void (func)(const char*)) } #endif -bool exists(const char* filename) +bool exists(const char *filename) { return !access(filename, F_OK); } @@ -309,14 +309,14 @@ int32 MakeLongLE(int32 val) } // Reads an uint16 in little-endian from the buffer -uint16 GetUShort(const unsigned char* buf) +uint16 GetUShort(const unsigned char *buf) { return ( ((uint16)(buf[0])) ) | ( ((uint16)(buf[1])) << 0x08 ); } // Reads an uint32 in little-endian from the buffer -uint32 GetULong(const unsigned char* buf) +uint32 GetULong(const unsigned char *buf) { return ( ((uint32)(buf[0])) ) | ( ((uint32)(buf[1])) << 0x08 ) @@ -325,13 +325,13 @@ uint32 GetULong(const unsigned char* buf) } // Reads an int32 in little-endian from the buffer -int32 GetLong(const unsigned char* buf) +int32 GetLong(const unsigned char *buf) { return (int32)GetULong(buf); } // Reads a float (32 bits) from the buffer -float GetFloat(const unsigned char* buf) +float GetFloat(const unsigned char *buf) { uint32 val = GetULong(buf); return *((float*)(void*)&val); @@ -359,6 +359,28 @@ unsigned int get_percentage(const unsigned int A, const unsigned int B) return (unsigned int)floor(result); } +/// calculates the value of A / B, in percent (rounded down) +uint64 get_percentage64(const uint64 A, const uint64 B) +{ + double result; + + if( B == 0 ) + { + ShowError("get_percentage(): division by zero! (A=%"PRIu64",B=%"PRIu64")\n", A, B); + return ~0U; + } + + result = 100 * ((double)A / (double)B); + + if( result > UINT_MAX ) + { + ShowError("get_percentage(): result percentage too high! (A=%"PRIu64",B=%"PRIu64",result=%g)\n", A, B, result); + return UINT_MAX; + } + + return (uint64)floor(result); +} + /** * Applies a percentual rate modifier. * @@ -404,7 +426,7 @@ int apply_percentrate(int value, int rate, int maxrate) //----------------------------------------------------- // custom timestamp formatting (from eApp) //----------------------------------------------------- -const char* timestamp2string(char* str, size_t size, time_t timestamp, const char* format) +const char *timestamp2string(char *str, size_t size, time_t timestamp, const char *format) { size_t len; nullpo_retr(NULL, str); @@ -414,7 +436,7 @@ const char* timestamp2string(char* str, size_t size, time_t timestamp, const cha } /* [Ind/Hercules] Caching */ -bool HCache_check(const char *file) +static bool HCache_check(const char *file) { struct stat bufa, bufb; FILE *first, *second; @@ -465,7 +487,7 @@ bool HCache_check(const char *file) return true; } -FILE *HCache_open(const char *file, const char *opt) +static FILE *HCache_open(const char *file, const char *opt) { FILE *first; char s_path[255]; @@ -498,7 +520,7 @@ FILE *HCache_open(const char *file, const char *opt) return first; } -void HCache_init(void) +static void HCache_init(void) { struct stat buf; if (stat(SERVER_NAME, &buf) != 0) { |