Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consolidate getting home and data dir into helper functions #2101

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will behave differently on Windows. What's the closest pendant to HOME on Windows, %USERPROFILE%?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer not to use the profile directory in Windows. “Serious” programs and commercial games use it, but I like simple conservative Doom ports to only create files in the port directory.

I think only GZDoom uses the profile directory in Windows, and one of the popular user questions is how to disable that. They added it because commercial GZDoom based games require it.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realized that the sole slash is entirely meaningless on Windows as a path string, and that it's thus okay to return it unconditionally as it's about the same as not returning any path string at all.

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