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