summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2020-08-21 23:40:12 +0200
committerDaniel Stenberg <daniel@haxx.se>2020-08-24 16:37:09 +0200
commit4be1f8dc01013e4dee2b99026cd3b806ea7253c4 (patch)
tree230595d13f11659f57605a33ccba2c1375f7c079 /src
parent98c94596f5928840177b6bd3c7b0f0dd03a431af (diff)
downloadcurl-4be1f8dc01013e4dee2b99026cd3b806ea7253c4.tar.gz
curl: support XDG_CONFIG_HOME to find .curlrc
Added test433 to verify. Updated documentation. Reviewed-by: Jay Satiro Suggested-by: Eli Schwartz Fixes #5829 Closes #5837
Diffstat (limited to 'src')
-rw-r--r--src/tool_homedir.c45
-rw-r--r--src/tool_homedir.h4
-rw-r--r--src/tool_operate.c2
-rw-r--r--src/tool_parsecfg.c2
4 files changed, 48 insertions, 5 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;
diff --git a/src/tool_homedir.h b/src/tool_homedir.h
index 1f9d54a63..66defc200 100644
--- a/src/tool_homedir.h
+++ b/src/tool_homedir.h
@@ -7,7 +7,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@@ -23,6 +23,6 @@
***************************************************************************/
#include "tool_setup.h"
-char *homedir(void);
+char *homedir(const char *fname);
#endif /* HEADER_CURL_TOOL_HOMEDIR_H */
diff --git a/src/tool_operate.c b/src/tool_operate.c
index 3936315a9..1e4ed7df8 100644
--- a/src/tool_operate.c
+++ b/src/tool_operate.c
@@ -1696,7 +1696,7 @@ static CURLcode single_transfer(struct GlobalConfig *global,
char *home;
char *file;
result = CURLE_FAILED_INIT;
- home = homedir();
+ home = homedir(NULL);
if(home) {
file = aprintf("%s/.ssh/known_hosts", home);
if(file) {
diff --git a/src/tool_parsecfg.c b/src/tool_parsecfg.c
index 5aeb2eebf..4e56492cb 100644
--- a/src/tool_parsecfg.c
+++ b/src/tool_parsecfg.c
@@ -82,7 +82,7 @@ int parseconfig(const char *filename, struct GlobalConfig *global)
if(!filename || !*filename) {
/* NULL or no file name attempts to load .curlrc from the homedir! */
- char *home = homedir(); /* portable homedir finder */
+ char *home = homedir(".curlrc");
#ifndef WIN32
if(home) {
pathalloc = curl_maprintf("%s%s.curlrc", home, DIR_CHAR);