PageRenderTime 34ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/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. #include "easy.h"
  6. struct space *loadfile(char *name)
  7. {
  8. FILE *file;
  9. struct stat stats;
  10. char *loaded_file;
  11. struct space sp; // chage it to a pointer
  12. if((file = fopen(name, "rb")) == NULL){
  13. printf("can't open: %s\n", name);
  14. exit(1);
  15. }
  16. fstat(fileno(file), &stats);
  17. loaded_file = alloc_mem(stats.st_size+4);
  18. if(fread(loaded_file, sizeof(byte) , sizeof(byte) * (int) stats.st_size, file) != stats.st_size){
  19. printf("erro loading file\n");
  20. exit(1);
  21. }
  22. sp.p = loaded_file;
  23. sp.size = stats.st_size;
  24. return(&sp);
  25. }
  26. void unloadfile(struct space *sp)
  27. {
  28. free((void *) sp->p);
  29. }
  30. byte *alloc_mem(u__int size)
  31. {
  32. char *ptr;
  33. ptr = (void*) calloc(size, sizeof(byte));
  34. if(!ptr){
  35. printf("Fail Allocking %ibytes\n", size);
  36. exit(1);
  37. }
  38. return(ptr);
  39. }
  40. void free_mem(byte *bp)
  41. {
  42. free((void *) bp);
  43. }