JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
now deleting dots which hit rocks.
[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 <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25
26 #include "common.h"
27 #include "config.h"
28 #include "file.h"
29
30 char *g_data_dir;
31 char *g_score_file;
32 mode_t g_score_mask;
33
34 char *
35 add_path(char *filename)
36 {
37         static char r[MAX_PATH_LEN];
38         snprintf(r, MAX_PATH_LEN, "%s/%s", g_data_dir, filename);
39         return r;
40 }
41
42 int
43 is_dir(char *dirname)
44 {
45         struct stat buf;
46         stat(dirname, &buf);
47         return S_ISDIR(buf.st_mode);
48 }
49
50 int
51 is_file(char *filename)
52 {
53         struct stat buf;
54         stat(filename, &buf);
55         return S_ISREG(buf.st_mode);
56 }
57
58 int
59 find_data_dir(void)
60 {
61         int i;
62         char *data_options[3] = {
63                 "./data",
64                 getenv("VOR_DATA"),
65                 DATA_PREFIX
66         };
67
68         for(i=0; i<3; i++) {
69                 if(!data_options[i]) continue;
70                 g_data_dir = strdup(data_options[i]);
71                 if(is_dir(g_data_dir)) return true;
72         }
73
74         fprintf(stderr, "Can't find VoR data! Tried:\n");
75         for(i=0; i<3; i++) {
76                 fprintf(stderr, "\t%s\n", data_options[i]);
77         }
78         return false;
79 }
80
81 int
82 find_score_file(void)
83 {
84         g_score_file = malloc(MAX_PATH_LEN);
85         snprintf(g_score_file, MAX_PATH_LEN,
86                         "%s/.vor-scores", getenv("HOME"));
87         g_score_mask = 0177;
88         if(is_file(g_score_file)) return true;
89
90         return false;
91 }
92
93 int
94 find_files(void)
95 {
96         int r;
97         r = find_data_dir();
98         find_score_file();
99         return r;
100 }
101
102 FILE *
103 open_score_file(char *mode)
104 {
105         mode_t old_mask;
106         FILE *f = NULL;
107
108         if(!g_score_file) return f;
109
110         old_mask = umask(g_score_mask);
111         f = fopen(g_score_file, mode);
112
113         umask(old_mask);
114         return f;
115 }