To comply with XDG Base Directory Specification, look for Muttrc in
folders listed in XDG_CONFIG_DIRS.
---
 init.c        | 58 ++++++++++++++++++++++++++++++++++++++++++++++------------
 muttbug.sh.in |  3 ++-
 2 files changed, 48 insertions(+), 13 deletions(-)

diff --git a/init.c b/init.c
index 09999c8..d594c74 100644
--- a/init.c
+++ b/init.c
@@ -2939,6 +2939,30 @@ static char* mutt_find_cfg (const char *home, const char 
*xdg_cfg_home)
   return NULL;
 }
 
+static char* mutt_find_sys_cfg (char *path, const char **files)
+{
+  char *p;
+
+  while ((p = strsep(&path, ":")) != NULL)
+  {
+    int i;
+
+    /* case where ':' at the end of the string */
+    if (*p == '\0')
+      break;
+
+    for (i = 0; files[i]; i++)
+    {
+      char buffer[STRING];
+      snprintf (buffer, sizeof (buffer), "%s/%s", p, files[i]);
+      if (access (buffer, F_OK) == 0)
+        return safe_strdup(buffer);
+    }
+  }
+
+  return NULL;
+}
+
 void mutt_init (int skip_sys_rc, LIST *commands)
 {
   struct passwd *pw;
@@ -3198,21 +3222,31 @@ void mutt_init (int skip_sys_rc, LIST *commands)
      requested not to via "-n".  */
   if (!skip_sys_rc)
   {
-    snprintf (buffer, sizeof(buffer), "%s/Muttrc-%s", SYSCONFDIR, 
MUTT_VERSION);
-    if (access (buffer, F_OK) == -1)
-      snprintf (buffer, sizeof(buffer), "%s/Muttrc", SYSCONFDIR);
-    if (access (buffer, F_OK) == -1)
-      snprintf (buffer, sizeof (buffer), "%s/Muttrc-%s", PKGDATADIR, 
MUTT_VERSION);
-    if (access (buffer, F_OK) == -1)
-      snprintf (buffer, sizeof (buffer), "%s/Muttrc", PKGDATADIR);
-    if (access (buffer, F_OK) != -1)
+    const char *files[] =
     {
-      if (source_rc (buffer, &err) != 0)
+      "Muttrc-" MUTT_VERSION,
+      "Muttrc",
+      NULL,
+    };
+    char *xdg_cfg_dirs = getenv ("XDG_CONFIG_DIRS");
+    char *sys_rc;
+
+    if (!xdg_cfg_dirs)
+      xdg_cfg_dirs = "/etc/xdg";
+
+    snprintf (buffer, sizeof(buffer), SYSCONFDIR ":" PKGDATADIR ":%s",
+              xdg_cfg_dirs);
+    sys_rc = mutt_find_sys_cfg (buffer, files);
+    if (sys_rc)
+    {
+      if (source_rc (sys_rc, &err) != 0)
       {
-       fputs (err.data, stderr);
-       fputc ('\n', stderr);
-       need_pause = 1;
+        fputs (err.data, stderr);
+        fputc ('\n', stderr);
+        need_pause = 1;
       }
+
+      FREE (&sys_rc);
     }
   }
 
diff --git a/muttbug.sh.in b/muttbug.sh.in
index 53d9de4..0695e04 100644
--- a/muttbug.sh.in
+++ b/muttbug.sh.in
@@ -291,8 +291,9 @@ fi
 
 
 if test "$global" = "yes" ; then
+       xdg_cfg_dirs=$( echo ${XDG_CONFIG_DIRS:-/etc/xdg} | tr ':' ' ' )
        CANDIDATES="Muttrc-${MUTTVERSION} Muttrc"
-       DIRECTORIES="$sysconfdir $pkgdatadir"
+       DIRECTORIES="$sysconfdir $pkgdatadir $xdg_cfg_dirs"
        MATCHED="none"
        for d in $DIRECTORIES ; do
                for f in $CANDIDATES; do
-- 
2.10.2

Reply via email to