diff options
Diffstat (limited to 'src/drain.h')
-rw-r--r-- | src/drain.h | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/drain.h b/src/drain.h new file mode 100644 index 0000000..d0f1111 --- /dev/null +++ b/src/drain.h @@ -0,0 +1,29 @@ +#pragma once +#include <stdio.h> +#include <stdlib.h> + +static char * +drain(FILE *fp, long *size) +{ + if (fseek(fp, 0, SEEK_END) < 0) { + perror("drain:SEEK_END"); + return NULL; + } + *size = ftell(fp); + if (fseek(fp, 0, SEEK_SET) < 0) { + perror("drain:SEEK_SET"); + return NULL; + } + char *const s = malloc(*size + 1); + if (s == NULL) { + perror("drain:malloc"); + return NULL; + } + if ((long)fread(s, 1, *size, fp) != *size) { + perror("drain:fread"); + free(s); + return NULL; + } + s[*size] = '\0'; + return s; +} |