JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
* rocks.c: refactored stuff and fixed linked-list bug (I think).
[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 *path, char *file)
36 {
37         char *s;
38         size_t plen, flen;
39
40         if(!path || !file) return NULL;
41         plen = strlen(path);
42         flen = strlen(file);
43         s = malloc(2+plen+flen);
44         if(!s) return NULL;
45         memcpy(s, path, plen);
46         s[plen] = '/';
47         memcpy(s+plen+1, file, flen+1);
48         return s;
49 }
50
51 char *
52 add_data_path(char *filename)
53 {
54         return add_path(g_data_dir, filename);
55 }
56
57 int
58 is_dir(char *dirname)
59 {
60         struct stat buf;
61         stat(dirname, &buf);
62         return S_ISDIR(buf.st_mode);
63 }
64
65 int
66 is_file(char *filename)
67 {
68         struct stat buf;
69         stat(filename, &buf);
70         return S_ISREG(buf.st_mode);
71 }
72
73 int
74 find_data_dir(void)
75 {
76         int i;
77         char *data_options[3] = {
78                 "./data",
79                 getenv("VOR_DATA"),
80                 DATA_PREFIX
81         };
82
83         for(i=0; i<3; i++) {
84                 if(!data_options[i]) continue;
85                 g_data_dir = strdup(data_options[i]);
86                 if(is_dir(g_data_dir)) return true;
87         }
88
89         fprintf(stderr, "Can't find VoR data! Tried:\n");
90         for(i=0; i<3; i++) {
91                 fprintf(stderr, "\t%s\n", data_options[i]);
92         }
93         return false;
94 }
95
96 int
97 find_score_file(void)
98 {
99         char *dir, *s;
100         
101         /* in case we get called twice */
102         if(g_score_file) return true;
103
104         dir = getenv("HOME"); if(!dir) return false;
105         s = add_path(dir, ".vor-scores");
106         if(s) {
107                 g_score_file = s;
108                 g_score_mask = 0177;
109                 return is_file(s);
110         } else return false;
111 }
112
113 int
114 find_files(void)
115 {
116         int r;
117         r = find_data_dir();
118         find_score_file();
119         return r;
120 }
121
122 FILE *
123 open_score_file(char *mode)
124 {
125         mode_t old_mask;
126         FILE *f;
127
128         if(!g_score_file) return NULL;
129
130         old_mask = umask(g_score_mask);
131         f = fopen(g_score_file, mode);
132         umask(old_mask);
133         return f;
134 }