6c0556
commit eeac390eecf7de24a110dc84e77e1190f42c5305
6c0556
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
6c0556
Date:   Thu Jun 10 14:31:57 2021 +0530
6c0556
6c0556
    iconvconfig: Use common gconv module parsing function
6c0556
    
6c0556
    Drop local copy of gconv file parsing and use the one in
6c0556
    gconv_parseconfdir.h instead.  Now there is a single implementation of
6c0556
    configuration file parsing.
6c0556
    
6c0556
    Reviewed-by: DJ Delorie <dj@redhat.com>
6c0556
6c0556
diff --git a/iconv/iconvconfig.c b/iconv/iconvconfig.c
6c0556
index 2f9d5f45ad3a8159..01ecf6f67d55dbbf 100644
6c0556
--- a/iconv/iconvconfig.c
6c0556
+++ b/iconv/iconvconfig.c
6c0556
@@ -18,7 +18,6 @@
6c0556
 
6c0556
 #include <argp.h>
6c0556
 #include <assert.h>
6c0556
-#include <dirent.h>
6c0556
 #include <error.h>
6c0556
 #include <errno.h>
6c0556
 #include <fcntl.h>
6c0556
@@ -34,10 +33,10 @@
6c0556
 #include <string.h>
6c0556
 #include <unistd.h>
6c0556
 #include <sys/cdefs.h>
6c0556
-#include <sys/types.h>
6c0556
 #include <sys/uio.h>
6c0556
 
6c0556
 #include "iconvconfig.h"
6c0556
+#include <gconv_parseconfdir.h>
6c0556
 
6c0556
 /* Get libc version number.  */
6c0556
 #include "../version.h"
6c0556
@@ -568,7 +567,9 @@ new_module (const char *fromname, size_t fromlen, const char *toname,
6c0556
 
6c0556
 /* Add new module.  */
6c0556
 static void
6c0556
-add_module (char *rp, const char *directory)
6c0556
+add_module (char *rp, const char *directory,
6c0556
+	    size_t dirlen __attribute__ ((__unused__)),
6c0556
+	    int modcount __attribute__ ((__unused__)))
6c0556
 {
6c0556
   /* We expect now
6c0556
      1. `from' name
6c0556
@@ -646,131 +647,28 @@ add_module (char *rp, const char *directory)
6c0556
 	      cost, need_ext);
6c0556
 }
6c0556
 
6c0556
-/* Read a gconv-modules configuration file.  */
6c0556
-static bool
6c0556
-handle_file (const char *dir, const char *infile)
6c0556
-{
6c0556
-  FILE *fp;
6c0556
-  char *line = NULL;
6c0556
-  size_t linelen = 0;
6c0556
-
6c0556
-  fp = fopen (infile, "r");
6c0556
-  if (fp == NULL)
6c0556
-    return false;
6c0556
-
6c0556
-  /* No threads present.  */
6c0556
-  __fsetlocking (fp, FSETLOCKING_BYCALLER);
6c0556
-
6c0556
-  while (!feof_unlocked (fp))
6c0556
-    {
6c0556
-      char *rp, *endp, *word;
6c0556
-      ssize_t n = __getdelim (&line, &linelen, '\n', fp);
6c0556
-
6c0556
-      if (n < 0)
6c0556
-	/* An error occurred.  */
6c0556
-	break;
6c0556
-
6c0556
-      rp = line;
6c0556
-      /* Terminate the line (excluding comments or newline) with a NUL
6c0556
-	 byte to simplify the following code.  */
6c0556
-      endp = strchr (rp, '#');
6c0556
-      if (endp != NULL)
6c0556
-	*endp = '\0';
6c0556
-      else
6c0556
-	if (rp[n - 1] == '\n')
6c0556
-	  rp[n - 1] = '\0';
6c0556
-
6c0556
-      while (isspace (*rp))
6c0556
-	++rp;
6c0556
-
6c0556
-      /* If this is an empty line go on with the next one.  */
6c0556
-      if (rp == endp)
6c0556
-	continue;
6c0556
-
6c0556
-      word = rp;
6c0556
-      while (*rp != '\0' && !isspace (*rp))
6c0556
-	++rp;
6c0556
-
6c0556
-      if (rp - word == sizeof ("alias") - 1
6c0556
-	  && memcmp (word, "alias", sizeof ("alias") - 1) == 0)
6c0556
-	add_alias (rp);
6c0556
-      else if (rp - word == sizeof ("module") - 1
6c0556
-	       && memcmp (word, "module", sizeof ("module") - 1) == 0)
6c0556
-	add_module (rp, dir);
6c0556
-      /* else */
6c0556
-	/* Otherwise ignore the line.  */
6c0556
-    }
6c0556
-
6c0556
-  free (line);
6c0556
-
6c0556
-  fclose (fp);
6c0556
-
6c0556
-  return true;
6c0556
-}
6c0556
-
6c0556
 /* Read config files and add the data for this directory to cache.  */
6c0556
 static int
6c0556
 handle_dir (const char *dir)
6c0556
 {
6c0556
-  char *cp;
6c0556
   size_t dirlen = strlen (dir);
6c0556
   bool found = false;
6c0556
 
6c0556
+  /* Add the prefix before sending it off to the parser.  */
6c0556
+  char *fulldir = xmalloc (prefix_len + dirlen + 2);
6c0556
+  char *cp = mempcpy (mempcpy (fulldir, prefix, prefix_len), dir, dirlen);
6c0556
+
6c0556
   if (dir[dirlen - 1] != '/')
6c0556
     {
6c0556
-      char *newp = (char *) xmalloc (dirlen + 2);
6c0556
-      dir = memcpy (newp, dir, dirlen);
6c0556
-      newp[dirlen++] = '/';
6c0556
-      newp[dirlen] = '\0';
6c0556
+      *cp++ = '/';
6c0556
+      *cp = '\0';
6c0556
+      dirlen++;
6c0556
     }
6c0556
 
6c0556
-  /* First, look for a gconv-modules file.  */
6c0556
-  char *buf = malloc (prefix_len + dirlen + sizeof "gconv-modules.d");
6c0556
-  if (buf == NULL)
6c0556
-    goto out;
6c0556
-
6c0556
-  cp = buf;
6c0556
-  if (dir[0] == '/')
6c0556
-    cp = mempcpy (cp, prefix, prefix_len);
6c0556
-  cp = mempcpy (cp, dir, dirlen);
6c0556
-  cp = stpcpy (cp, "gconv-modules");
6c0556
-
6c0556
-  found |= handle_file (dir, buf);
6c0556
-
6c0556
-  /* Next, see if there is a gconv-modules.d directory containing configuration
6c0556
-     files and if it is non-empty.  */
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
-	  if (len > strlen (suffix)
6c0556
-	      && strcmp (ent->d_name + len - strlen (suffix), suffix) == 0)
6c0556
-	    {
6c0556
-	      char *conf;
6c0556
-	      if (asprintf (&conf, "%s/%s", buf, ent->d_name) < 0)
6c0556
-		continue;
6c0556
-	      found |= handle_file (dir, conf);
6c0556
-	      free (conf);
6c0556
-	    }
6c0556
-	}
6c0556
-      closedir (confdir);
6c0556
-    }
6c0556
+  found = gconv_parseconfdir (fulldir, dirlen + prefix_len);
6c0556
 
6c0556
-  free (buf);
6c0556
+  free (fulldir);
6c0556
 
6c0556
-out:
6c0556
   if (!found)
6c0556
     {
6c0556
       error (0, errno, "failed to open gconv configuration files in `%s'",