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