#include "../src/config.h" #include #include #include #include "slurp.h" static const size_t NULL_TERMINATOR = sizeof((char)'\0'); int slurp_for_tests(const char *const filename, char **out) { int rc = -1; FILE *file = NULL; char *str = NULL; file = fopen(filename, "r"); if (file == NULL) { perror("fopen(filename, \"r\")"); goto out; } if (fseek(file, 0L, SEEK_END)) { perror("fseek(file, 0L, SEEK_END)"); goto out; } const long lsize = ftell(file); if (lsize == -1) { perror("ftell(file)"); goto out; } const size_t size = (size_t)lsize; errno = 0; rewind(file); if (errno) { perror("rewind(file)"); goto out; } str = malloc(size + NULL_TERMINATOR); if (str == NULL) { perror("malloc(...)"); goto out; } if (fread(str, sizeof(char), size, file) != size) { perror("fread(...)"); goto out; } str[size] = '\0'; *out = str; rc = 0; out: if (file != NULL) { if (fclose(file)) { perror("flcose(file"); rc = -1; } } if (rc) { if (str != NULL) { free(str); } } return rc; }