diff options
Diffstat (limited to 'src/tool_homedir.c')
-rw-r--r-- | src/tool_homedir.c | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/src/tool_homedir.c b/src/tool_homedir.c index 719ff6a55..3529672d6 100644 --- a/src/tool_homedir.c +++ b/src/tool_homedir.c @@ -25,6 +25,13 @@ # include <pwd.h> #endif +#ifdef HAVE_SYS_STAT_H +#include <sys/stat.h> +#endif +#ifdef HAVE_FCNTL_H +#include <fcntl.h> +#endif + #include <curl/mprintf.h> #include "tool_homedir.h" @@ -45,7 +52,27 @@ static char *GetEnv(const char *variable) } /* return the home directory of the current user as an allocated string */ -char *homedir(void) + +/* + * The original logic found a home dir to use (by checking a range of + * environment variables and last using getpwuid) and returned that for the + * parent to use. + * + * With the XDG_CONFIG_HOME support (added much later than the other), this + * variable is treated differently in order to not ruin existing installations + * even if this environment variable is set. If this variable is set, and a + * file name is set to check, then only if that file name exists in that + * directory will it be returned as a "home directory". + * + * 1. use CURL_HOME if set + * 2. use XDG_CONFIG_HOME if set and fname is present + * 3. use HOME if set + * 4. Non-windows: use getpwuid + * 5. Windows: use APPDATA if set + * 6. Windows: use "USERPROFILE\Application Data" is set + */ + +char *homedir(const char *fname) { char *home; @@ -53,6 +80,22 @@ char *homedir(void) if(home) return home; + if(fname) { + home = GetEnv("XDG_CONFIG_HOME"); + if(home) { + char *c = curl_maprintf("%s" DIR_CHAR "%s", home, fname); + if(c) { + int fd = open(c, O_RDONLY); + curl_free(c); + if(fd >= 0) { + close(fd); + return home; + } + } + free(home); + } + } + home = GetEnv("HOME"); if(home) return home; |