PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/BF/lib_easy/easy.c

http://dlemos-src.googlecode.com/
C | 51 lines | 39 code | 8 blank | 4 comment | 5 complexity | 83ee431880f112c58d62203fa438f2db MD5 | raw file
 1/*
 2 * Ease lib
 3 * by x_sp4g3r@yahoo.com.br
 4*/
 5
 6#include "easy.h"
 7
 8struct space *loadfile(char *name)
 9{
10	FILE *file;
11	struct stat stats;
12	char *loaded_file;
13	struct space sp;		// chage it to a pointer
14
15	if((file = fopen(name, "rb")) == NULL){
16		printf("can't open: %s\n", name);
17		exit(1);
18	}
19	fstat(fileno(file), &stats);
20	loaded_file = alloc_mem(stats.st_size+4);
21	if(fread(loaded_file, sizeof(byte) , sizeof(byte) * (int) stats.st_size, file) != stats.st_size){
22		printf("erro loading file\n");
23		exit(1);
24	}
25	sp.p = loaded_file;
26	sp.size = stats.st_size;
27	return(&sp);
28}
29
30void unloadfile(struct space *sp)
31{
32	free((void *) sp->p);
33}
34
35byte *alloc_mem(u__int size)
36{
37	char *ptr;
38
39	ptr = (void*) calloc(size, sizeof(byte));
40	if(!ptr){
41		printf("Fail Allocking %ibytes\n", size);
42		exit(1);
43	}
44	return(ptr);
45}
46
47void free_mem(byte *bp)
48{
49	free((void *) bp);
50}
51