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