b6c903
commit 0c78b0bb78d87a7de18726a033d88904f158f0fe
b6c903
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
b6c903
Date:   Mon Jun 7 14:22:17 2021 +0530
b6c903
b6c903
    iconvconfig: Make file handling more general purpose
b6c903
    
b6c903
    Split out configuration file handling code from handle_dir into its
b6c903
    own function so that it can be reused for multiple configuration
b6c903
    files.
b6c903
    
b6c903
    Reviewed-by: DJ Delorie <dj@redhat.com>
b6c903
b6c903
diff --git a/iconv/iconvconfig.c b/iconv/iconvconfig.c
b6c903
index b6fef1553cbbdd3d..2b3c587bc77cfdcd 100644
b6c903
--- a/iconv/iconvconfig.c
b6c903
+++ b/iconv/iconvconfig.c
b6c903
@@ -644,37 +644,17 @@ add_module (char *rp, const char *directory)
b6c903
 	      cost, need_ext);
b6c903
 }
b6c903
 
b6c903
-
b6c903
-/* Read the config file and add the data for this directory to that.  */
b6c903
-static int
b6c903
-handle_dir (const char *dir)
b6c903
+/* Read a gconv-modules configuration file.  */
b6c903
+static bool
b6c903
+handle_file (const char *dir, const char *infile)
b6c903
 {
b6c903
-  char *cp;
b6c903
   FILE *fp;
b6c903
   char *line = NULL;
b6c903
   size_t linelen = 0;
b6c903
-  size_t dirlen = strlen (dir);
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
-    }
b6c903
-
b6c903
-  char infile[prefix_len + dirlen + sizeof "gconv-modules"];
b6c903
-  cp = infile;
b6c903
-  if (dir[0] == '/')
b6c903
-    cp = mempcpy (cp, prefix, prefix_len);
b6c903
-  strcpy (mempcpy (cp, dir, dirlen), "gconv-modules");
b6c903
 
b6c903
   fp = fopen (infile, "r");
b6c903
   if (fp == NULL)
b6c903
-    {
b6c903
-      error (0, errno, "cannot open `%s'", infile);
b6c903
-      return 1;
b6c903
-    }
b6c903
+    return false;
b6c903
 
b6c903
   /* No threads present.  */
b6c903
   __fsetlocking (fp, FSETLOCKING_BYCALLER);
b6c903
@@ -723,7 +703,42 @@ handle_dir (const char *dir)
b6c903
 
b6c903
   fclose (fp);
b6c903
 
b6c903
-  return 0;
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
+  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
+    }
b6c903
+
b6c903
+  char infile[prefix_len + dirlen + sizeof "gconv-modules"];
b6c903
+  cp = infile;
b6c903
+  if (dir[0] == '/')
b6c903
+    cp = mempcpy (cp, prefix, prefix_len);
b6c903
+  strcpy (mempcpy (cp, dir, dirlen), "gconv-modules");
b6c903
+
b6c903
+  found |= handle_file (dir, infile);
b6c903
+
b6c903
+  if (!found)
b6c903
+    {
b6c903
+      error (0, errno, "failed to open gconv configuration file in `%s'",
b6c903
+	     dir);
b6c903
+      error (0, 0,
b6c903
+	     "ensure that the directory contains a valid gconv-modules file.");
b6c903
+    }
b6c903
+
b6c903
+  return found ? 0 : 1;
b6c903
 }
b6c903
 
b6c903