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.
22 #include "base_config.h"
26 /*---------------------------------------------------------------------------*/
28 /* Define the mkdir symbol. */
36 /*---------------------------------------------------------------------------*/
38 static char data_path[MAXSTR];
39 static char user_path[MAXSTR];
42 * Given a path and a file name relative to that path, create an
43 * absolute path name and return a temporary pointer to it.
45 static const char *config_file(const char *path, const char *file)
47 static char absolute[MAXSTR];
49 size_t d = strlen(path);
51 strncpy(absolute, path, MAXSTR - 1);
52 strncat(absolute, "/", MAXSTR - d - 1);
53 strncat(absolute, file, MAXSTR - d - 2);
58 static int config_test(const char *path, const char *file)
64 if ((fp = fopen(config_file(path, file), "r")))
74 const char *config_data(const char *file)
76 return config_file(data_path, file);
79 const char *config_user(const char *file)
81 return config_file(user_path, file);
84 /*---------------------------------------------------------------------------*/
87 * Attempt to find the game data directory. Search the command line
88 * parameter, the environment, and the hard-coded default, in that
89 * order. Confirm it by checking for presence of the named file.
91 int config_data_path(const char *path, const char *file)
95 if (path && config_test(path, file))
97 strncpy(data_path, path, MAXSTR);
101 if ((dir = getenv("NEVERBALL_DATA")) && config_test(dir, file))
103 strncpy(data_path, dir, MAXSTR);
107 if (CONFIG_DATA && config_test(CONFIG_DATA, file))
109 strncpy(data_path, CONFIG_DATA, MAXSTR);
117 * Determine the location of the user's home directory. Ensure there
118 * is a directory there for storing configuration, high scores, and
121 * Under Windows check the APPDATA environment variable and if that's
122 * not set, just assume the user has permission to write to the data
125 int config_user_path(const char *file)
130 if ((dir = getenv("APPDATA")) || (dir = data_path))
132 size_t d = strlen(dir);
134 strncpy(user_path, dir, MAXSTR - 1);
135 strncat(user_path, "\\", MAXSTR - d - 1);
136 strncat(user_path, CONFIG_USER, MAXSTR - d - 2);
139 if ((mkdir(user_path) == 0) || (errno == EEXIST))
140 if (config_test(user_path, file))
145 if ((dir = getenv("HOME")))
147 size_t d = strlen(dir);
149 strncpy(user_path, dir, MAXSTR - 1);
150 strncat(user_path, "/", MAXSTR - d - 1);
151 strncat(user_path, CONFIG_USER, MAXSTR - d - 2);
154 if ((mkdir(user_path, 0777) == 0) || (errno == EEXIST))
155 if (config_test(user_path, file))
162 /*---------------------------------------------------------------------------*/