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)
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);
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;
}
}
- 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;
* 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))) {