summaryrefslogtreecommitdiff
path: root/src/common/utils.c
diff options
context:
space:
mode:
authorHaru <haru@dotalux.com>2015-01-24 03:42:16 +0100
committerHaru <haru@dotalux.com>2015-01-24 03:42:36 +0100
commita75714ca455c728d34918dd12200fcec87ebc0d4 (patch)
treed95d174b239a14fafdb8e824bad4a9c81a5a5a80 /src/common/utils.c
parentdf8d6560fc7eb34806d27d28b8b7dbce36868c24 (diff)
downloadhercules-a75714ca455c728d34918dd12200fcec87ebc0d4.tar.gz
hercules-a75714ca455c728d34918dd12200fcec87ebc0d4.tar.bz2
hercules-a75714ca455c728d34918dd12200fcec87ebc0d4.tar.xz
hercules-a75714ca455c728d34918dd12200fcec87ebc0d4.zip
Fixed 18 minor issues
Signed-off-by: Haru <haru@dotalux.com>
Diffstat (limited to 'src/common/utils.c')
-rw-r--r--src/common/utils.c57
1 files changed, 35 insertions, 22 deletions
diff --git a/src/common/utils.c b/src/common/utils.c
index c168bd74e..ad68706ca 100644
--- a/src/common/utils.c
+++ b/src/common/utils.c
@@ -354,40 +354,51 @@ const char* timestamp2string(char* str, size_t size, time_t timestamp, const cha
/* [Ind/Hercules] Caching */
-bool HCache_check(const char *file) {
+bool HCache_check(const char *file)
+{
struct stat bufa, bufb;
FILE *first, *second;
char s_path[255], dT[1];
time_t rtime;
- if( !(first = fopen(file,"rb")) )
+ if (!(first = fopen(file,"rb")))
return false;
- if( file[0] == '.' && file[1] == '/' )
+ if (file[0] == '.' && file[1] == '/')
file += 2;
- else if( file[0] == '.' )
+ else if (file[0] == '.')
file++;
snprintf(s_path, 255, "./cache/%s", file);
- if( !(second = fopen(s_path,"rb")) ) {
+ if (!(second = fopen(s_path,"rb"))) {
fclose(first);
return false;
}
- if( fread(dT,sizeof(dT),1,second) != 1 || fread(&rtime,sizeof(rtime),1,second) != 1 || dT[0] != HCACHE_KEY || HCache->recompile_time > rtime ) {
+ if (fread(dT,sizeof(dT),1,second) != 1
+ || fread(&rtime,sizeof(rtime),1,second) != 1
+ || dT[0] != HCACHE_KEY
+ || HCache->recompile_time > rtime) {
fclose(first);
fclose(second);
return false;
}
- fstat(fileno(first), &bufa);
- fstat(fileno(second), &bufb);
-
+ if (fstat(fileno(first), &bufa) != 0) {
+ fclose(first);
+ fclose(second);
+ return false;
+ }
fclose(first);
+
+ if (fstat(fileno(second), &bufb) != 0) {
+ fclose(second);
+ return false;
+ }
fclose(second);
- if( bufa.st_mtime > bufb.st_mtime )
+ if (bufa.st_mtime > bufb.st_mtime)
return false;
return true;
@@ -414,24 +425,26 @@ FILE *HCache_open(const char *file, const char *opt) {
hwrite(dT,sizeof(dT),1,first);
hwrite(&HCache->recompile_time,sizeof(HCache->recompile_time),1,first);
}
- fseek(first, 20, SEEK_SET);/* skip first 20, might wanna store something else later */
+ if (fseek(first, 20, SEEK_SET) != 0) { // skip first 20, might wanna store something else later
+ fclose(first);
+ return NULL;
+ }
return first;
}
-void HCache_init(void) {
- FILE *server;
-
- if( (server = fopen(SERVER_NAME,"rb")) ) {
- struct stat buf;
-
- fstat(fileno(server), &buf);
- HCache->recompile_time = buf.st_mtime;
- fclose(server);
- HCache->enabled = true;
- } else
+void HCache_init(void)
+{
+ struct stat buf;
+ if (stat(SERVER_NAME, &buf) != 0) {
ShowWarning("Unable to open '%s', caching capabilities have been disabled!\n",SERVER_NAME);
+ return;
+ }
+
+ HCache->recompile_time = buf.st_mtime;
+ HCache->enabled = true;
}
+
/* transit to fread, shields vs warn_unused_result */
size_t hread(void * ptr, size_t size, size_t count, FILE * stream) {
return fread(ptr, size, count, stream);