Windows code leaked memory (result of _fullpath()).
POSIX code passed a null buffer to getcwd(), which is not portable,
and failed to check for errors.
};
static struct keymatch *keylookup(char *key, struct keymatch tbl[]);
};
static struct keymatch *keylookup(char *key, struct keymatch tbl[]);
-static void set_paths(char *);
+static int set_paths(char *);
/*
* read in empire configuration
/*
* read in empire configuration
done:
WORLD_X &= ~1; /* force even */
done:
WORLD_X &= ~1; /* force even */
+ if (set_paths(file) < 0)
+ return -1;
set_paths(char *econfig)
{
set_paths(char *econfig)
{
- char *slash;
- char *cwd = getcwd(NULL, 0);
- /* normalize path separator to '\\', for easier searching: */
- econfig = _fullpath(NULL, econfig, 0);
- slash = strrchr(econfig, '\\');
- configdir = malloc(slash - econfig + 1);
- memcpy(configdir, econfig, slash - econfig);
- configdir[slash - econfig] = 0;
+ p = _fullpath(NULL, econfig, 0);
+ slash = strrchr(p, '\\');
- if ((slash = strrchr(econfig, '/'))) {
- configdir = malloc(slash - econfig + 1);
- memcpy(configdir, econfig, slash - econfig);
- configdir[slash - econfig] = 0;
- } else
- configdir = strdup(cwd);
-
- if (configdir[0] != '/') {
- char *tmp = configdir;
- size_t len = strlen(cwd);
-
- configdir = malloc(len + 1 + strlen(tmp) + 1);
- sprintf(configdir, "%s/%s", cwd, tmp);
- free(tmp);
+ char buf[1024];
+ char *cwd;
+
+ cwd = getcwd(buf, sizeof(buf));
+ p = fnameat(econfig, cwd);
+ if (p[0] != '/') {
+ fprintf(stderr, "Can't get current working directory (%s)\n",
+ strerror(errno));
+ return -1;
+ if (p == econfig)
+ p = strdup(p);
+ slash = strrchr(p, '/');
+ *slash = 0;
+ configdir = realloc(p, slash + 1 - configdir);
+
infodir = fnameat(infodir_conf, configdir);
gamedir = fnameat(gamedir_conf, configdir);
builtindir = fnameat(builtindir_conf, configdir);
schedulefil = fnameat("schedule", configdir);
infodir = fnameat(infodir_conf, configdir);
gamedir = fnameat(gamedir_conf, configdir);
builtindir = fnameat(builtindir_conf, configdir);
schedulefil = fnameat("schedule", configdir);