Use `.config/ly` instead of dumping in `.config/`

Using `.config/ly` (`XDG_CONFIG_HOME/ly`) instead of `.config/` (`XDG_CONFIG_HOME`)
Added many safety checks so it's impossible to break
This commit is contained in:
Cavernosa 2022-08-14 01:01:30 +00:00 committed by GitHub
parent 5787c26e69
commit 793f469035
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 2 deletions

View File

@ -307,18 +307,35 @@ void xauth(const char* display_name, const char* shell, char* pwd)
if ((xauth_dir == NULL) || (*xauth_dir == '\0')) if ((xauth_dir == NULL) || (*xauth_dir == '\0'))
{ {
xauth_dir = getenv("XDG_CONFIG_HOME"); xauth_dir = getenv("XDG_CONFIG_HOME");
struct stat sb;
if ((xauth_dir == NULL) || (*xauth_dir == '\0')) if ((xauth_dir == NULL) || (*xauth_dir == '\0'))
{ {
xauth_dir = strdup(pwd); xauth_dir = strdup(pwd);
strcat(xauth_dir, "/.config"); strcat(xauth_dir, "/.config");
struct stat sb;
stat(xauth_dir, &sb); stat(xauth_dir, &sb);
if (!S_ISDIR(sb.st_mode)) if (S_ISDIR(sb.st_mode))
{
strcat(xauth_dir, "/ly");
}
else
{ {
xauth_dir = pwd; xauth_dir = pwd;
xauth_file = ".lyxauth"; xauth_file = ".lyxauth";
} }
} }
else
{
strcat(xauth_dir, "/ly");
}
// If .config/ly/ or XDG_CONFIG_HOME/ly/ doesn't exist and can't create the directory, use pwd
// Passing pwd beforehand is safe since stat will always evaluate false
stat(xauth_dir, &sb);
if (!S_ISDIR(sb.st_mode) && mkdir(xauth_dir, 0777) == -1)
{
xauth_dir = pwd;
xauth_file = ".lyxauth";
}
} }
// trim trailing slashes // trim trailing slashes