quick commit
[webserver.git] / file.c
diff --git a/file.c b/file.c
new file mode 100644 (file)
index 0000000..c720dba
--- /dev/null
+++ b/file.c
@@ -0,0 +1,36 @@
+#include <malloc.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "debug.h"
+
+#include "file.h"
+
+/* read full file */
+
+int readfile (unsigned char **buffer, char *filename)
+{
+
+    /* open file */
+
+    FILE *fd = fopen (filename, "rb");
+    if (fd == NULL) {
+        VERBOSE (WARNING, PRINT ("Can't open file (%s)\n", filename));
+        return -1;
+    }
+
+    fseek (fd, 0, SEEK_END);
+    int size = ftell (fd);
+    fseek (fd, 0, SEEK_SET);  /* same as rewind(f); */
+
+    /* read full file */
+
+    *buffer = calloc (size + 1, 1);
+    fread (*buffer, size, 1, fd);
+    fclose (fd);
+
+    *buffer[size] = 0;
+    return size;
+}
+
+/* vim: set ts=4 sw=4 et: */