aboutsummaryrefslogtreecommitdiff
path: root/src/lib/file.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/file.c')
-rw-r--r--src/lib/file.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/lib/file.c b/src/lib/file.c
new file mode 100644
index 0000000..3464866
--- /dev/null
+++ b/src/lib/file.c
@@ -0,0 +1,41 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "file.h"
+#include "lib.h"
+
+struct filedata readfile(FILE *fp) {
+ struct filedata f;
+
+ f.lc = 0;
+ f.len = 0;
+
+ unsigned bufsize = 4;
+
+ f.buf = malloc(bufsize);
+ if (f.buf == NULL)
+ die("malloc");
+
+ char c;
+ while (fread(&c, sizeof(char), 1, fp) > 0) {
+ if (f.len == bufsize - 1) {
+ bufsize *= 2;
+
+ char *new_buf = realloc(f.buf, bufsize);
+ if (f.buf == NULL) {
+ free(f.buf);
+ die("realloc");
+ }
+
+ f.buf = new_buf;
+ }
+
+ if (c == '\n') {
+ f.lc++;
+ }
+
+ f.buf[f.len++] = c;
+ }
+
+ return f;
+}