diff options
author | EuAndreh <eu@euandre.org> | 2022-01-28 07:33:30 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-01-01 12:35:01 -0300 |
commit | 9f554a72b01705ebf6be66143c5e69b09b3c1372 (patch) | |
tree | fadf04038b2d5df32fd4b5bc93ba291269c322a0 /tests/slurp.c | |
download | pindaiba-9f554a72b01705ebf6be66143c5e69b09b3c1372.tar.gz pindaiba-9f554a72b01705ebf6be66143c5e69b09b3c1372.tar.xz |
Init project: copy files and skeletons from others
Diffstat (limited to 'tests/slurp.c')
-rw-r--r-- | tests/slurp.c | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/tests/slurp.c b/tests/slurp.c new file mode 100644 index 0000000..7b5936f --- /dev/null +++ b/tests/slurp.c @@ -0,0 +1,71 @@ +#include "../src/config.h" + +#include <stdio.h> +#include <stdlib.h> + +#include "slurp.h" + +int +slurp_for_tests(const char *const FNAME, char **strref) { + int rc = 0; + + FILE *file = NULL; + char *str = NULL; + + file = fopen(FNAME, "r"); + if (!file) { + perror("fopen(FNAME, \"r\")"); + rc = -1; + goto out; + } + + if (fseek(file, 0L, SEEK_END)) { + perror("fseek(file, 0L, SEEK_END)"); + rc = -1; + goto out; + } + + const long lsize = ftell(file); + if (lsize == -1) { + perror("ftell(file)"); + rc = -1; + goto out; + } + const size_t size = (size_t)lsize + sizeof(char); + + if (fseek(file, 0L, SEEK_SET)) { + perror("fseek(file, 0L, SEEK_SET)"); + rc = -1; + goto out; + } + + str = malloc(size); + if (!str) { + perror("malloc(...)"); + rc = -1; + goto out; + } + + if (fread(str, sizeof(char), size - 1, file) != size - 1) { + perror("fread(...)"); + rc = -1; + goto out; + } + str[size - 1] = '\0'; + *strref = str; + +out: + if (file) { + if (fclose(file)) { + perror("flcose(file"); + rc = -1; + } + } + + if (rc) { + if (str) { + free(str); + } + } + return rc; +} |