From c51dd419f9d40facf516558510704891c0cf66b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Courr=C3=A8ges-Anglas?= Date: Tue, 4 Mar 2014 02:44:15 +0100 Subject: get_homedir(), checks HOME and the password database * ensures that the resulting home directory isn't the empty string (else returns NULL) --- src/main.c | 40 ++++++++++++++++++++++++++++------------ src/ratpoison.h | 1 + 2 files changed, 29 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 0593b64..a64d284 100644 --- a/src/main.c +++ b/src/main.c @@ -404,10 +404,35 @@ read_rc_file (FILE *file) free (line); } +const char * +get_homedir (void) +{ + char *homedir; + + homedir = getenv ("HOME"); + if (homedir != NULL && homedir[0] == '\0') + homedir = NULL; + +#if defined (HAVE_PWD_H) && defined (HAVE_GETPWUID) + if (homedir == NULL) + { + struct passwd *pw; + + pw = getpwuid (getuid ()); + if (pw != NULL) + homedir = pw->pw_dir; + + if (homedir != NULL && homedir[0] == '\0') + homedir = NULL; + } +#endif + + return homedir; +} + static int read_startup_files (char *alt_rcfile) { - char *homedir; FILE *fileptr = NULL; if (alt_rcfile) @@ -423,18 +448,9 @@ read_startup_files (char *alt_rcfile) { /* first check $HOME/.ratpoisonrc and if that does not exist then try $sysconfdir/ratpoisonrc */ + const char *homedir; - homedir = getenv ("HOME"); -#if defined (HAVE_PWD_H) && defined (HAVE_GETPWUID) - if (!homedir) - { - struct passwd *pw; - - pw = getpwuid (getuid ()); - if (pw) - homedir = pw->pw_dir; - } -#endif + homedir = get_homedir (); if (!homedir) PRINT_ERROR (("ratpoison: no home directory!?\n")); diff --git a/src/ratpoison.h b/src/ratpoison.h index 2c829ef..5ff7c44 100644 --- a/src/ratpoison.h +++ b/src/ratpoison.h @@ -85,6 +85,7 @@ void clean_up (void); rp_screen *find_screen (Window w); void set_close_on_exec (int fd); +const char *get_homedir (void); void read_rc_file (FILE *file); void fatal (const char *msg); -- cgit v1.2.3