Skip to content

Commit

Permalink
consolidate getting home and data dir into helper functions (#2101)
Browse files Browse the repository at this point in the history
Co-authored-by: Fabian Greffrath <fabian@brainbug>
  • Loading branch information
fabiangreffrath and Fabian Greffrath authored Dec 23, 2024
1 parent 13bd9e2 commit 395ab83
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 47 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ check_library_exists(m pow "" HAVE_LIBM)
check_include_file("dirent.h" HAVE_DIRENT_H)
check_symbol_exists(strcasecmp "strings.h" HAVE_DECL_STRCASECMP)
check_symbol_exists(strncasecmp "strings.h" HAVE_DECL_STRNCASECMP)
check_symbol_exists(getpwuid "unistd.h;sys/types.h;pwd.h" HAVE_GETPWUID)
check_c_source_compiles("
#include <windows.h>
int main()
Expand Down
1 change: 1 addition & 0 deletions config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#cmakedefine HAVE_DIRENT_H
#cmakedefine01 HAVE_DECL_STRCASECMP
#cmakedefine01 HAVE_DECL_STRNCASECMP
#cmakedefine HAVE_GETPWUID
#cmakedefine HAVE_HIGH_RES_TIMER
#cmakedefine HAVE__DIV64
#cmakedefine HAVE_ALSA
Expand Down
36 changes: 4 additions & 32 deletions src/d_iwad.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,33 +486,12 @@ static void AddIWADPath(const char *path, const char *suffix)
// <http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>
static void AddXdgDirs(void)
{
char *env, *tmp_env;

// Quote:
// > $XDG_DATA_HOME defines the base directory relative to which
// > user specific data files should be stored. If $XDG_DATA_HOME
// > is either not set or empty, a default equal to
// > $HOME/.local/share should be used.
env = M_getenv("XDG_DATA_HOME");
tmp_env = NULL;

if (env == NULL)
{
char *homedir = M_getenv("HOME");
if (homedir == NULL)
{
homedir = "/";
}

tmp_env = M_StringJoin(homedir, "/.local/share");
env = tmp_env;
}
char *env = M_DataDir();

// We support $XDG_DATA_HOME/games/doom (which will usually be
// ~/.local/share/games/doom) as a user-writeable extension to
// the usual /usr/share/games/doom location.
AddIWADDir(M_StringJoin(env, "/games/doom"));
free(tmp_env);

// Quote:
// > $XDG_DATA_DIRS defines the preference-ordered set of base
Expand Down Expand Up @@ -550,11 +529,7 @@ static void AddSteamDirs(void)
{
char *homedir, *steampath;

homedir = M_getenv("HOME");
if (homedir == NULL)
{
homedir = "/";
}
homedir = M_HomeDir();
steampath = M_StringJoin(homedir, "/.steam/root/steamapps/common");

AddIWADPath(steampath, "/Doom 2/base");
Expand Down Expand Up @@ -602,11 +577,8 @@ void BuildIWADDirList(void)
}

// [FG] Add plain HOME directory
env = M_getenv("HOME");
if (env != NULL)
{
AddIWADDir(env);
}
env = M_HomeDir();
AddIWADDir(env);

#ifdef _WIN32

Expand Down
19 changes: 4 additions & 15 deletions src/i_flmusic.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,10 @@ static void ScanDir(const char *dir, boolean recursion)
const char usr_share[] = "/usr/share";
if (strncmp(dir, usr_share, strlen(usr_share)) == 0)
{
char *home_dir = M_getenv("XDG_DATA_HOME");

if (home_dir == NULL)
{
home_dir = M_getenv("HOME");
}

if (home_dir)
{
char *local_share = M_StringJoin(home_dir, "/.local/share");
char *local_dir = M_StringReplace(dir, usr_share, local_share);
free(local_share);
ScanDir(local_dir, true);
free(local_dir);
}
char *local_share = M_DataDir();
char *local_dir = M_StringReplace(dir, usr_share, local_share);
ScanDir(local_dir, true);
free(local_dir);
}
else if (dir[0] == '.')
{
Expand Down
54 changes: 54 additions & 0 deletions src/m_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
#include "m_misc.h"
#include "z_zone.h"

#include "config.h"
#ifdef HAVE_GETPWUID
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#endif

// Check if a file exists

boolean M_FileExists(const char *filename)
Expand Down Expand Up @@ -231,6 +238,53 @@ const char *M_BaseName(const char *path)
}
}

char *M_HomeDir(void)
{
static char *home_dir;

if (home_dir == NULL)
{
home_dir = M_getenv("HOME");

if (home_dir == NULL)
{
#ifdef HAVE_GETPWUID
struct passwd *user_info = getpwuid(getuid());
if (user_info != NULL)
home_dir = user_info->pw_dir;
else
#endif
home_dir = "/";
}
}

return home_dir;
}

// Quote:
// > $XDG_DATA_HOME defines the base directory relative to which
// > user specific data files should be stored. If $XDG_DATA_HOME
// > is either not set or empty, a default equal to
// > $HOME/.local/share should be used.

char *M_DataDir(void)
{
static char *data_dir;

if (data_dir == NULL)
{
data_dir = M_getenv("XDG_DATA_HOME");

if (data_dir == NULL || *data_dir == '\0')
{
const char *home_dir = M_HomeDir();
data_dir = M_StringJoin(home_dir, "/.local/share");
}
}

return data_dir;
}

// Change string to uppercase.

char M_ToUpper(const char c)
Expand Down
2 changes: 2 additions & 0 deletions src/m_misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ char *M_FileCaseExists(const char *file);
boolean M_StrToInt(const char *str, int *result);
char *M_DirName(const char *path);
const char *M_BaseName(const char *path);
char *M_HomeDir(void);
char *M_DataDir(void);
char M_ToUpper(const char c);
void M_StringToUpper(char *text);
char M_ToLower(const char c);
Expand Down

0 comments on commit 395ab83

Please sign in to comment.