From a8220ab2d293bb7f4b014b79d16b2fb05090fa93 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Feb 2021 12:45:55 +0100 Subject: Adding upstream version 1.29.0. Signed-off-by: Daniel Baumann --- libnetdata/libnetdata.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'libnetdata/libnetdata.c') diff --git a/libnetdata/libnetdata.c b/libnetdata/libnetdata.c index 095c38c25..c9b7ab198 100644 --- a/libnetdata/libnetdata.c +++ b/libnetdata/libnetdata.c @@ -168,6 +168,7 @@ char *strdupz(const char *s) { return t; } +// If ptr is NULL, no operation is performed. void freez(void *ptr) { free(ptr); } @@ -1452,3 +1453,41 @@ void recursive_config_double_dir_load(const char *user_path, const char *stock_p freez(udir); freez(sdir); } + +// Returns the number of bytes read from the file if file_size is not NULL. +// The actual buffer has an extra byte set to zero (not included in the count). +char *read_by_filename(char *filename, long *file_size) +{ + FILE *f = fopen(filename, "r"); + if (!f) + return NULL; + if (fseek(f, 0, SEEK_END) < 0) { + fclose(f); + return NULL; + } + long size = ftell(f); + if (size <= 0 || fseek(f, 0, SEEK_END) < 0) { + fclose(f); + return NULL; + } + char *contents = callocz(size + 1, 1); + if (!contents) { + fclose(f); + return NULL; + } + if (fseek(f, 0, SEEK_SET) < 0) { + fclose(f); + freez(contents); + return NULL; + } + size_t res = fread(contents, 1, size, f); + if ( res != (size_t)size) { + freez(contents); + fclose(f); + return NULL; + } + fclose(f); + if (file_size) + *file_size = size; + return contents; +} -- cgit v1.2.3