X-Git-Url: https://git.wpitchoune.net/gitweb/?p=psensor.git;a=blobdiff_plain;f=src%2Flib%2Fpio.c;fp=src%2Flib%2Fpio.c;h=1942348b6af01e709a335fdce4c81cb62b70eba2;hp=7bb3d7c297b251c9131327a6cf982b65b355ac89;hb=790c76c1fb187bdc358e0a34e4edcf428d6c8970;hpb=18114fac7acc48b8f402ca0de70bb7e3df70be45 diff --git a/src/lib/pio.c b/src/lib/pio.c index 7bb3d7c..1942348 100644 --- a/src/lib/pio.c +++ b/src/lib/pio.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2016 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 @@ -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; }