Blame iconv/gconv_conf.c

Packit 6c4009
/* Handle configuration data.
Packit 6c4009
   Copyright (C) 1997-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <ctype.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <limits.h>
Packit 6c4009
#include <locale.h>
Packit 6c4009
#include <search.h>
Packit 6c4009
#include <stddef.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdio_ext.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/param.h>
Packit 6c4009
Packit 6c4009
#include <libc-lock.h>
Packit 6c4009
#include <gconv_int.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* This is the default path where we look for module lists.  */
Packit 6c4009
static const char default_gconv_path[] = GCONV_PATH;
Packit 6c4009
Packit 6c4009
/* The path elements, as determined by the __gconv_get_path function.
Packit 6c4009
   All path elements end in a slash.  */
Packit 6c4009
struct path_elem *__gconv_path_elem;
Packit 6c4009
/* Maximum length of a single path element in __gconv_path_elem.  */
Packit 6c4009
size_t __gconv_max_path_elem_len;
Packit 6c4009
Packit 6c4009
/* We use the following struct if we couldn't allocate memory.  */
Packit 6c4009
static const struct path_elem empty_path_elem = { NULL, 0 };
Packit 6c4009
Packit 6c4009
/* Name of the file containing the module information in the directories
Packit 6c4009
   along the path.  */
Packit 6c4009
static const char gconv_conf_filename[] = "gconv-modules";
Packit 6c4009
Packit 6c4009
/* Filename extension for the modules.  */
Packit 6c4009
#ifndef MODULE_EXT
Packit 6c4009
# define MODULE_EXT ".so"
Packit 6c4009
#endif
Packit 6c4009
static const char gconv_module_ext[] = MODULE_EXT;
Packit 6c4009
Packit 6c4009
/* We have a few builtin transformations.  */
Packit 6c4009
static struct gconv_module builtin_modules[] =
Packit 6c4009
{
Packit 6c4009
#define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \
Packit 6c4009
			       MinF, MaxF, MinT, MaxT) \
Packit 6c4009
  {									      \
Packit 6c4009
    .from_string = From,						      \
Packit 6c4009
    .to_string = To,							      \
Packit 6c4009
    .cost_hi = Cost,							      \
Packit 6c4009
    .cost_lo = INT_MAX,							      \
Packit 6c4009
    .module_name = Name							      \
Packit 6c4009
  },
Packit 6c4009
#define BUILTIN_ALIAS(From, To)
Packit 6c4009
Packit 6c4009
#include "gconv_builtin.h"
Packit 6c4009
Packit 6c4009
#undef BUILTIN_TRANSFORMATION
Packit 6c4009
#undef BUILTIN_ALIAS
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static const char builtin_aliases[] =
Packit 6c4009
{
Packit 6c4009
#define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \
Packit 6c4009
			       MinF, MaxF, MinT, MaxT)
Packit 6c4009
#define BUILTIN_ALIAS(From, To) From "\0" To "\0"
Packit 6c4009
Packit 6c4009
#include "gconv_builtin.h"
Packit 6c4009
Packit 6c4009
#undef BUILTIN_TRANSFORMATION
Packit 6c4009
#undef BUILTIN_ALIAS
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
#include <libio/libioP.h>
Packit 6c4009
#define __getdelim(line, len, c, fp) _IO_getdelim (line, len, c, fp)
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Value of the GCONV_PATH environment variable.  */
Packit 6c4009
const char *__gconv_path_envvar;
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Test whether there is already a matching module known.  */
Packit 6c4009
static int
Packit 6c4009
detect_conflict (const char *alias)
Packit 6c4009
{
Packit 6c4009
  struct gconv_module *node = __gconv_modules_db;
Packit 6c4009
Packit 6c4009
  while (node != NULL)
Packit 6c4009
    {
Packit 6c4009
      int cmpres = strcmp (alias, node->from_string);
Packit 6c4009
Packit 6c4009
      if (cmpres == 0)
Packit 6c4009
	/* We have a conflict.  */
Packit 6c4009
	return 1;
Packit 6c4009
      else if (cmpres < 0)
Packit 6c4009
	node = node->left;
Packit 6c4009
      else
Packit 6c4009
	node = node->right;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return node != NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* The actual code to add aliases.  */
Packit 6c4009
static void
Packit 6c4009
add_alias2 (const char *from, const char *to, const char *wp, void *modules)
Packit 6c4009
{
Packit 6c4009
  /* Test whether this alias conflicts with any available module.  */
Packit 6c4009
  if (detect_conflict (from))
Packit 6c4009
    /* It does conflict, don't add the alias.  */
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
  struct gconv_alias *new_alias = (struct gconv_alias *)
Packit 6c4009
    malloc (sizeof (struct gconv_alias) + (wp - from));
Packit 6c4009
  if (new_alias != NULL)
Packit 6c4009
    {
Packit 6c4009
      void **inserted;
Packit 6c4009
Packit 6c4009
      new_alias->fromname = memcpy ((char *) new_alias
Packit 6c4009
				    + sizeof (struct gconv_alias),
Packit 6c4009
				    from, wp - from);
Packit 6c4009
      new_alias->toname = new_alias->fromname + (to - from);
Packit 6c4009
Packit 6c4009
      inserted = (void **) __tsearch (new_alias, &__gconv_alias_db,
Packit 6c4009
				      __gconv_alias_compare);
Packit 6c4009
      if (inserted == NULL || *inserted != new_alias)
Packit 6c4009
	/* Something went wrong, free this entry.  */
Packit 6c4009
	free (new_alias);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Add new alias.  */
Packit 6c4009
static void
Packit 6c4009
add_alias (char *rp, void *modules)
Packit 6c4009
{
Packit 6c4009
  /* We now expect two more string.  The strings are normalized
Packit 6c4009
     (converted to UPPER case) and strored in the alias database.  */
Packit 6c4009
  char *from, *to, *wp;
Packit 6c4009
Packit 6c4009
  while (__isspace_l (*rp, _nl_C_locobj_ptr))
Packit 6c4009
    ++rp;
Packit 6c4009
  from = wp = rp;
Packit 6c4009
  while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
Packit 6c4009
    *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr);
Packit 6c4009
  if (*rp == '\0')
Packit 6c4009
    /* There is no `to' string on the line.  Ignore it.  */
Packit 6c4009
    return;
Packit 6c4009
  *wp++ = '\0';
Packit 6c4009
  to = ++rp;
Packit 6c4009
  while (__isspace_l (*rp, _nl_C_locobj_ptr))
Packit 6c4009
    ++rp;
Packit 6c4009
  while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
Packit 6c4009
    *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr);
Packit 6c4009
  if (to == wp)
Packit 6c4009
    /* No `to' string, ignore the line.  */
Packit 6c4009
    return;
Packit 6c4009
  *wp++ = '\0';
Packit 6c4009
Packit 6c4009
  add_alias2 (from, to, wp, modules);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Insert a data structure for a new module in the search tree.  */
Packit 6c4009
static void
Packit 6c4009
insert_module (struct gconv_module *newp, int tobefreed)
Packit 6c4009
{
Packit 6c4009
  struct gconv_module **rootp = &__gconv_modules_db;
Packit 6c4009
Packit 6c4009
  while (*rootp != NULL)
Packit 6c4009
    {
Packit 6c4009
      struct gconv_module *root = *rootp;
Packit 6c4009
      int cmpres;
Packit 6c4009
Packit 6c4009
      cmpres = strcmp (newp->from_string, root->from_string);
Packit 6c4009
      if (cmpres == 0)
Packit 6c4009
	{
Packit 6c4009
	  /* Both strings are identical.  Insert the string at the
Packit 6c4009
	     end of the `same' list if it is not already there.  */
Packit 6c4009
	  while (strcmp (newp->from_string, root->from_string) != 0
Packit 6c4009
		 || strcmp (newp->to_string, root->to_string) != 0)
Packit 6c4009
	    {
Packit 6c4009
	      rootp = &root->same;
Packit 6c4009
	      root = *rootp;
Packit 6c4009
	      if (root == NULL)
Packit 6c4009
		break;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  if (root != NULL)
Packit 6c4009
	    {
Packit 6c4009
	      /* This is a no new conversion.  But maybe the cost is
Packit 6c4009
		 better.  */
Packit 6c4009
	      if (newp->cost_hi < root->cost_hi
Packit 6c4009
		  || (newp->cost_hi == root->cost_hi
Packit 6c4009
		      && newp->cost_lo < root->cost_lo))
Packit 6c4009
		{
Packit 6c4009
		  newp->left = root->left;
Packit 6c4009
		  newp->right = root->right;
Packit 6c4009
		  newp->same = root->same;
Packit 6c4009
		  *rootp = newp;
Packit 6c4009
Packit 6c4009
		  free (root);
Packit 6c4009
		}
Packit 6c4009
	      else if (tobefreed)
Packit 6c4009
		free (newp);
Packit 6c4009
	      return;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  break;
Packit 6c4009
	}
Packit 6c4009
      else if (cmpres < 0)
Packit 6c4009
	rootp = &root->left;
Packit 6c4009
      else
Packit 6c4009
	rootp = &root->right;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Plug in the new node here.  */
Packit 6c4009
  *rootp = newp;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Add new module.  */
Packit 6c4009
static void
Packit 6c4009
add_module (char *rp, const char *directory, size_t dir_len, void **modules,
Packit 6c4009
	    size_t *nmodules, int modcounter)
Packit 6c4009
{
Packit 6c4009
  /* We expect now
Packit 6c4009
     1. `from' name
Packit 6c4009
     2. `to' name
Packit 6c4009
     3. filename of the module
Packit 6c4009
     4. an optional cost value
Packit 6c4009
  */
Packit 6c4009
  struct gconv_alias fake_alias;
Packit 6c4009
  struct gconv_module *new_module;
Packit 6c4009
  char *from, *to, *module, *wp;
Packit 6c4009
  int need_ext;
Packit 6c4009
  int cost_hi;
Packit 6c4009
Packit 6c4009
  while (__isspace_l (*rp, _nl_C_locobj_ptr))
Packit 6c4009
    ++rp;
Packit 6c4009
  from = rp;
Packit 6c4009
  while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
Packit 6c4009
    {
Packit 6c4009
      *rp = __toupper_l (*rp, _nl_C_locobj_ptr);
Packit 6c4009
      ++rp;
Packit 6c4009
    }
Packit 6c4009
  if (*rp == '\0')
Packit 6c4009
    return;
Packit 6c4009
  *rp++ = '\0';
Packit 6c4009
  to = wp = rp;
Packit 6c4009
  while (__isspace_l (*rp, _nl_C_locobj_ptr))
Packit 6c4009
    ++rp;
Packit 6c4009
  while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
Packit 6c4009
    *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr);
Packit 6c4009
  if (*rp == '\0')
Packit 6c4009
    return;
Packit 6c4009
  *wp++ = '\0';
Packit 6c4009
  do
Packit 6c4009
    ++rp;
Packit 6c4009
  while (__isspace_l (*rp, _nl_C_locobj_ptr));
Packit 6c4009
  module = wp;
Packit 6c4009
  while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
Packit 6c4009
    *wp++ = *rp++;
Packit 6c4009
  if (*rp == '\0')
Packit 6c4009
    {
Packit 6c4009
      /* There is no cost, use one by default.  */
Packit 6c4009
      *wp++ = '\0';
Packit 6c4009
      cost_hi = 1;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* There might be a cost value.  */
Packit 6c4009
      char *endp;
Packit 6c4009
Packit 6c4009
      *wp++ = '\0';
Packit 6c4009
      cost_hi = strtol (rp, &endp, 10);
Packit 6c4009
      if (rp == endp || cost_hi < 1)
Packit 6c4009
	/* No useful information.  */
Packit 6c4009
	cost_hi = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (module[0] == '\0')
Packit 6c4009
    /* No module name given.  */
Packit 6c4009
    return;
Packit 6c4009
  if (module[0] == '/')
Packit 6c4009
    dir_len = 0;
Packit 6c4009
Packit 6c4009
  /* See whether we must add the ending.  */
Packit 6c4009
  need_ext = 0;
Packit 6c4009
  if (wp - module < (ptrdiff_t) sizeof (gconv_module_ext)
Packit 6c4009
      || memcmp (wp - sizeof (gconv_module_ext), gconv_module_ext,
Packit 6c4009
		 sizeof (gconv_module_ext)) != 0)
Packit 6c4009
    /* We must add the module extension.  */
Packit 6c4009
    need_ext = sizeof (gconv_module_ext) - 1;
Packit 6c4009
Packit 6c4009
  /* See whether we have already an alias with this name defined.  */
Packit 6c4009
  fake_alias.fromname = strndupa (from, to - from);
Packit 6c4009
Packit 6c4009
  if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare) != NULL)
Packit 6c4009
    /* This module duplicates an alias.  */
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
  new_module = (struct gconv_module *) calloc (1,
Packit 6c4009
					       sizeof (struct gconv_module)
Packit 6c4009
					       + (wp - from)
Packit 6c4009
					       + dir_len + need_ext);
Packit 6c4009
  if (new_module != NULL)
Packit 6c4009
    {
Packit 6c4009
      char *tmp;
Packit 6c4009
Packit 6c4009
      new_module->from_string = tmp = (char *) (new_module + 1);
Packit 6c4009
      tmp = __mempcpy (tmp, from, to - from);
Packit 6c4009
Packit 6c4009
      new_module->to_string = tmp;
Packit 6c4009
      tmp = __mempcpy (tmp, to, module - to);
Packit 6c4009
Packit 6c4009
      new_module->cost_hi = cost_hi;
Packit 6c4009
      new_module->cost_lo = modcounter;
Packit 6c4009
Packit 6c4009
      new_module->module_name = tmp;
Packit 6c4009
Packit 6c4009
      if (dir_len != 0)
Packit 6c4009
	tmp = __mempcpy (tmp, directory, dir_len);
Packit 6c4009
Packit 6c4009
      tmp = __mempcpy (tmp, module, wp - module);
Packit 6c4009
Packit 6c4009
      if (need_ext)
Packit 6c4009
	memcpy (tmp - 1, gconv_module_ext, sizeof (gconv_module_ext));
Packit 6c4009
Packit 6c4009
      /* Now insert the new module data structure in our search tree.  */
Packit 6c4009
      insert_module (new_module, 1);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Read the next configuration file.  */
Packit 6c4009
static void
Packit 6c4009
read_conf_file (const char *filename, const char *directory, size_t dir_len,
Packit 6c4009
		void **modules, size_t *nmodules)
Packit 6c4009
{
Packit 6c4009
  /* Note the file is opened with cancellation in the I/O functions
Packit 6c4009
     disabled.  */
Packit 6c4009
  FILE *fp = fopen (filename, "rce");
Packit 6c4009
  char *line = NULL;
Packit 6c4009
  size_t line_len = 0;
Packit 6c4009
  static int modcounter;
Packit 6c4009
Packit 6c4009
  /* Don't complain if a file is not present or readable, simply silently
Packit 6c4009
     ignore it.  */
Packit 6c4009
  if (fp == NULL)
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
  /* No threads reading from this stream.  */
Packit 6c4009
  __fsetlocking (fp, FSETLOCKING_BYCALLER);
Packit 6c4009
Packit 6c4009
  /* Process the known entries of the file.  Comments start with `#' and
Packit 6c4009
     end with the end of the line.  Empty lines are ignored.  */
Packit 6c4009
  while (!__feof_unlocked (fp))
Packit 6c4009
    {
Packit 6c4009
      char *rp, *endp, *word;
Packit 6c4009
      ssize_t n = __getdelim (&line, &line_len, '\n', fp);
Packit 6c4009
      if (n < 0)
Packit 6c4009
	/* An error occurred.  */
Packit 6c4009
	break;
Packit 6c4009
Packit 6c4009
      rp = line;
Packit 6c4009
      /* Terminate the line (excluding comments or newline) by an NUL byte
Packit 6c4009
	 to simplify the following code.  */
Packit 6c4009
      endp = strchr (rp, '#');
Packit 6c4009
      if (endp != NULL)
Packit 6c4009
	*endp = '\0';
Packit 6c4009
      else
Packit 6c4009
	if (rp[n - 1] == '\n')
Packit 6c4009
	  rp[n - 1] = '\0';
Packit 6c4009
Packit 6c4009
      while (__isspace_l (*rp, _nl_C_locobj_ptr))
Packit 6c4009
	++rp;
Packit 6c4009
Packit 6c4009
      /* If this is an empty line go on with the next one.  */
Packit 6c4009
      if (rp == endp)
Packit 6c4009
	continue;
Packit 6c4009
Packit 6c4009
      word = rp;
Packit 6c4009
      while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
Packit 6c4009
	++rp;
Packit 6c4009
Packit 6c4009
      if (rp - word == sizeof ("alias") - 1
Packit 6c4009
	  && memcmp (word, "alias", sizeof ("alias") - 1) == 0)
Packit 6c4009
	add_alias (rp, *modules);
Packit 6c4009
      else if (rp - word == sizeof ("module") - 1
Packit 6c4009
	       && memcmp (word, "module", sizeof ("module") - 1) == 0)
Packit 6c4009
	add_module (rp, directory, dir_len, modules, nmodules, modcounter++);
Packit 6c4009
      /* else */
Packit 6c4009
	/* Otherwise ignore the line.  */
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  free (line);
Packit 6c4009
Packit 6c4009
  fclose (fp);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Determine the directories we are looking for data in.  */
Packit 6c4009
void
Packit 6c4009
__gconv_get_path (void)
Packit 6c4009
{
Packit 6c4009
  struct path_elem *result;
Packit 6c4009
  __libc_lock_define_initialized (static, lock);
Packit 6c4009
Packit 6c4009
  __libc_lock_lock (lock);
Packit 6c4009
Packit 6c4009
  /* Make sure there wasn't a second thread doing it already.  */
Packit 6c4009
  result = (struct path_elem *) __gconv_path_elem;
Packit 6c4009
  if (result == NULL)
Packit 6c4009
    {
Packit 6c4009
      /* Determine the complete path first.  */
Packit 6c4009
      char *gconv_path;
Packit 6c4009
      size_t gconv_path_len;
Packit 6c4009
      char *elem;
Packit 6c4009
      char *oldp;
Packit 6c4009
      char *cp;
Packit 6c4009
      int nelems;
Packit 6c4009
      char *cwd;
Packit 6c4009
      size_t cwdlen;
Packit 6c4009
Packit 6c4009
      if (__gconv_path_envvar == NULL)
Packit 6c4009
	{
Packit 6c4009
	  /* No user-defined path.  Make a modifiable copy of the
Packit 6c4009
	     default path.  */
Packit 6c4009
	  gconv_path = strdupa (default_gconv_path);
Packit 6c4009
	  gconv_path_len = sizeof (default_gconv_path);
Packit 6c4009
	  cwd = NULL;
Packit 6c4009
	  cwdlen = 0;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* Append the default path to the user-defined path.  */
Packit 6c4009
	  size_t user_len = strlen (__gconv_path_envvar);
Packit 6c4009
Packit 6c4009
	  gconv_path_len = user_len + 1 + sizeof (default_gconv_path);
Packit 6c4009
	  gconv_path = alloca (gconv_path_len);
Packit 6c4009
	  __mempcpy (__mempcpy (__mempcpy (gconv_path, __gconv_path_envvar,
Packit 6c4009
					   user_len),
Packit 6c4009
				":", 1),
Packit 6c4009
		     default_gconv_path, sizeof (default_gconv_path));
Packit 6c4009
	  cwd = __getcwd (NULL, 0);
Packit 6c4009
	  cwdlen = __glibc_unlikely (cwd == NULL) ? 0 : strlen (cwd);
Packit 6c4009
	}
Packit 6c4009
      assert (default_gconv_path[0] == '/');
Packit 6c4009
Packit 6c4009
      /* In a first pass we calculate the number of elements.  */
Packit 6c4009
      oldp = NULL;
Packit 6c4009
      cp = strchr (gconv_path, ':');
Packit 6c4009
      nelems = 1;
Packit 6c4009
      while (cp != NULL)
Packit 6c4009
	{
Packit 6c4009
	  if (cp != oldp + 1)
Packit 6c4009
	    ++nelems;
Packit 6c4009
	  oldp = cp;
Packit 6c4009
	  cp =  strchr (cp + 1, ':');
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Allocate the memory for the result.  */
Packit 6c4009
      result = (struct path_elem *) malloc ((nelems + 1)
Packit 6c4009
					    * sizeof (struct path_elem)
Packit 6c4009
					    + gconv_path_len + nelems
Packit 6c4009
					    + (nelems - 1) * (cwdlen + 1));
Packit 6c4009
      if (result != NULL)
Packit 6c4009
	{
Packit 6c4009
	  char *strspace = (char *) &result[nelems + 1];
Packit 6c4009
	  int n = 0;
Packit 6c4009
Packit 6c4009
	  /* Separate the individual parts.  */
Packit 6c4009
	  __gconv_max_path_elem_len = 0;
Packit 6c4009
	  elem = __strtok_r (gconv_path, ":", &gconv_path);
Packit 6c4009
	  assert (elem != NULL);
Packit 6c4009
	  do
Packit 6c4009
	    {
Packit 6c4009
	      result[n].name = strspace;
Packit 6c4009
	      if (elem[0] != '/')
Packit 6c4009
		{
Packit 6c4009
		  assert (cwd != NULL);
Packit 6c4009
		  strspace = __mempcpy (strspace, cwd, cwdlen);
Packit 6c4009
		  *strspace++ = '/';
Packit 6c4009
		}
Packit 6c4009
	      strspace = __stpcpy (strspace, elem);
Packit 6c4009
	      if (strspace[-1] != '/')
Packit 6c4009
		*strspace++ = '/';
Packit 6c4009
Packit 6c4009
	      result[n].len = strspace - result[n].name;
Packit 6c4009
	      if (result[n].len > __gconv_max_path_elem_len)
Packit 6c4009
		__gconv_max_path_elem_len = result[n].len;
Packit 6c4009
Packit 6c4009
	      *strspace++ = '\0';
Packit 6c4009
	      ++n;
Packit 6c4009
	    }
Packit 6c4009
	  while ((elem = __strtok_r (NULL, ":", &gconv_path)) != NULL);
Packit 6c4009
Packit 6c4009
	  result[n].name = NULL;
Packit 6c4009
	  result[n].len = 0;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      __gconv_path_elem = result ?: (struct path_elem *) &empty_path_elem;
Packit 6c4009
Packit 6c4009
      free (cwd);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  __libc_lock_unlock (lock);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Read all configuration files found in the user-specified and the default
Packit 6c4009
   path.  */
Packit 6c4009
void
Packit 6c4009
attribute_hidden
Packit 6c4009
__gconv_read_conf (void)
Packit 6c4009
{
Packit 6c4009
  void *modules = NULL;
Packit 6c4009
  size_t nmodules = 0;
Packit 6c4009
  int save_errno = errno;
Packit 6c4009
  size_t cnt;
Packit 6c4009
Packit 6c4009
  /* First see whether we should use the cache.  */
Packit 6c4009
  if (__gconv_load_cache () == 0)
Packit 6c4009
    {
Packit 6c4009
      /* Yes, we are done.  */
Packit 6c4009
      __set_errno (save_errno);
Packit 6c4009
      return;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
#ifndef STATIC_GCONV
Packit 6c4009
  /* Find out where we have to look.  */
Packit 6c4009
  if (__gconv_path_elem == NULL)
Packit 6c4009
    __gconv_get_path ();
Packit 6c4009
Packit 6c4009
  for (cnt = 0; __gconv_path_elem[cnt].name != NULL; ++cnt)
Packit 6c4009
    {
Packit 6c4009
      const char *elem = __gconv_path_elem[cnt].name;
Packit 6c4009
      size_t elem_len = __gconv_path_elem[cnt].len;
Packit 6c4009
      char *filename;
Packit 6c4009
Packit 6c4009
      /* No slash needs to be inserted between elem and gconv_conf_filename;
Packit 6c4009
	 elem already ends in a slash.  */
Packit 6c4009
      filename = alloca (elem_len + sizeof (gconv_conf_filename));
Packit 6c4009
      __mempcpy (__mempcpy (filename, elem, elem_len),
Packit 6c4009
		 gconv_conf_filename, sizeof (gconv_conf_filename));
Packit 6c4009
Packit 6c4009
      /* Read the next configuration file.  */
Packit 6c4009
      read_conf_file (filename, elem, elem_len, &modules, &nmodules);
Packit 6c4009
    }
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  /* Add the internal modules.  */
Packit 6c4009
  for (cnt = 0; cnt < sizeof (builtin_modules) / sizeof (builtin_modules[0]);
Packit 6c4009
       ++cnt)
Packit 6c4009
    {
Packit 6c4009
      struct gconv_alias fake_alias;
Packit 6c4009
Packit 6c4009
      fake_alias.fromname = (char *) builtin_modules[cnt].from_string;
Packit 6c4009
Packit 6c4009
      if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare)
Packit 6c4009
	  != NULL)
Packit 6c4009
	/* It'll conflict so don't add it.  */
Packit 6c4009
	continue;
Packit 6c4009
Packit 6c4009
      insert_module (&builtin_modules[cnt], 0);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Add aliases for builtin conversions.  */
Packit 6c4009
  const char *cp = builtin_aliases;
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      const char *from = cp;
Packit 6c4009
      const char *to = __rawmemchr (from, '\0') + 1;
Packit 6c4009
      cp = __rawmemchr (to, '\0') + 1;
Packit 6c4009
Packit 6c4009
      add_alias2 (from, to, cp, modules);
Packit 6c4009
    }
Packit 6c4009
  while (*cp != '\0');
Packit 6c4009
Packit 6c4009
  /* Restore the error number.  */
Packit 6c4009
  __set_errno (save_errno);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Free all resources if necessary.  */
Packit 6c4009
libc_freeres_fn (free_mem)
Packit 6c4009
{
Packit 6c4009
  if (__gconv_path_elem != NULL && __gconv_path_elem != &empty_path_elem)
Packit 6c4009
    free ((void *) __gconv_path_elem);
Packit 6c4009
}