forked from jserv/jit-construct
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
33 lines (28 loc) · 602 Bytes
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "util.h"
#define GUARD(expr) assert(!(expr))
void err(const char * const msg)
{
fprintf(stderr, "%s\n", msg);
exit(1);
}
char *read_file(const char * const filename)
{
if (filename == NULL)
return NULL;
FILE *fp = fopen(filename, "r");
if (fp == NULL)
return NULL;
GUARD(fseek(fp, 0, SEEK_END));
long file_size = ftell(fp);
rewind(fp);
size_t code_size = sizeof(char) * file_size;
char *code = malloc(code_size);
if (code == NULL)
return NULL;
fread(code, 1, file_size, fp);
GUARD(fclose(fp));
return code;
}