2 * Copyright (C) 2003 Robert Kooima
4 * NEVERBALL is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
21 #include "base_config.h"
25 /*---------------------------------------------------------------------------*/
27 /* Define the mkdir symbol. */
35 /*---------------------------------------------------------------------------*/
37 static char data_path[MAXSTR];
38 static char user_path[MAXSTR];
41 * Given a path and a file name relative to that path, create an
42 * absolute path name and return a temporary pointer to it.
44 static const char *config_file(const char *path, const char *file)
46 static char absolute[MAXSTR];
48 size_t d = strlen(path);
50 strncpy(absolute, path, MAXSTR - 1);
51 strncat(absolute, "/", MAXSTR - d - 1);
52 strncat(absolute, file, MAXSTR - d - 2);
57 static int config_test(const char *path, const char *file)
63 if ((fp = fopen(config_file(path, file), "r")))
73 const char *config_data(const char *file)
75 return config_file(data_path, file);
78 const char *config_user(const char *file)
80 return config_file(user_path, file);
83 /*---------------------------------------------------------------------------*/
86 * Attempt to find the game data directory. Search the command line
87 * parameter, the environment, and the hard-coded default, in that
88 * order. Confirm it by checking for presence of the named file.
90 int config_data_path(const char *path, const char *file)
94 if (path && config_test(path, file))
96 strncpy(data_path, path, MAXSTR);
100 if ((dir = getenv("NEVERBALL_DATA")) && config_test(dir, file))
102 strncpy(data_path, dir, MAXSTR);
106 if (CONFIG_DATA && config_test(CONFIG_DATA, file))
108 strncpy(data_path, CONFIG_DATA, MAXSTR);
116 * Determine the location of the user's home directory. Ensure there
117 * is a directory there for storing configuration, high scores, and
120 * Under Windows check the APPDATA environment variable and if that's
121 * not set, just assume the user has permission to write to the data
124 int config_user_path(const char *file)
129 if ((dir = getenv("APPDATA")) || (dir = data_path))
131 size_t d = strlen(dir);
133 strncpy(user_path, dir, MAXSTR - 1);
134 strncat(user_path, "\\", MAXSTR - d - 1);
135 strncat(user_path, CONFIG_USER, MAXSTR - d - 2);
138 if ((mkdir(user_path) == 0) || (errno == EEXIST))
139 if (config_test(user_path, file))
144 if ((dir = getenv("HOME")))
146 size_t d = strlen(dir);
148 strncpy(user_path, dir, MAXSTR - 1);
149 strncat(user_path, "/", MAXSTR - d - 1);
150 strncat(user_path, CONFIG_USER, MAXSTR - d - 2);
153 if ((mkdir(user_path, 0777) == 0) || (errno == EEXIST))
154 if (config_test(user_path, file))
161 /*---------------------------------------------------------------------------*/