b6c903
commit b17d29b390154df9dfad9d21f1e6605422521fd2
b6c903
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
b6c903
Date:   Mon Jun 7 14:22:19 2021 +0530
b6c903
b6c903
    gconv_conf: Read configuration files in gconv-modules.d
b6c903
    
b6c903
    Read configuration files with names ending in .conf in
b6c903
    GCONV_PATH/gconv-modules.d to mirror configuration flexibility in
b6c903
    iconvconfig into the iconv program and function.
b6c903
    
b6c903
    Reviewed-by: DJ Delorie <dj@redhat.com>
b6c903
b6c903
diff --git a/iconv/gconv_conf.c b/iconv/gconv_conf.c
b6c903
index f173cde71b2a61d7..8eb981fca7cee36a 100644
b6c903
--- a/iconv/gconv_conf.c
b6c903
+++ b/iconv/gconv_conf.c
b6c903
@@ -19,6 +19,7 @@
b6c903
 
b6c903
 #include <assert.h>
b6c903
 #include <ctype.h>
b6c903
+#include <dirent.h>
b6c903
 #include <errno.h>
b6c903
 #include <limits.h>
b6c903
 #include <locale.h>
b6c903
@@ -30,6 +31,7 @@
b6c903
 #include <string.h>
b6c903
 #include <unistd.h>
b6c903
 #include <sys/param.h>
b6c903
+#include <sys/types.h>
b6c903
 
b6c903
 #include <libc-lock.h>
b6c903
 #include <gconv_int.h>
b6c903
@@ -50,6 +52,7 @@ static const struct path_elem empty_path_elem = { NULL, 0 };
b6c903
 /* Name of the file containing the module information in the directories
b6c903
    along the path.  */
b6c903
 static const char gconv_conf_filename[] = "gconv-modules";
b6c903
+static const char gconv_conf_dirname[] = "gconv-modules.d";
b6c903
 
b6c903
 /* Filename extension for the modules.  */
b6c903
 #ifndef MODULE_EXT
b6c903
@@ -554,18 +557,52 @@ __gconv_read_conf (void)
b6c903
 
b6c903
   for (cnt = 0; __gconv_path_elem[cnt].name != NULL; ++cnt)
b6c903
     {
b6c903
+#define BUF_LEN elem_len + sizeof (gconv_conf_dirname)
b6c903
+
b6c903
       const char *elem = __gconv_path_elem[cnt].name;
b6c903
       size_t elem_len = __gconv_path_elem[cnt].len;
b6c903
-      char *filename;
b6c903
+      char *buf;
b6c903
 
b6c903
       /* No slash needs to be inserted between elem and gconv_conf_filename;
b6c903
 	 elem already ends in a slash.  */
b6c903
-      filename = alloca (elem_len + sizeof (gconv_conf_filename));
b6c903
-      __mempcpy (__mempcpy (filename, elem, elem_len),
b6c903
-		 gconv_conf_filename, sizeof (gconv_conf_filename));
b6c903
+      buf = alloca (BUF_LEN);
b6c903
+      char *cp = __mempcpy (__mempcpy (buf, elem, elem_len),
b6c903
+			    gconv_conf_filename, sizeof (gconv_conf_filename));
b6c903
+
b6c903
+      /* Read the gconv-modules configuration file first.  */
b6c903
+      read_conf_file (buf, elem, elem_len, &modules, &nmodules);
b6c903
+
b6c903
+      /* Next, see if there is a gconv-modules.d directory containing
b6c903
+	 configuration files and if it is non-empty.  */
b6c903
+      cp--;
b6c903
+      cp[0] = '.';
b6c903
+      cp[1] = 'd';
b6c903
+      cp[2] = '\0';
b6c903
+
b6c903
+      DIR *confdir = __opendir (buf);
b6c903
+      if (confdir != NULL)
b6c903
+	{
b6c903
+	  struct dirent *ent;
b6c903
+	  while ((ent = __readdir (confdir)) != NULL)
b6c903
+	    {
b6c903
+	      if (ent->d_type != DT_REG)
b6c903
+		continue;
b6c903
+
b6c903
+	      size_t len = strlen (ent->d_name);
b6c903
+	      const char *suffix = ".conf";
b6c903
 
b6c903
-      /* Read the next configuration file.  */
b6c903
-      read_conf_file (filename, elem, elem_len, &modules, &nmodules);
b6c903
+	      if (len > strlen (suffix)
b6c903
+		  && strcmp (ent->d_name + len - strlen (suffix), suffix) == 0)
b6c903
+		{
b6c903
+		  /* LEN <= PATH_MAX so this alloca is not unbounded.  */
b6c903
+		  char *conf = alloca (BUF_LEN + len + 1);
b6c903
+		  cp = stpcpy (conf, buf);
b6c903
+		  sprintf (cp, "/%s", ent->d_name);
b6c903
+		  read_conf_file (conf, elem, elem_len, &modules, &nmodules);
b6c903
+		}
b6c903
+	    }
b6c903
+	  __closedir (confdir);
b6c903
+	}
b6c903
     }
b6c903
 #endif
b6c903