Now, the following locations are searched, and the first existing file
is used as user config file:

- $XDG_CONFIG_HOME/wget/config
- ~/.config/wget/config
- ~/.wgetrc

This is a backward-compatible change, and care have been taked to preserve
fuzzing-related logic: when fuzzing expansion of "~/.wgetrc" is returned even
if it doesn't exist.

  * src/init.c (wgetrc_user_file_name): Search for the config file in
    locations prescribed by XDG standard.
---
 doc/wget.texi |  6 ++++--
 src/init.c    | 29 +++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/doc/wget.texi b/doc/wget.texi
index ab4ff1dc..04df4591 100644
--- a/doc/wget.texi
+++ b/doc/wget.texi
@@ -207,7 +207,7 @@ If both --config and --no-config are given, --no-config is 
ignored.
 @item /usr/local/etc/wgetrc
 Default location of the @dfn{global} startup file.
 
-@item .wgetrc
+@item ${XDG_CONFIG_HOME:-~/.config}/wget/config, ~/.wgetrc
 User startup file.
 @end table
 @c man end
@@ -3196,7 +3196,9 @@ Then it will look for the user's file.  If the 
environmental variable
 @code{WGETRC} is set, Wget will try to load that file.  Failing that, no
 further attempts will be made.
 
-If @code{WGETRC} is not set, Wget will try to load @file{$HOME/.wgetrc}.
+If @code{WGETRC} is not set, Wget will load first existing of
+@file{$XDG_CONFIG_HOME:-~/.config}/, @file{$HOME/.config/wget/config}
+and @file{$HOME/.wgetrc}, if any.
 
 The fact that user's settings are loaded after the system-wide ones
 means that in case of collision user's wgetrc @emph{overrides} the
diff --git a/src/init.c b/src/init.c
index 1c452836..f2835f82 100644
--- a/src/init.c
+++ b/src/init.c
@@ -611,6 +611,35 @@ char *
 wgetrc_user_file_name (void)
 {
   char *file = NULL;
+  const char *base = NULL;
+  const char *suffix = NULL;
+
+  base = getenv("XDG_CONFIG_HOME");
+  suffix = "wget/config";
+
+  // Two conditions, since XDG standard uses "is either not set or empty" 
wording.
+  // https://specifications.freedesktop.org/basedir-spec/0.8/#variables
+  if (!(base && *base))
+    {
+      base = opt.homedir;
+      suffix = ".config/wget/config";
+    }
+
+  if (base)
+    {
+      file = ajoin_dir_file (base, suffix);
+      if (!file)
+        return NULL;
+
+      if (file_exists_p (file, NULL))
+        {
+          return file;
+        }
+      else
+        {
+          xfree (file);
+        }
+    }
 
   /* Join opt.homedir ($HOME) and ".wgetrc" */
   if (opt.homedir) {
-- 
2.47.0


Reply via email to