1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <unistd.h>
5
6#include <sys/stat.h>
7#include <sys/types.h>
8
9#define MIN(A, B) ((A) < (B) ? (A) : (B))
10
11int
12xmemcmp(void *s1, size_t l1, void *s2, size_t l2)
13{
14 return memcmp(s1, s2, MIN(l1, l2));
15}
16
17unsigned char*
18readfile(char *fp)
19{
20 FILE *fd;
21 struct stat st;
22 size_t len, read;
23 unsigned char *fc;
24
25 if (stat(fp, &st))
26 return NULL;
27 len = st.st_size;
28
29 fc = malloc(len + 1);
30 if (!fc)
31 return NULL;
32 if (!(fd = fopen(fp, "r")))
33 return NULL;
34
35 read = fread(fc, sizeof(unsigned char), len, fd);
36 if (ferror(fd))
37 return NULL;
38 if (fclose(fd))
39 return NULL;
40
41 fc[read] = '\0';
42 return fc;
43}