summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: f19f64d)
raw | patch | inline | side by side (parent: f19f64d)
author | Florian Forster <octo@collectd.org> | |
Thu, 18 Apr 2013 09:29:04 +0000 (11:29 +0200) | ||
committer | Florian Forster <octo@collectd.org> | |
Thu, 18 Apr 2013 09:29:04 +0000 (11:29 +0200) |
src/common.c | patch | blob | history | |
src/common.h | patch | blob | history | |
src/thermal.c | patch | blob | history |
diff --git a/src/common.c b/src/common.c
index d617832ca48d6a29680ea7482df8b67e13255b5f..79e4c02c631841334307222430e02d4bc381153d 100644 (file)
--- a/src/common.c
+++ b/src/common.c
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 ae8e311f9983e8ff63441c0ab2936d287220ae67..7c0d9369cf498db70712958a6d593ccb0d58d3e8 100644 (file)
--- a/src/common.h
+++ b/src/common.h
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 603f85bbf6a96595ee04891127f37006d90730aa..27c92bc730c65b44c54c25bd9dc27a75c279980c 100644 (file)
--- a/src/thermal.c
+++ b/src/thermal.c
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))) {