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