]> git.ipfire.org Git - thirdparty/glibc.git/blame - iconv/gconv_open.c
(dcigettext): Fix interpretation of tsearch return value.
[thirdparty/glibc.git] / iconv / gconv_open.c
CommitLineData
6973fc01 1/* Find matching transformation algorithms and initialize steps.
0aece08d 2 Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
6973fc01
UD
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21#include <errno.h>
6973fc01 22#include <stdlib.h>
fd19ed3d 23#include <string.h>
6973fc01 24
e62c19f1
UD
25#include <gconv_int.h>
26
6973fc01
UD
27
28int
e62c19f1 29internal_function
c90a2db6
UD
30__gconv_open (const char *toset, const char *fromset, __gconv_t *handle,
31 int flags)
6973fc01 32{
d64b6ad0 33 struct __gconv_step *steps;
6973fc01 34 size_t nsteps;
d64b6ad0 35 __gconv_t result = NULL;
6973fc01
UD
36 size_t cnt = 0;
37 int res;
38
c90a2db6 39 res = __gconv_find_transform (toset, fromset, &steps, &nsteps, flags);
d64b6ad0 40 if (res == __GCONV_OK)
6973fc01
UD
41 {
42 /* Allocate room for handle. */
d64b6ad0
UD
43 result = (__gconv_t) malloc (sizeof (struct __gconv_info)
44 + (nsteps
45 * sizeof (struct __gconv_step_data)));
6973fc01 46 if (result == NULL)
d64b6ad0 47 res = __GCONV_NOMEM;
6973fc01
UD
48 else
49 {
50 /* Remember the list of steps. */
d64b6ad0
UD
51 result->__steps = steps;
52 result->__nsteps = nsteps;
6973fc01 53
390500b1 54 /* Clear the array for the step data. */
d64b6ad0
UD
55 memset (result->__data, '\0',
56 nsteps * sizeof (struct __gconv_step_data));
6973fc01 57
390500b1 58 /* Call all initialization functions for the transformation
49c091e5 59 step implementations. */
0aece08d 60 for (cnt = 0; cnt < nsteps - 1; ++cnt)
6973fc01 61 {
0aece08d
UD
62 size_t size;
63
390500b1
UD
64 /* If this is the last step we must not allocate an
65 output buffer. */
0aece08d 66 result->__data[cnt].__is_last = 0;
6973fc01 67
390500b1 68 /* Reset the counter. */
d64b6ad0 69 result->__data[cnt].__invocation_counter = 0;
6973fc01 70
390500b1 71 /* It's a regular use. */
d64b6ad0 72 result->__data[cnt].__internal_use = 0;
e3e0a182 73
390500b1 74 /* We use the `mbstate_t' member in DATA. */
d64b6ad0 75 result->__data[cnt].__statep = &result->__data[cnt].__state;
e3e0a182 76
390500b1 77 /* Allocate the buffer. */
0aece08d
UD
78 size = (GCONV_NCHAR_GOAL * steps[cnt].__max_needed_to);
79
80 result->__data[cnt].__outbuf = (char *) malloc (size);
81 if (result->__data[cnt].__outbuf == NULL)
390500b1 82 {
0aece08d
UD
83 res = __GCONV_NOMEM;
84 break;
6973fc01 85 }
0aece08d
UD
86 result->__data[cnt].__outbufend =
87 result->__data[cnt].__outbuf + size;
6973fc01 88 }
0aece08d
UD
89
90 /* Now handle the last entry. */
91 result->__data[cnt].__is_last = 1;
92 result->__data[cnt].__invocation_counter = 0;
93 result->__data[cnt].__internal_use = 0;
94 result->__data[cnt].__statep = &result->__data[cnt].__state;
6973fc01
UD
95 }
96 }
97
d64b6ad0 98 if (res != __GCONV_OK)
6973fc01
UD
99 {
100 /* Something went wrong. Free all the resources. */
101 int serrno = errno;
102
103 if (result != NULL)
104 {
390500b1 105 while (cnt-- > 0)
d64b6ad0 106 free (result->__data[cnt].__outbuf);
6973fc01
UD
107
108 free (result);
109 result = NULL;
110 }
111
112 __gconv_close_transform (steps, nsteps);
113
114 __set_errno (serrno);
115 }
116
117 *handle = result;
118 return res;
119}