diff options
author | Vincent Douillet <vincent@vdouillet.fr> | 2023-11-28 08:38:19 +0100 |
---|---|---|
committer | Vincent Douillet <vincent@vdouillet.fr> | 2023-11-29 15:45:28 +0100 |
commit | 43f65c76152017420ce723b4d4ef4230ff072818 (patch) | |
tree | 5c981b2d6949ebd5a5bdefe112383f6874d9342a /util.c | |
parent | a8158372e4173f760d893516c0d8aecbb0e737ac (diff) |
browse: use khttp_template
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 43 |
1 files changed, 43 insertions, 0 deletions
@@ -0,0 +1,43 @@ +#include <stdio.h> +#include <stdlib.h> + +#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; +} |