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