polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

file.c (651B)


      1 
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include "file.h"
      5 
      6 #include <sys/stat.h>
      7 
      8 time_t file_mtime(const char *filename) {
      9 	// TODO: windows file_mtime
     10 	struct stat stats;
     11 	stat(filename, &stats);
     12 
     13 	return stats.st_mtime;
     14 }
     15 
     16 char *file_contents(const char *filename, size_t *length) {
     17   FILE *f = fopen(filename, "r");
     18   char *buffer;
     19 
     20   if (!f) {
     21       fprintf(stderr, "Unable to open %s for reading\n", filename);
     22       return NULL;
     23   }
     24 
     25   fseek(f, 0, SEEK_END);
     26   *length = (size_t)ftell(f);
     27   fseek(f, 0, SEEK_SET);
     28 
     29   buffer = malloc(*length+1);
     30   *length = fread(buffer, 1, *length, f);
     31   fclose(f);
     32 
     33   buffer[*length] = 0;
     34 
     35   return buffer;
     36 }