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

Save the settings file under XDG_CONFIG_HOME by default #107

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
69 changes: 68 additions & 1 deletion src/command-line.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses>.
*/

#include <errno.h>
#include <stdlib.h>
#include <pwd.h>
#include <sys/types.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/stat.h>

#include "option-table.h"
#include "query-assign.h"
Expand Down Expand Up @@ -199,6 +202,70 @@ void print_help(void)
nvgetopt_print_help(__options, 0, print_help_helper);
}

/*
* locate_default_rc_file() - find a suitable location for the default
* configuration file. This will be one of:
* - $XDG_CONFIG_HOME/nvidia/settings-rc,
* - $HOME/.config/nvidia/settings-rc or
* - $HOME/.nvidia-settings-rc
* The last of which is chosen only if the file already exists, for
* backwards compatibility.
*
* The parent directory, `nvidia` for the first two options will be
* created if it doesn't already exist.
*
* The string returned is malloc'ed, but must not be freed as it is
* re-used if this is called multiple times.
*/
const char *locate_default_rc_file(void)
{
static char *default_rc_file = NULL;
const char *home;
const char *xdg_config_home;

if (default_rc_file) {
return default_rc_file;
}

home = get_user_home();
xdg_config_home = getenv("XDG_CONFIG_HOME");

/* Prefer the legacy dot-file in $HOME if it exists. */

if (home) {
nv_append_sprintf(&default_rc_file, "%s/.nvidia-settings-rc", home);

if (access(default_rc_file, F_OK) == 0) {
return default_rc_file;
}

nvfree(default_rc_file);
default_rc_file = NULL;
}

if (xdg_config_home) {
nv_append_sprintf(&default_rc_file, "%s/nvidia", xdg_config_home);
} else if (home) {
nv_append_sprintf(&default_rc_file, "%s/.config/nvidia", home);
} else {
/* Store in the current directory as the last resort if we cannot find a home. */

default_rc_file = ".nvidia-settings-rc";
return default_rc_file;
}

if (access(default_rc_file, F_OK) != 0) {
if (mkdir(default_rc_file, 0755) < 0) {
fprintf(stderr,
"Failed to create the default configuration directory '%s': %s.\n",
default_rc_file, strerror(errno));
}
}

nv_append_sprintf(&default_rc_file, "/settings-rc");

return default_rc_file;
}

/*
* parse_command_line() - malloc an Options structure, initialize it
Expand All @@ -218,7 +285,7 @@ Options *parse_command_line(int argc, char *argv[],
int boolval;

op = nvalloc(sizeof(Options));
op->config = DEFAULT_RC_FILE;
op->config = locate_default_rc_file();
op->write_config = NV_TRUE;

/*
Expand Down
7 changes: 3 additions & 4 deletions src/command-line.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
*/
struct _CtrlSystemList;

#define DEFAULT_RC_FILE "~/.nvidia-settings-rc"
#define CONFIG_FILE_OPTION 1
#define DISPLAY_OPTION 2

Expand All @@ -47,8 +46,8 @@ typedef struct {
char *config; /*
* The name of the configuration file (to be
* read from, and to be written to); defaults
* to the value of the constant
* DEFAULT_RC_FILE.
* to $XDG_CONFIG_HOME/nvidia/settings-rc or
* ~/.nvidia-settings-rc if the latter exists.
*/

char **assignments; /*
Expand Down Expand Up @@ -124,7 +123,7 @@ typedef struct {

} Options;


const char *locate_default_rc_file(void);
Options *parse_command_line(int argc, char *argv[],
struct _CtrlSystemList *systems);

Expand Down
34 changes: 25 additions & 9 deletions src/common-utils/common-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,29 @@ void nvfree(void *s)
/* misc */
/****************************************************************************/

/*
* get_user_home() - locate current user's home directory using either
* the enrivonment variable or their passwd entry.
*
* Returns NULL if the home directory cannot be located, otherwise
* returns a statically allocated string that is either the value
* of the environment variable or the passwd entry.
*/
const char *get_user_home()
{
char *home = getenv("HOME");
struct passwd *pw;

if (home) return home;

/* $HOME isn't set; get the home directory from /etc/passwd */

pw = getpwuid(getuid());
if (pw) return pw->pw_dir;

return NULL;
}

/*
* tilde_expansion() - do tilde expansion on the given path name;
* based loosely on code snippets found in the comp.unix.programmer
Expand All @@ -310,7 +333,7 @@ void nvfree(void *s)

char *tilde_expansion(const char *str)
{
char *prefix = NULL;
const char *prefix = NULL;
const char *replace;
char *user, *ret;
struct passwd *pw;
Expand All @@ -324,14 +347,7 @@ char *tilde_expansion(const char *str)

/* expand to the current user's home directory */

prefix = getenv("HOME");
if (!prefix) {

/* $HOME isn't set; get the home directory from /etc/passwd */

pw = getpwuid(getuid());
if (pw) prefix = pw->pw_dir;
}
prefix = get_user_home();

replace = str + 1;

Expand Down
1 change: 1 addition & 0 deletions src/common-utils/common-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ char *nvasprintf(const char *fmt, ...) NV_ATTRIBUTE_PRINTF(1, 2);
void nv_append_sprintf(char **buf, const char *fmt, ...) NV_ATTRIBUTE_PRINTF(2, 3);
void nvfree(void *s);

const char *get_user_home();
char *tilde_expansion(const char *str);
char *nv_prepend_to_string_list(char *list, const char *item, const char *delim);

Expand Down
5 changes: 4 additions & 1 deletion src/gtk+-2.x/ctkconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/

#include "ctkconfig.h"
#include "command-line.h"
#include "ctkhelp.h"
#include "ctkwindow.h"
#include "ctkutils.h"
Expand Down Expand Up @@ -305,12 +306,14 @@ static void save_rc_clicked(GtkWidget *widget, gpointer user_data)
CtkWindow *ctk_window =
CTK_WINDOW(ctk_get_parent_window(GTK_WIDGET(ctk_config)));

char *default_rc_file = locate_default_rc_file();

filename =
ctk_get_filename_from_dialog("Please select a file to save to.",
GTK_WINDOW(ctk_window),
ctk_config->rc_filename ?
ctk_config->rc_filename :
DEFAULT_RC_FILE);
default_rc_file);

if (!filename) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/option-table.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static const NVGetoptOption __options[] = {
{ "config", CONFIG_FILE_OPTION,
NVGETOPT_STRING_ARGUMENT | NVGETOPT_HELP_ALWAYS, NULL,
"Use the configuration file &CONFIG& rather than the "
"default &" DEFAULT_RC_FILE "&" },
"default &$XDG_CONFIG_HOME/nvidia/settings-rc&" },

{ "ctrl-display", 'c',
NVGETOPT_STRING_ARGUMENT | NVGETOPT_HELP_ALWAYS, NULL,
Expand Down