From: Florian Forster Date: Mon, 12 Sep 2016 05:51:47 +0000 (+0200) Subject: battery plugin: Reimplement sysfs_file_to_buffer() using read_file_contents(). X-Git-Tag: collectd-5.7.0~105 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=bc8a268a5919681b7aee07dd59870b71c582a57f;p=collectd.git battery plugin: Reimplement sysfs_file_to_buffer() using read_file_contents(). --- diff --git a/src/battery.c b/src/battery.c index 3e6d7bda..d227d559 100644 --- a/src/battery.c +++ b/src/battery.c @@ -392,47 +392,17 @@ static int sysfs_file_to_buffer(char const *dir, /* {{{ */ char const *basename, char *buffer, size_t buffer_size) { - int status; - FILE *fp; char filename[PATH_MAX]; + int status; ssnprintf (filename, sizeof (filename), "%s/%s/%s", dir, power_supply, basename); - /* No file isn't the end of the world -- not every system will be - * reporting the same set of statistics */ - if (access (filename, R_OK) != 0) - return ENOENT; - - fp = fopen (filename, "r"); - if (fp == NULL) - { - status = errno; - if (status != ENOENT) - { - char errbuf[1024]; - WARNING ("battery plugin: fopen (%s) failed: %s", filename, - sstrerror (status, errbuf, sizeof (errbuf))); - } - return status; - } - - if (fgets (buffer, buffer_size, fp) == NULL) - { - status = errno; - if (status != ENODEV) - { - char errbuf[1024]; - WARNING ("battery plugin: fgets (%s) failed: %s", filename, - sstrerror (status, errbuf, sizeof (errbuf))); - } - fclose (fp); + status = (int) read_file_contents (filename, buffer, buffer_size); + if (status < 0) return status; - } strstripnewline (buffer); - - fclose (fp); return 0; } /* }}} int sysfs_file_to_buffer */