Support XDG config directory.

This commit is contained in:
Brandon Wright 2017-05-06 18:39:01 -05:00
parent 4c2b41e6f2
commit 75b867128b
1 changed files with 49 additions and 7 deletions

View File

@ -15,25 +15,67 @@
#include "gtk_sound.h"
#include "gtk_display.h"
static int
directory_exists (const char *directory)
{
DIR *dir;
dir = opendir (directory);
if (dir)
{
closedir (dir);
return TRUE;
}
return FALSE;
}
char *
get_config_dir (void)
{
char *homedir, *configdir;
char *home_dir = NULL,
*classic_config_dir = NULL,
*xdg_config_dir = NULL,
*xdg_snes9x_dir = NULL;
/* Find config directory */
homedir = getenv ("HOME");
home_dir = getenv ("HOME");
xdg_config_dir = getenv ("XDG_CONFIG_DIR");
if (!homedir)
if (!home_dir && !xdg_config_dir)
{
configdir = strdup (".snes9x");
return strdup (".snes9x");
}
if (!xdg_config_dir)
{
xdg_snes9x_dir = (char *) malloc (strlen (home_dir) + 16);
sprintf (xdg_snes9x_dir, "%s/.config/snes9x", home_dir);
}
else
{
configdir = (char *) malloc (strlen (homedir) + 9);
sprintf (configdir, "%s/.snes9x", homedir);
xdg_snes9x_dir = (char *) malloc (strlen (xdg_config_dir) + 9);
sprintf (xdg_snes9x_dir, "%s/snes9x", xdg_config_dir);
}
return configdir;
classic_config_dir = (char *) malloc (strlen (home_dir) + 9);
sprintf (classic_config_dir, "%s/.snes9x", home_dir);
char *config_dir;
if (directory_exists (classic_config_dir) && !directory_exists(xdg_snes9x_dir))
{
free (xdg_snes9x_dir);
config_dir = classic_config_dir;
}
else
{
free (classic_config_dir);
config_dir = xdg_snes9x_dir;
}
return config_dir;
}
char *