X-Git-Url: https://jasonwoof.com/gitweb/?p=vor.git;a=blobdiff_plain;f=file.c;h=1e02c8eace6d47dad5c96f5d0e0bffd281f534d8;hp=45532d3bea7abf99616ea640896ed6d80f044aec;hb=31a00c721be0b569bc554069cc0a3eb2864eb61f;hpb=189cc09109987a44aa8af4419a2d106d675a3ea0 diff --git a/file.c b/file.c index 45532d3..1e02c8e 100644 --- a/file.c +++ b/file.c @@ -19,7 +19,9 @@ #include #include #include -#include +#ifndef WIN32 +# include +#endif #include #include @@ -29,39 +31,63 @@ char *g_data_dir; char *g_score_file; -mode_t g_score_mask; + +static char * +add_path(char *path, char *file) +{ + char *r, *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] = PATH_SEPARATOR; + memcpy(s+plen+1, file, flen+1); + return s; +} char * -add_path(char *filename) +add_data_path(char *filename) { - static char r[MAX_PATH_LEN]; - snprintf(r, MAX_PATH_LEN, "%s/%s", g_data_dir, filename); - return r; + return add_path(g_data_dir, filename); } -int -is_dir(char *dirname) +#ifdef WIN32 + +static bool +find_data_dir(void) { - struct stat buf; - stat(dirname, &buf); - return S_ISDIR(buf.st_mode); + g_data_dir = "."; + return true; } -int -is_file(char *filename) +static bool +find_score_file(void) +{ + g_score_file = "scores"; + return true; +} + +#else /* !WIN32 */ + +static bool +is_dir(char *dirname) { struct stat buf; - stat(filename, &buf); - return S_ISREG(buf.st_mode); + stat(dirname, &buf); + return S_ISDIR(buf.st_mode); } -int +static bool find_data_dir(void) { int i; char *data_options[3] = { - "./data", getenv("VOR_DATA"), + "data", DATA_PREFIX }; @@ -78,38 +104,33 @@ find_data_dir(void) return false; } -int +static bool find_score_file(void) { - g_score_file = malloc(MAX_PATH_LEN); - snprintf(g_score_file, MAX_PATH_LEN, - "%s/.vor-scores", getenv("HOME")); - g_score_mask = 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; + return true; + } else return false; } -int +#endif /* !WIN32 */ + +bool find_files(void) { - int r; - r = find_data_dir(); - find_score_file(); - return r; + return find_data_dir() && find_score_file(); } FILE * open_score_file(char *mode) { - mode_t old_mask; - FILE *f = NULL; - - if(!g_score_file) return f; - - old_mask = umask(g_score_mask); - f = fopen(g_score_file, mode); - - umask(old_mask); - return f; + if(!g_score_file) return NULL; + return fopen(g_score_file, mode); }