Commit | Line | Data |
---|---|---|
4baf6839 LM |
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 | ||
cae06547 | 11 | int readfile (char **buffer, char *filename) |
4baf6839 LM |
12 | { |
13 | ||
14 | /* open file */ | |
15 | ||
50c7ef81 | 16 | VERBOSE (DEBUG, PRINT ("Opening file %s\n", filename)); |
4baf6839 LM |
17 | FILE *fd = fopen (filename, "rb"); |
18 | if (fd == NULL) { | |
19 | VERBOSE (WARNING, PRINT ("Can't open file (%s)\n", filename)); | |
20 | return -1; | |
21 | } | |
22 | ||
50c7ef81 | 23 | VERBOSE (DEBUG, PRINT ("Seek file size\n")); |
4baf6839 LM |
24 | fseek (fd, 0, SEEK_END); |
25 | int size = ftell (fd); | |
26 | fseek (fd, 0, SEEK_SET); /* same as rewind(f); */ | |
27 | ||
28 | /* read full file */ | |
29 | ||
50c7ef81 | 30 | VERBOSE (DEBUG, PRINT ("Reading %d bytes\n", size)); |
4baf6839 LM |
31 | *buffer = calloc (size + 1, 1); |
32 | fread (*buffer, size, 1, fd); | |
33 | fclose (fd); | |
34 | ||
50c7ef81 | 35 | (*buffer)[size] = 0; |
4baf6839 LM |
36 | return size; |
37 | } | |
38 | ||
39 | /* vim: set ts=4 sw=4 et: */ |