From: Florian Forster Date: Thu, 18 Apr 2013 09:29:04 +0000 (+0200) Subject: src/common.[ch]: read_file_contents: Use {s,}size_t rather than int. X-Git-Tag: collectd-5.4.0~54 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=5d9ad0bcb8b1ae1b7b0994f51237c04273f5cfbc;p=collectd.git src/common.[ch]: read_file_contents: Use {s,}size_t rather than int. --- diff --git a/src/common.c b/src/common.c index d617832c..79e4c02c 100644 --- a/src/common.c +++ b/src/common.c @@ -1213,18 +1213,25 @@ int walk_directory (const char *dir, dirwalk_callback_f callback, return (0); } -int read_file_contents (const char *filename, char *buf, int bufsize) +ssize_t read_file_contents (const char *filename, char *buf, size_t bufsize) { FILE *fh; - int n; + ssize_t ret; - if ((fh = fopen (filename, "r")) == NULL) - return -1; + fh = fopen (filename, "r"); + if (fh == NULL) + return (-1); - n = fread(buf, 1, bufsize, fh); - fclose(fh); + ret = (ssize_t) fread (buf, 1, bufsize, fh); + if ((ret == 0) && (ferror (fh) != 0)) + { + ERROR ("read_file_contents: Reading file \"%s\" failed.", + filename); + ret = -1; + } - return n; + fclose(fh); + return (ret); } counter_t counter_diff (counter_t old_value, counter_t new_value) diff --git a/src/common.h b/src/common.h index ae8e311f..7c0d9369 100644 --- a/src/common.h +++ b/src/common.h @@ -303,7 +303,7 @@ typedef int (*dirwalk_callback_f)(const char *dirname, const char *filename, int walk_directory (const char *dir, dirwalk_callback_f callback, void *user_data, int hidden); /* Returns the number of bytes read or negative on error. */ -int read_file_contents (const char *filename, char *buf, int bufsize); +ssize_t read_file_contents (char const *filename, char *buf, size_t bufsize); counter_t counter_diff (counter_t old_value, counter_t new_value); diff --git a/src/thermal.c b/src/thermal.c index 603f85bb..27c92bc7 100644 --- a/src/thermal.c +++ b/src/thermal.c @@ -80,12 +80,12 @@ static int thermal_sysfs_device_read (const char __attribute__((unused)) *dir, if (device_list && ignorelist_match (device_list, name)) return -1; - len = snprintf (filename, sizeof (filename), + len = ssnprintf (filename, sizeof (filename), "%s/%s/temp", dirname_sysfs, name); if ((len < 0) || ((size_t) len >= sizeof (filename))) return -1; - len = read_file_contents (filename, data, sizeof(data)); + len = (ssize_t) read_file_contents (filename, data, sizeof(data)); if (len > 1 && data[--len] == '\n') { char *endptr = NULL; double temp; @@ -100,12 +100,12 @@ static int thermal_sysfs_device_read (const char __attribute__((unused)) *dir, } } - len = snprintf (filename, sizeof (filename), + len = ssnprintf (filename, sizeof (filename), "%s/%s/cur_state", dirname_sysfs, name); if ((len < 0) || ((size_t) len >= sizeof (filename))) return -1; - len = read_file_contents (filename, data, sizeof(data)); + len = (ssize_t) read_file_contents (filename, data, sizeof(data)); if (len > 1 && data[--len] == '\n') { char *endptr = NULL; double state; @@ -139,12 +139,12 @@ static int thermal_procfs_device_read (const char __attribute__((unused)) *dir, * temperature: 55 C */ - len = snprintf (filename, sizeof (filename), + len = ssnprintf (filename, sizeof (filename), "%s/%s/temperature", dirname_procfs, name); if ((len < 0) || ((size_t) len >= sizeof (filename))) return -1; - len = read_file_contents (filename, data, sizeof(data)); + len = (ssize_t) read_file_contents (filename, data, sizeof(data)); if ((len > 0) && ((size_t) len > sizeof(str_temp)) && (data[--len] == '\n') && (! strncmp(data, str_temp, sizeof(str_temp)-1))) {