diff -r c2f4c0285180 -r f600b0d1fc5d bl/fileutils.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bl/fileutils.c Fri Jan 27 10:30:16 2012 +0000 @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include +#include + +/* + * Read a file into memory (which should be freed with mem_free when no + * longer required). Returns FALSE on error and outputs a suitable error + * message to stderr. + */ +boolean file_get_contents(const char *filename,char **contents,size_t *length) +{ + FILE *fp; + size_t n; + char *buffer; + String *string; + fp=fopen(filename,"rb"); + if (!fp) + { + perror(filename); + return FALSE; + } + buffer=mem_new(char,1024); + string=string_new(NULL); + do + { + n=fread(buffer,1,1024,fp); + if (n<0) + { + perror(filename); + string_free(string,TRUE); + mem_free(buffer); + free(fp); + return FALSE; + } + string_append_len(string,buffer,n); + } while(n); + mem_free(buffer); + if (length) + *length=string->len; + *contents=string_free(string,FALSE); + fclose(fp); + return TRUE; +}