From 43f65c76152017420ce723b4d4ef4230ff072818 Mon Sep 17 00:00:00 2001 From: Vincent Douillet Date: Tue, 28 Nov 2023 08:38:19 +0100 Subject: browse: use khttp_template --- util.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 util.c (limited to 'util.c') diff --git a/util.c b/util.c new file mode 100644 index 0000000..54586c4 --- /dev/null +++ b/util.c @@ -0,0 +1,43 @@ +#include +#include + +#include "util.h" + +int +read_file(char *path, char **output, size_t * read_size) +{ + long file_size; + FILE *fp; + + fp = fopen(path, "r"); + if (fp == NULL) + return -1; + + /* get file size */ + if (fseek(fp, 0L, SEEK_END) != 0) + return -1; + + file_size = ftell(fp); + if (file_size < 0) + return -1; + + /* rewind to file start */ + if (fseek(fp, 0L, SEEK_SET) != 0) + return -1; + + /* create output buffer */ + *output = malloc(sizeof(char) * (file_size + 1)); + if (*output == NULL) + return -1; + + /* read the file */ + *read_size = fread(*output, sizeof(char), file_size, fp); + if (*read_size == 0 || ferror(fp) != 0) { + free(*output); + return -1; + } + /* enforce string termination */ + (*output)[*read_size] = '\0'; + + return 0; +} -- cgit v1.2.3