#define _GNU_SOURCE #include #include #include #include #include #include #include #include #include "read-cache.h" char *cache; char *cache_end; char *current_cache_entry; void cache_init(const char *path) { FILE *f = cache_open(path, "r"); if (!f) { printf("no cache\n"); return; } struct stat buf; fstat(fileno(f), &buf); cache = malloc(buf.st_size); fread(cache, 1, buf.st_size, f); fclose(f); /* set the current entry to the begining of the cache */ current_cache_entry = cache; cache_end = cache + buf.st_size; } FILE *cache_open(const char *path, const char *mode) { FILE *cache; char *cache_path; char *path_copy = strdup(path); char *dir = dirname(path_copy); asprintf(&cache_path, "%s/cache", dir); cache = fopen(cache_path, mode); free(cache_path); free(path_copy); return cache; } #define MIN(a, b) (a) < (b) ? (a) : (b) static void print_hash(const unsigned char *hash) { int i; for (i=0; i