]> git.ipfire.org Git - thirdparty/glibc.git/blame - wcsmbs/wctob.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / wcsmbs / wctob.c
CommitLineData
04277e02 1/* Copyright (C) 1996-2019 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 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://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
9d46370c 30wctob (wint_t c)
30de3b18 31{
3cc4a097 32 unsigned char buf[MB_LEN_MAX];
d64b6ad0 33 struct __gconv_step_data data;
4bca4c17 34 wchar_t inbuf[1];
8619129f 35 wchar_t *inptr = inbuf;
5e7a22c9 36 size_t dummy;
4bca4c17 37 int status;
96310297 38 const struct gconv_fcts *fcts;
4bca4c17 39
0e16ecfa
UD
40 if (c == WEOF)
41 return EOF;
42
91682d70
UD
43 /* We know that only ASCII compatible encodings are used for the
44 locale and that the wide character encoding is ISO 10646. */
45 if (c >= L'\0' && c <= L'\x7f')
46 return (int) c;
47
4bca4c17 48 /* Tell where we want the result. */
d64b6ad0
UD
49 data.__outbuf = buf;
50 data.__outbufend = buf + MB_LEN_MAX;
51 data.__invocation_counter = 0;
52 data.__internal_use = 1;
85830c4c 53 data.__flags = __GCONV_IS_LAST;
d64b6ad0 54 data.__statep = &data.__state;
4bca4c17
UD
55
56 /* Make sure we start in the initial state. */
57 memset (&data.__state, '\0', sizeof (mbstate_t));
58
96310297
RM
59 /* Get the conversion functions. */
60 fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
4bca4c17
UD
61
62 /* Create the input string. */
63 inbuf[0] = c;
4bca4c17 64
9c7ff11a 65 const unsigned char *argptr = (const unsigned char *) inptr;
915a6c51
UD
66 __gconv_fct fct = fcts->tomb->__fct;
67#ifdef PTR_DEMANGLE
68 if (fcts->tomb->__shlib_handle != NULL)
69 PTR_DEMANGLE (fct);
70#endif
71 status = DL_CALL_FCT (fct,
9c7ff11a
UD
72 (fcts->tomb, &data, &argptr,
73 argptr + sizeof (inbuf[0]), NULL, &dummy, 0, 1));
55985355 74
4bca4c17 75 /* The conversion failed or the output is too long. */
d64b6ad0
UD
76 if ((status != __GCONV_OK && status != __GCONV_FULL_OUTPUT
77 && status != __GCONV_EMPTY_INPUT)
78 || data.__outbuf != (unsigned char *) (buf + 1))
3c720987 79 return EOF;
4bca4c17 80
91682d70 81 return buf[0];
30de3b18 82}