JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
hmm...apparently you can't strdup a NULL pointer :)
[vor.git] / file.c
1 /* Variations on RockDodger
2  * Copyright (C) 2004 Joshua Grams <josh@qualdan.com>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18
19 #include "config.h"
20 #include "file.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 char *g_data_dir;
30 char *g_score_file;
31 mode_t g_score_mode;
32
33 char *
34 add_path(char *filename)
35 {
36         static char r[MAX_PATH_LEN];
37         snprintf(r, MAX_PATH_LEN, "%s/%s", g_data_dir, filename);
38         return r;
39 }
40
41 int
42 is_dir(char *dirname)
43 {
44         struct stat buf;
45         stat(dirname, &buf);
46         return S_ISDIR(buf.st_mode);
47 }
48
49 int
50 is_file(char *filename)
51 {
52         struct stat buf;
53         stat(filename, &buf);
54         return S_ISREG(buf.st_mode);
55 }
56
57 int
58 find_data_dir(void)
59 {
60         int i;
61         char *data_options[3] = {
62                 "./data",
63                 getenv("VOR_DATA"),
64                 DATA_PREFIX
65         };
66
67         for(i=0; i<3; i++) {
68                 if(!data_options[i]) continue;
69                 g_data_dir = strdup(data_options[i]);
70                 if(is_dir(g_data_dir)) return true;
71         }
72
73         fprintf(stderr, "Can't find VoR data! Tried:\n");
74         for(i=0; i<3; i++) {
75                 fprintf(stderr, "\t%s\n", data_options[i]);
76         }
77         return false;
78 }
79
80 int
81 find_score_file(void)
82 {
83         g_score_file = add_path("scores");
84         g_score_mode = 0111;
85         if(is_file(g_score_file)) return true;
86
87         g_score_file = malloc(MAX_PATH_LEN);
88         snprintf(g_score_file, MAX_PATH_LEN,
89                         "%s/.vor-high", getenv("HOME"));
90         g_score_mode = 0177;
91         if(is_file(g_score_file)) return true;
92
93         return false;
94 }
95
96 int
97 find_files(void)
98 {
99         int r;
100         r = find_data_dir();
101         find_score_file();
102         return r;
103 }
104
105 FILE *
106 open_score_file(char *mode)
107 {
108         mode_t old_mask;
109         FILE *f = NULL;
110
111         if(!g_score_file) return f;
112
113         old_mask = umask(g_score_mode);
114         f = fopen(g_score_file, mode);
115
116         umask(old_mask);
117         return f;
118 }