X-Git-Url: https://git.wpitchoune.net/gitweb/?p=psensor.git;a=blobdiff_plain;f=src%2Flib%2Fpio.c;h=1942348b6af01e709a335fdce4c81cb62b70eba2;hp=217349bd4a9d6685316383ed6152de79e087cd9d;hb=c1e20f2631a1249720e9c75d753eacfcb0f6c7b9;hpb=8a3d16ade26321374c684080d627cf12ba71e598 diff --git a/src/lib/pio.c b/src/lib/pio.c index 217349b..1942348 100644 --- a/src/lib/pio.c +++ b/src/lib/pio.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2014 jeanfi@gmail.com + * Copyright (C) 2010-2017 jeanfi@gmail.com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -24,13 +24,13 @@ #include #include #include -#include #include #include /* Directory separator is \ when cross-compiling for MS Windows - systems */ + * systems + */ #if defined(__MINGW32__) #define DIRSEP ('\\') #else @@ -63,7 +63,7 @@ int is_file(const char *path) return 0; } -char *dir_normalize(const char *dpath) +static char *dir_normalize(const char *dpath) { char *npath; int n; @@ -150,11 +150,13 @@ void paths_free(char **paths) char *file_get_content(const char *fpath) { - long size; - + long size, n; char *page; + log_fct_enter(); + size = file_get_size(fpath); + if (size == -1) { page = NULL; @@ -167,19 +169,27 @@ char *file_get_content(const char *fpath) if (fp) { page = malloc(size + 1); - if (!page || size != fread(page, 1, size, fp)) { - free(page); - page = NULL; - } else { - *(page + size) = '\0'; + + if (page) { + clearerr(fp); + n = fread(page, 1, size, fp); + if (n != size && ferror(fp)) { + free(page); + page = NULL; + } else { + *(page + n) = '\0'; + } } fclose(fp); } else { + log_debug("failed to open %s", fpath); page = NULL; } } + log_fct_exit(); + return page; }