Merge remote-tracking branch 'refs/remotes/origin/master'
[webserver.git] / file.c
1 #include <malloc.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #include "debug.h"
6
7 #include "file.h"
8
9 /* read full file */
10
11 int readfile (char **buffer, char *filename)
12 {
13
14 /* open file */
15
16 FILE *fd = fopen (filename, "rb");
17 if (fd == NULL) {
18 VERBOSE (WARNING, PRINT ("Can't open file (%s)\n", filename));
19 return -1;
20 }
21
22 fseek (fd, 0, SEEK_END);
23 int size = ftell (fd);
24 fseek (fd, 0, SEEK_SET); /* same as rewind(f); */
25
26 /* read full file */
27
28 *buffer = calloc (size + 1, 1);
29 fread (*buffer, size, 1, fd);
30 fclose (fd);
31
32 *buffer[size] = 0;
33 return size;
34 }
35
36 /* vim: set ts=4 sw=4 et: */