]> git.ipfire.org Git - thirdparty/glibc.git/blame - wcsmbs/wctob.c
Update copyright notices with scripts/update-copyrights
[thirdparty/glibc.git] / wcsmbs / wctob.c
CommitLineData
d4697bc9 1/* Copyright (C) 1996-2014 Free Software Foundation, Inc.
33a934a3
UD
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1996.
30de3b18 4
33a934a3 5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
30de3b18 9
33a934a3
UD
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 13 Lesser General Public License for more details.
30de3b18 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
30de3b18 18
55985355 19#include <dlfcn.h>
4bca4c17 20#include <gconv.h>
30de3b18 21#include <stdio.h>
4bca4c17 22#include <string.h>
30de3b18 23#include <wchar.h>
4bca4c17 24#include <wcsmbsload.h>
30de3b18 25
915a6c51
UD
26#include <sysdep.h>
27
30de3b18
RM
28
29int
30wctob (c)
31 wint_t c;
32{
3cc4a097 33 unsigned char buf[MB_LEN_MAX];
d64b6ad0 34 struct __gconv_step_data data;
4bca4c17 35 wchar_t inbuf[1];
8619129f 36 wchar_t *inptr = inbuf;
5e7a22c9 37 size_t dummy;
4bca4c17 38 int status;
96310297 39 const struct gconv_fcts *fcts;
4bca4c17 40
0e16ecfa
UD
41 if (c == WEOF)
42 return EOF;
43
91682d70
UD
44 /* We know that only ASCII compatible encodings are used for the
45 locale and that the wide character encoding is ISO 10646. */
46 if (c >= L'\0' && c <= L'\x7f')
47 return (int) c;
48
4bca4c17 49 /* Tell where we want the result. */
d64b6ad0
UD
50 data.__outbuf = buf;
51 data.__outbufend = buf + MB_LEN_MAX;
52 data.__invocation_counter = 0;
53 data.__internal_use = 1;
85830c4c 54 data.__flags = __GCONV_IS_LAST;
d64b6ad0 55 data.__statep = &data.__state;
d6204268 56 data.__trans = NULL;
4bca4c17
UD
57
58 /* Make sure we start in the initial state. */
59 memset (&data.__state, '\0', sizeof (mbstate_t));
60
96310297
RM
61 /* Get the conversion functions. */
62 fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
4bca4c17
UD
63
64 /* Create the input string. */
65 inbuf[0] = c;
4bca4c17 66
9c7ff11a 67 const unsigned char *argptr = (const unsigned char *) inptr;
915a6c51
UD
68 __gconv_fct fct = fcts->tomb->__fct;
69#ifdef PTR_DEMANGLE
70 if (fcts->tomb->__shlib_handle != NULL)
71 PTR_DEMANGLE (fct);
72#endif
73 status = DL_CALL_FCT (fct,
9c7ff11a
UD
74 (fcts->tomb, &data, &argptr,
75 argptr + sizeof (inbuf[0]), NULL, &dummy, 0, 1));
55985355 76
4bca4c17 77 /* The conversion failed or the output is too long. */
d64b6ad0
UD
78 if ((status != __GCONV_OK && status != __GCONV_FULL_OUTPUT
79 && status != __GCONV_EMPTY_INPUT)
80 || data.__outbuf != (unsigned char *) (buf + 1))
3c720987 81 return EOF;
4bca4c17 82
91682d70 83 return buf[0];
30de3b18 84}