X-Git-Url: https://jasonwoof.com/gitweb/?p=vor.git;a=blobdiff_plain;f=file.c;h=0c6177f84d57eb65fbf35cac804beda0eeb5e2c0;hp=b9bc7e2e404838b0b1129071de43416ed24d0d6a;hb=0224eb908d4084f52a22c8d64404a0efb4acfa35;hpb=a2af96e68b210b7c3db90b7ec474498c5611a3a7 diff --git a/file.c b/file.c index b9bc7e2..0c6177f 100644 --- a/file.c +++ b/file.c @@ -16,29 +16,69 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "config.h" -#include "file.h" - #include #include #include -#include +#ifndef WIN32 +# include +#endif #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] = PATH_SEPARATOR; + memcpy(s+plen+1, file, flen+1); + return s; } -int +char * +add_data_path(char *filename) +{ + return add_path(g_data_dir, filename); +} + +#ifdef WIN32 + +bool +is_dir(char *dirname) +{ + WIN32_FILE_ATTRIBUTE_DATA buf; + if(!GetFileAttributesEx(dirname, GetFileExInfoStandard, &buf)) + return false; + return buf.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; +} + +bool +is_file(char *filename) +{ + WIN32_FILE_ATTRIBUTE_DATA buf; + if(!GetFileAttributesEx(filename, GetFileExInfoStandard, &buf)) + return false; + return buf.dwFileAttributes & FILE_ATTRIBUTE_NORMAL; +} + +#else /* !WIN32 */ + +bool is_dir(char *dirname) { struct stat buf; @@ -46,7 +86,7 @@ is_dir(char *dirname) return S_ISDIR(buf.st_mode); } -int +bool is_file(char *filename) { struct stat buf; @@ -54,17 +94,20 @@ is_file(char *filename) return S_ISREG(buf.st_mode); } -int +#endif /* !WIN32 */ + +bool find_data_dir(void) { int i; char *data_options[3] = { - "./data", getenv("VOR_DATA"), - "/usr/share/vor" + "data", + DATA_PREFIX }; 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; } @@ -76,42 +119,39 @@ find_data_dir(void) return false; } -int +bool 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 true; + } else return false; } -int +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; + 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; }