ce426f
The functions in locarchive.c cannot create files in non-default
ce426f
locations.  Rather than enhancing locarchive.c (and risk regressions
ce426f
in localedef), this patch adapts build-locale-archive to use the
ce426f
existing output_prefix configuration variable.
ce426f
ce426f
Index: glibc-2.17-c758a686/releng/build-locale-archive.c
ce426f
===================================================================
ce426f
--- glibc-2.17-c758a686.orig/releng/build-locale-archive.c
ce426f
+++ glibc-2.17-c758a686/releng/build-locale-archive.c
ce426f
@@ -92,23 +92,34 @@ xmalloc (size_t size)
ce426f
   return p;
ce426f
 }
ce426f
 
ce426f
+static char *
ce426f
+xasprintf (const char *format, ...)
ce426f
+{
ce426f
+  va_list ap;
ce426f
+  va_start (ap, format);
ce426f
+  char *result;
ce426f
+  if (vasprintf (&result, format, ap) < 0)
ce426f
+    error (EXIT_FAILURE, errno, "could not format string");
ce426f
+  va_end (ap);
ce426f
+  return result;
ce426f
+}
ce426f
+
ce426f
 static void
ce426f
 open_tmpl_archive (struct locarhandle *ah)
ce426f
 {
ce426f
   struct stat64 st;
ce426f
   int fd;
ce426f
   struct locarhead head;
ce426f
-  const char *archivefname = ah->fname == NULL ? tmpl_file : ah->fname;
ce426f
 
ce426f
   /* Open the archive.  We must have exclusive write access.  */
ce426f
-  fd = open64 (archivefname, O_RDONLY);
ce426f
+  fd = open64 (tmpl_file, O_RDONLY);
ce426f
   if (fd == -1)
ce426f
     error (EXIT_FAILURE, errno, "cannot open locale archive template file \"%s\"",
ce426f
-	   archivefname);
ce426f
+	   tmpl_file);
ce426f
 
ce426f
   if (fstat64 (fd, &st) < 0)
ce426f
     error (EXIT_FAILURE, errno, "cannot stat locale archive template file \"%s\"",
ce426f
-	   archivefname);
ce426f
+	   tmpl_file);
ce426f
 
ce426f
   /* Read the header.  */
ce426f
   if (TEMP_FAILURE_RETRY (read (fd, &head, sizeof (head))) != sizeof (head))
ce426f
@@ -253,12 +264,11 @@ compute_data (struct locarhandle *ah, st
ce426f
 
ce426f
 static int
ce426f
 fill_archive (struct locarhandle *tmpl_ah,
ce426f
-	      const char *fname,
ce426f
 	      size_t install_langs_count, char *install_langs_list[],
ce426f
 	      size_t nlist, char *list[],
ce426f
 	      const char *primary)
ce426f
 {
ce426f
-  struct locarhandle ah;
ce426f
+  struct locarhandle ah = {};
ce426f
   struct locarhead *head;
ce426f
   int result = 0;
ce426f
   struct nameent *names;
ce426f
@@ -338,9 +348,6 @@ fill_archive (struct locarhandle *tmpl_a
ce426f
 
ce426f
   /* Open the archive.  This call never returns if we cannot
ce426f
      successfully open the archive.  */
ce426f
-  ah.fname = NULL;
ce426f
-  if (fname != NULL)
ce426f
-    ah.fname = fname;
ce426f
   open_archive (&ah, false);
ce426f
 
ce426f
   if (primary != NULL)
ce426f
@@ -619,38 +626,39 @@ Usage: build-locale-archive [OPTION]...
ce426f
                              \"de\" but also the aliases \"deutsch\"\n\
ce426f
                              and and \"german\" although the latter does not\n\
ce426f
                              start with \"de\".\n\
ce426f
+  --prefix=DIR               Operate under the file system root DIR\n\
ce426f
 \n\
ce426f
-  If the arguments TEMPLATE-FILE and ARCHIVE-FILE are not given the locations\n\
ce426f
-  where the glibc used expects these files are used by default.\n\
ce426f
 ");
ce426f
 }
ce426f
 
ce426f
 int main (int argc, char *argv[])
ce426f
 {
ce426f
-  char path[4096];
ce426f
   DIR *dirp;
ce426f
   struct dirent64 *d;
ce426f
   struct stat64 st;
ce426f
   char *list[16384], *primary;
ce426f
   char *lang;
ce426f
   int install_langs_count = 0;
ce426f
-  int i;
ce426f
   char *install_langs_arg, *ila_start;
ce426f
   char **install_langs_list;
ce426f
   unsigned int cnt = 0;
ce426f
-  struct locarhandle tmpl_ah;
ce426f
-  char *new_locar_fname = NULL;
ce426f
-  size_t loc_path_len = strlen (loc_path);
ce426f
+  struct locarhandle tmpl_ah = {};
ce426f
 
ce426f
   while (1)
ce426f
     {
ce426f
       int c;
ce426f
 
ce426f
+      enum
ce426f
+      {
ce426f
+	opt_prefix = 1000,
ce426f
+      };
ce426f
+
ce426f
       static struct option long_options[] =
ce426f
         {
ce426f
             {"help",            no_argument,       0, 'h'},
ce426f
             {"verbose",         no_argument,       0, 'v'},
ce426f
             {"install-langs",   required_argument, 0, 'l'},
ce426f
+	    {"prefix",          required_argument, 0, opt_prefix},
ce426f
             {0, 0, 0, 0}
ce426f
         };
ce426f
       /* getopt_long stores the option index here. */
ce426f
@@ -721,6 +729,10 @@ int main (int argc, char *argv[])
ce426f
             }
ce426f
           break;
ce426f
 
ce426f
+	case opt_prefix:
ce426f
+	  output_prefix = optarg;
ce426f
+	  break;
ce426f
+
ce426f
         case '?':
ce426f
           /* getopt_long already printed an error message. */
ce426f
           usage ();
ce426f
@@ -730,23 +742,26 @@ int main (int argc, char *argv[])
ce426f
           abort ();
ce426f
         }
ce426f
     }
ce426f
-  tmpl_ah.fname = NULL;
ce426f
-  if (optind < argc)
ce426f
-    tmpl_ah.fname = argv[optind];
ce426f
-  if (optind + 1 < argc)
ce426f
-    new_locar_fname = argv[optind + 1];
ce426f
+  if (optind != argc)
ce426f
+    {
ce426f
+      usage ();
ce426f
+      exit (1);
ce426f
+    }
ce426f
+  if (output_prefix)
ce426f
+    {
ce426f
+      tmpl_file = xasprintf ("%s%s", output_prefix, tmpl_file);
ce426f
+      alias_file = xasprintf ("%s%s", output_prefix, alias_file);
ce426f
+      locar_file = xasprintf ("%s%s", output_prefix, locar_file);
ce426f
+      loc_path = xasprintf ("%s%s", output_prefix, loc_path);
ce426f
+    }
ce426f
   if (verbose)
ce426f
     {
ce426f
-      if (tmpl_ah.fname)
ce426f
-        printf("input archive file specified on command line: %s\n",
ce426f
-               tmpl_ah.fname);
ce426f
-      else
ce426f
-        printf("using default input archive file.\n");
ce426f
-      if (new_locar_fname)
ce426f
-        printf("output archive file specified on command line: %s\n",
ce426f
-               new_locar_fname);
ce426f
-      else
ce426f
-        printf("using default output archive file.\n");
ce426f
+      if (output_prefix != NULL)
ce426f
+	printf ("output prefix: %s\n", output_prefix);
ce426f
+      printf ("input archive file: %s\n", tmpl_file);
ce426f
+      printf ("input alias file: %s\n", alias_file);
ce426f
+      printf ("input locale directory prefix: %s\n", loc_path);
ce426f
+      printf ("output archive file: %s\n", locar_file);
ce426f
     }
ce426f
 
ce426f
   dirp = opendir (loc_path);
ce426f
@@ -754,11 +769,7 @@ int main (int argc, char *argv[])
ce426f
     error (EXIT_FAILURE, errno, "cannot open directory \"%s\"", loc_path);
ce426f
 
ce426f
   open_tmpl_archive (&tmpl_ah);
ce426f
-
ce426f
-  if (new_locar_fname)
ce426f
-    unlink (new_locar_fname);
ce426f
-  else
ce426f
-    unlink (locar_file);
ce426f
+  unlink (locar_file);
ce426f
 
ce426f
   primary = getenv ("LC_ALL");
ce426f
   if (primary == NULL)
ce426f
@@ -790,8 +801,6 @@ int main (int argc, char *argv[])
ce426f
 	}
ce426f
     }
ce426f
 
ce426f
-  memcpy (path, loc_path, loc_path_len);
ce426f
-
ce426f
   while ((d = readdir64 (dirp)) != NULL)
ce426f
     {
ce426f
       if (strcmp (d->d_name, ".") == 0 || strcmp (d->d_name, "..") == 0)
ce426f
@@ -799,32 +808,25 @@ int main (int argc, char *argv[])
ce426f
       if (strchr (d->d_name, '_') == NULL)
ce426f
 	continue;
ce426f
 
ce426f
-      size_t d_name_len = strlen (d->d_name);
ce426f
-      if (loc_path_len + d_name_len + 1 > sizeof (path))
ce426f
-	{
ce426f
-	  error (0, 0, "too long filename \"%s\"", d->d_name);
ce426f
-	  continue;
ce426f
-	}
ce426f
-
ce426f
-      memcpy (path + loc_path_len, d->d_name, d_name_len + 1);
ce426f
+      char *path = xasprintf ("%s%s", loc_path, d->d_name);
ce426f
       if (stat64 (path, &st) < 0)
ce426f
 	{
ce426f
 	  error (0, errno, "cannot stat \"%s\"", path);
ce426f
+	  free (path);
ce426f
 	  continue;
ce426f
 	}
ce426f
       if (! S_ISDIR (st.st_mode))
ce426f
-	continue;
ce426f
+	{
ce426f
+	  free (path);
ce426f
+	  continue;
ce426f
+	}
ce426f
       if (cnt == 16384)
ce426f
 	{
ce426f
 	  error (0, 0, "too many directories in \"%s\"", loc_path);
ce426f
+	  free (path);
ce426f
 	  break;
ce426f
 	}
ce426f
-      list[cnt] = strdup (path);
ce426f
-      if (list[cnt] == NULL)
ce426f
-	{
ce426f
-	  error (0, errno, "cannot add file to list \"%s\"", path);
ce426f
-	  continue;
ce426f
-	}
ce426f
+      list[cnt] = path;
ce426f
       if (primary != NULL && cnt > 0 && strcmp (primary, d->d_name) == 0)
ce426f
 	{
ce426f
 	  char *p = list[0];
ce426f
@@ -836,7 +838,7 @@ int main (int argc, char *argv[])
ce426f
   closedir (dirp);
ce426f
   /* Store the archive to the file specified as the second argument on the
ce426f
      command line or the default locale archive.  */
ce426f
-  fill_archive (&tmpl_ah, new_locar_fname,
ce426f
+  fill_archive (&tmpl_ah,
ce426f
                 install_langs_count, install_langs_list,
ce426f
                 cnt, list, primary);
ce426f
   close_archive (&tmpl_ah);