Blame iconv/iconv_open.c

Packit 6c4009
/* Get descriptor for character set conversion.
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 <alloca.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <iconv.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
#include <gconv_int.h>
Packit 6c4009
#include "gconv_charset.h"
Packit 6c4009
Packit 6c4009
Packit 6c4009
iconv_t
Packit 6c4009
iconv_open (const char *tocode, const char *fromcode)
Packit 6c4009
{
Packit Service c3a4e7
  __gconv_t cd;
Packit Service c3a4e7
  struct gconv_spec conv_spec;
Packit 6c4009
Packit Service c3a4e7
  if (__gconv_create_spec (&conv_spec, fromcode, tocode) == NULL)
Packit Service c3a4e7
    return (iconv_t) -1;
Packit 6c4009
Packit Service c3a4e7
  int res = __gconv_open (&conv_spec, &cd, 0);
Packit 6c4009
Packit Service 95f050
  __gconv_destroy_spec (&conv_spec);
Packit 6c4009
Packit 6c4009
  if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
Packit 6c4009
    {
Packit 6c4009
      /* We must set the error number according to the specs.  */
Packit 6c4009
      if (res == __GCONV_NOCONV || res == __GCONV_NODB)
Packit 6c4009
	__set_errno (EINVAL);
Packit 6c4009
Packit 6c4009
      cd = (iconv_t) -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return (iconv_t) cd;
Packit 6c4009
}