X-Git-Url: https://jasonwoof.com/gitweb/?p=vor.git;a=blobdiff_plain;f=file.c;h=ee14299b6df62773a055330d11b2f4fa8cb7b0b9;hp=a8f9ff40b39c6690a17b3605b3cc88d31e045365;hb=cd7fb220b16a73c15ff9dff7a5627bf78478875f;hpb=6f82bba8833f187abebab53375ac87905487cf8c diff --git a/file.c b/file.c index a8f9ff4..ee14299 100644 --- a/file.c +++ b/file.c @@ -16,9 +16,6 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "config.h" -#include "file.h" - #include #include #include @@ -26,16 +23,35 @@ #include #include +#include "common.h" +#include "config.h" +#include "file.h" + char *g_data_dir; char *g_score_file; -mode_t g_score_mode; +mode_t g_score_mask; char * -load_file(char *filename) +add_path(char *path, char *file) { - static char r[MAX_PATH_LEN]; - snprintf(r, MAX_PATH_LEN, "%s/%s", g_data_dir, filename); - return r; + char *s; + size_t plen, flen; + + if(!path || !file) return NULL; + plen = strlen(path); + flen = strlen(file); + s = malloc(2+plen+flen); + if(!s) return NULL; + memcpy(s, path, plen); + s[plen] = '/'; + memcpy(s+plen+1, file, flen+1); + return s; +} + +char * +add_data_path(char *filename) +{ + return add_path(g_data_dir, filename); } int @@ -65,6 +81,7 @@ find_data_dir(void) }; for(i=0; i<3; i++) { + if(!data_options[i]) continue; g_data_dir = strdup(data_options[i]); if(is_dir(g_data_dir)) return true; } @@ -79,17 +96,18 @@ find_data_dir(void) int find_score_file(void) { - g_score_file = load_file("scores"); - g_score_mode = 0111; - if(is_file(g_score_file)) return true; - - g_score_file = malloc(MAX_PATH_LEN); - snprintf(g_score_file, MAX_PATH_LEN, - "%s/.vor-high", getenv("HOME")); - g_score_mode = 0177; - if(is_file(g_score_file)) return true; - - return false; + char *dir, *s; + + /* in case we get called twice */ + if(g_score_file) return true; + + dir = getenv("HOME"); if(!dir) return false; + s = add_path(dir, ".vor-scores"); + if(s) { + g_score_file = s; + g_score_mask = 0177; + return is_file(s); + } else return false; } int @@ -105,13 +123,12 @@ FILE * open_score_file(char *mode) { mode_t old_mask; - FILE *f = NULL; + FILE *f; - if(!g_score_file) return f; + if(!g_score_file) return NULL; - old_mask = umask(g_score_mode); + old_mask = umask(g_score_mask); f = fopen(g_score_file, mode); - umask(old_mask); return f; }