2020-03-18 11:29:08 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <malloc.h>
|
2020-03-18 11:53:11 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2020-03-18 11:29:08 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2020-04-01 12:40:21 +00:00
|
|
|
char *readFile(char *filename, char *mode, size_t *size)
|
2020-03-18 11:29:08 +00:00
|
|
|
{
|
2020-03-18 15:55:27 +00:00
|
|
|
FILE *file;
|
|
|
|
char *buffer;
|
2020-04-01 12:40:21 +00:00
|
|
|
size_t bytes;
|
2020-03-18 11:29:08 +00:00
|
|
|
|
2020-03-18 15:55:27 +00:00
|
|
|
if((file = fopen(filename, mode)) == NULL)
|
2020-03-18 11:29:08 +00:00
|
|
|
{
|
2020-03-18 11:53:11 +00:00
|
|
|
printf("ERROR open file %s: %s\n", filename, strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
fseek(file, 0L, SEEK_END);
|
2020-04-01 12:40:21 +00:00
|
|
|
bytes = (size_t) ftell(file);
|
2020-03-18 11:53:11 +00:00
|
|
|
fseek(file, 0L, SEEK_SET);
|
2020-03-18 11:29:08 +00:00
|
|
|
|
2020-03-18 15:55:27 +00:00
|
|
|
if((buffer = calloc(bytes, sizeof(char))) == NULL)
|
2020-03-18 11:53:11 +00:00
|
|
|
{
|
|
|
|
printf("ERROR allocating memory: %s\n", strerror(errno));
|
2020-03-18 11:29:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 15:55:27 +00:00
|
|
|
fread(buffer, sizeof(char), bytes, file);
|
2020-03-18 11:29:08 +00:00
|
|
|
fclose(file);
|
|
|
|
|
2020-03-18 15:55:27 +00:00
|
|
|
if (size != NULL)
|
|
|
|
{
|
|
|
|
*size = bytes;
|
|
|
|
}
|
|
|
|
|
2020-03-18 11:53:11 +00:00
|
|
|
return buffer;
|
2020-04-01 20:17:35 +00:00
|
|
|
}
|