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.
16 #include <SDL_mixer.h>
23 #include "base_config.h"
27 /*---------------------------------------------------------------------------*/
29 /* Define the mkdir symbol. */
37 /*---------------------------------------------------------------------------*/
39 static char data_path[MAXSTR];
40 static char user_path[MAXSTR];
43 * Given a path and a file name relative to that path, create an
44 * absolute path name and return a temporary pointer to it.
46 static const char *config_file(const char *path, const char *file)
48 static char absolute[MAXSTR];
50 size_t d = strlen(path);
52 strncpy(absolute, path, MAXSTR - 1);
53 strncat(absolute, "/", MAXSTR - d - 1);
54 strncat(absolute, file, MAXSTR - d - 2);
59 static int config_test(const char *path, const char *file)
65 if ((fp = fopen(config_file(path, file), "r")))
75 const char *config_data(const char *file)
77 return config_file(data_path, file);
80 const char *config_user(const char *file)
82 return config_file(user_path, file);
85 /*---------------------------------------------------------------------------*/
88 * Attempt to find the game data directory. Search the command line
89 * paramater, the environment, and the hard-coded default, in that
90 * order. Confirm it by checking for presense of the named file.
92 int config_data_path(const char *path, const char *file)
96 if (path && config_test(path, file))
98 strncpy(data_path, path, MAXSTR);
102 if ((dir = getenv("NEVERBALL_DATA")) && config_test(dir, file))
104 strncpy(data_path, dir, MAXSTR);
108 if (CONFIG_DATA && config_test(CONFIG_DATA, file))
110 strncpy(data_path, CONFIG_DATA, MAXSTR);
118 * Determine the location of the user's home directory. Ensure there
119 * is a directory there for storing configuration, high scores, and
122 * HACK: under Windows just assume the user has permission to write to
123 * the data directory. This is more reliable than trying to devine
124 * anything reasonable from the environment.
126 int config_user_path(const char *file)
129 size_t d = strlen(CONFIG_USER);
131 strncpy(user_path, data_path, MAXSTR - 1);
132 strncat(user_path, "\\", MAXSTR - d - 1);
133 strncat(user_path, CONFIG_USER, MAXSTR - d - 2);
135 if ((mkdir(user_path) == 0) || (errno = EEXIST))
136 if (config_test(user_path, file))
141 if ((dir = getenv("HOME")))
143 size_t d = strlen(dir);
145 strncpy(user_path, dir, MAXSTR - 1);
146 strncat(user_path, "/", MAXSTR - d - 1);
147 strncat(user_path, CONFIG_USER, MAXSTR - d - 2);
150 if ((mkdir(user_path, 0777) == 0) || (errno = EEXIST))
151 if (config_test(user_path, file))
158 /*---------------------------------------------------------------------------*/