util.c (806B)
1#include <stdio.h> 2#include <stdlib.h> 3#include <sys/stat.h> 4 5#include "util.h" 6 7/* read file named FILENAME into an array of *len bytes, 8 returning NULL on error */ 9uint8_t *readfile(const char *filename, size_t *len) 10{ 11 *len = 0; 12 struct stat st; 13 if (0 != stat(filename, &st)) return NULL; 14 *len = st.st_size; 15 FILE *f = fopen(filename, "r"); 16 if (!f) return NULL; 17 uint8_t *s = (uint8_t *) malloc(sizeof(uint8_t) * *len); 18 if (!s) return NULL; 19 if (fread(s, 1, *len, f) != *len) { 20 free(s); 21 s = NULL; 22 } 23 fclose(f); 24 return s; 25} 26 27mytime gettime(void) { 28 mytime t; 29 gettimeofday(&t, NULL); 30 return t; 31} 32 33/* time difference in seconds */ 34double elapsed(mytime t1, mytime t0) 35{ 36 return (double)(t1.tv_sec - t0.tv_sec) + 37 (double)(t1.tv_usec - t0.tv_usec) * 1.0E-6; 38} 39