]>
Commit | Line | Data |
---|---|---|
04277e02 | 1 | /* Copyright (C) 1996-2019 Free Software Foundation, Inc. |
b5791037 | 2 | This file is part of the GNU C Library. |
6a07e1f8 | 3 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. |
30de3b18 | 4 | |
b5791037 | 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 | |
b5791037 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 | |
915a6c51 | 19 | #include <assert.h> |
55985355 | 20 | #include <dlfcn.h> |
30de3b18 | 21 | #include <errno.h> |
4bca4c17 UD |
22 | #include <gconv.h> |
23 | #include <stdlib.h> | |
30de3b18 | 24 | #include <wchar.h> |
4bca4c17 UD |
25 | #include <wcsmbsload.h> |
26 | ||
915a6c51 | 27 | #include <sysdep.h> |
30de3b18 | 28 | |
80bcb410 | 29 | #ifndef EILSEQ |
4bca4c17 | 30 | # define EILSEQ EINVAL |
80bcb410 | 31 | #endif |
30de3b18 | 32 | |
6dbe2837 | 33 | |
4bca4c17 UD |
34 | /* This is the private state used if PS is NULL. */ |
35 | static mbstate_t state; | |
6dbe2837 | 36 | |
30de3b18 | 37 | size_t |
23396375 | 38 | __wcrtomb (char *s, wchar_t wc, mbstate_t *ps) |
30de3b18 | 39 | { |
9954432e | 40 | char buf[MB_LEN_MAX]; |
d64b6ad0 | 41 | struct __gconv_step_data data; |
4bca4c17 UD |
42 | int status; |
43 | size_t result; | |
8619129f | 44 | size_t dummy; |
96310297 | 45 | const struct gconv_fcts *fcts; |
4bca4c17 | 46 | |
a7f91846 | 47 | /* Set information for this step. */ |
d64b6ad0 UD |
48 | data.__invocation_counter = 0; |
49 | data.__internal_use = 1; | |
85830c4c | 50 | data.__flags = __GCONV_IS_LAST; |
d64b6ad0 | 51 | data.__statep = ps ?: &state; |
4bca4c17 UD |
52 | |
53 | /* A first special case is if S is NULL. This means put PS in the | |
54 | initial state. */ | |
30de3b18 RM |
55 | if (s == NULL) |
56 | { | |
a7f91846 | 57 | s = buf; |
4bca4c17 | 58 | wc = L'\0'; |
30de3b18 RM |
59 | } |
60 | ||
a7f91846 | 61 | /* Tell where we want to have the result. */ |
1c99f950 UD |
62 | data.__outbuf = (unsigned char *) s; |
63 | data.__outbufend = (unsigned char *) s + MB_CUR_MAX; | |
a7f91846 | 64 | |
96310297 RM |
65 | /* Get the conversion functions. */ |
66 | fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE)); | |
915a6c51 UD |
67 | __gconv_fct fct = fcts->tomb->__fct; |
68 | #ifdef PTR_DEMANGLE | |
69 | if (fcts->tomb->__shlib_handle != NULL) | |
70 | PTR_DEMANGLE (fct); | |
71 | #endif | |
4bca4c17 UD |
72 | |
73 | /* If WC is the NUL character we write into the output buffer the byte | |
74 | sequence necessary for PS to get into the initial state, followed | |
75 | by a NUL byte. */ | |
76 | if (wc == L'\0') | |
30de3b18 | 77 | { |
915a6c51 UD |
78 | status = DL_CALL_FCT (fct, (fcts->tomb, &data, NULL, NULL, |
79 | NULL, &dummy, 1, 1)); | |
07a4742f | 80 | |
d64b6ad0 UD |
81 | if (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT) |
82 | *data.__outbuf++ = '\0'; | |
4bca4c17 UD |
83 | } |
84 | else | |
07a4742f | 85 | { |
4bca4c17 | 86 | /* Do a normal conversion. */ |
b117f744 | 87 | const unsigned char *inbuf = (const unsigned char *) &wc; |
4bca4c17 | 88 | |
915a6c51 | 89 | status = DL_CALL_FCT (fct, |
96310297 | 90 | (fcts->tomb, &data, &inbuf, |
f1d5c60d | 91 | inbuf + sizeof (wchar_t), NULL, &dummy, 0, 1)); |
30de3b18 RM |
92 | } |
93 | ||
4bca4c17 UD |
94 | /* There must not be any problems with the conversion but illegal input |
95 | characters. The output buffer must be large enough, otherwise the | |
96 | definition of MB_CUR_MAX is not correct. All the other possible | |
97 | errors also must not happen. */ | |
d64b6ad0 UD |
98 | assert (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT |
99 | || status == __GCONV_ILLEGAL_INPUT | |
100 | || status == __GCONV_INCOMPLETE_INPUT | |
101 | || status == __GCONV_FULL_OUTPUT); | |
102 | ||
103 | if (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT | |
104 | || status == __GCONV_FULL_OUTPUT) | |
a7f91846 | 105 | result = data.__outbuf - (unsigned char *) s; |
4bca4c17 | 106 | else |
30de3b18 | 107 | { |
4bca4c17 UD |
108 | result = (size_t) -1; |
109 | __set_errno (EILSEQ); | |
30de3b18 RM |
110 | } |
111 | ||
4bca4c17 | 112 | return result; |
30de3b18 | 113 | } |
23396375 | 114 | weak_alias (__wcrtomb, wcrtomb) |
8784cc18 | 115 | libc_hidden_weak (wcrtomb) |