Factor out read_text_file_contents to ensure that data read from text files is a...
[collectd.git] / src / utils / common / common.c
index aad767e..2cebc0d 100644 (file)
@@ -1263,7 +1263,7 @@ int walk_directory(const char *dir, dirwalk_callback_f callback,
   return 0;
 }
 
-ssize_t read_file_contents(const char *filename, char *buf, size_t bufsize) {
+ssize_t read_file_contents(const char *filename, void *buf, size_t bufsize) {
   FILE *fh;
   ssize_t ret;
 
@@ -1281,6 +1281,16 @@ ssize_t read_file_contents(const char *filename, char *buf, size_t bufsize) {
   return ret;
 }
 
+ssize_t read_text_file_contents(const char *filename, char *buf,
+                                size_t bufsize) {
+  ssize_t ret = read_file_contents(filename, buf, bufsize - 1);
+  if (ret < 0)
+    return ret;
+
+  buf[ret] = '\0';
+  return ret + 1;
+}
+
 counter_t counter_diff(counter_t old_value, counter_t new_value) {
   counter_t diff;