X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fdaemon%2Fcommon.c;h=84d86608deac0cb14a40e3ada3d481f01dd5d16e;hb=a6f084608f38fa0ad66247f8e4b8ce7520292276;hp=e396b79d4285ae6c6b324dec722a56d4151300fc;hpb=3f9f6b9bc37cfd8ee3eb7a21cb895ff524c533c8;p=collectd.git diff --git a/src/daemon/common.c b/src/daemon/common.c index e396b79d..84d86608 100644 --- a/src/daemon/common.c +++ b/src/daemon/common.c @@ -1,6 +1,6 @@ /** * collectd - src/common.c - * Copyright (C) 2005-2014 Florian octo Forster + * Copyright (C) 2005-2015 Florian octo Forster * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -264,6 +264,39 @@ ssize_t sread (int fd, void *buf, size_t count) return (0); } +int read_file (char const *file, void **ret_data, size_t *ret_data_size) +{ + int fd = open (file, O_RDONLY); + if (fd == -1) + return (-1); + + struct stat statbuf = { 0 }; + if (fstat (fd, &statbuf) == -1) + { + close (fd); + return (-1); + } + + size_t data_size = (size_t) statbuf.st_size; + void *data = malloc (data_size); + if (data == NULL) + { + close (fd); + return (-1); + } + + if (sread (fd, data, data_size) != 0) + { + close (fd); + sfree (data); + return (-1); + } + + close (fd); + *ret_data = data; + *ret_data_size = data_size; + return (0); +} /* }}} int read_file */ ssize_t swrite (int fd, const void *buf, size_t count) {