]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/mbstowcs.3
libc.7: Add a note on why glibc 2.x uses the soname libc.so.6
[thirdparty/man-pages.git] / man3 / mbstowcs.3
1 '\" t -*- coding: UTF-8 -*-
2 .\" Copyright (c) Bruno Haible <haible@clisp.cons.org>
3 .\" and Copyright 2014 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\"
5 .\" %%%LICENSE_START(GPLv2+_DOC_ONEPARA)
6 .\" This is free documentation; you can redistribute it and/or
7 .\" modify it under the terms of the GNU General Public License as
8 .\" published by the Free Software Foundation; either version 2 of
9 .\" the License, or (at your option) any later version.
10 .\" %%%LICENSE_END
11 .\"
12 .\" References consulted:
13 .\" GNU glibc-2 source code and manual
14 .\" Dinkumware C library reference http://www.dinkumware.com/
15 .\" OpenGroup's Single UNIX specification http://www.UNIX-systems.org/online.html
16 .\" ISO/IEC 9899:1999
17 .\"
18 .TH MBSTOWCS 3 2016-10-08 "GNU" "Linux Programmer's Manual"
19 .SH NAME
20 mbstowcs \- convert a multibyte string to a wide-character string
21 .SH SYNOPSIS
22 .nf
23 .B #include <stdlib.h>
24 .sp
25 .BI "size_t mbstowcs(wchar_t *" dest ", const char *" src ", size_t " n );
26 .fi
27 .SH DESCRIPTION
28 If
29 .I dest
30 is not NULL,
31 the
32 .BR mbstowcs ()
33 function converts the
34 multibyte string
35 .I src
36 to a wide-character string starting at
37 .IR dest .
38 At most
39 .I n
40 wide characters are written to
41 .IR dest .
42 The sequence of characters in the string
43 .I src
44 shall begin in the initial shift state.
45 The conversion can stop for three reasons:
46 .IP 1. 3
47 An invalid multibyte sequence has been encountered.
48 In this case,
49 .I (size_t)\ \-1
50 is returned.
51 .IP 2.
52 .I n
53 non-L\(aq\\0\(aq wide characters have been stored at
54 .IR dest .
55 In this case, the number of wide characters written to
56 .I dest
57 is returned, but the
58 shift state at this point is lost.
59 .IP 3.
60 The multibyte string has been completely converted, including the
61 terminating null character (\(aq\\0\(aq).
62 In this case, the number of wide characters written to
63 .IR dest ,
64 excluding the terminating null wide character, is returned.
65 .PP
66 The programmer must ensure that there is room for at least
67 .I n
68 wide
69 characters at
70 .IR dest .
71 .PP
72 If
73 .IR dest
74 is NULL,
75 .I n
76 is ignored, and the conversion proceeds as
77 above, except that the converted wide characters are not written out to memory,
78 and that no length limit exists.
79 .PP
80 In order to avoid the case 2 above, the programmer should make sure
81 .I n
82 is
83 greater than or equal to
84 .IR "mbstowcs(NULL,src,0)+1" .
85 .SH RETURN VALUE
86 The
87 .BR mbstowcs ()
88 function returns the number of wide characters that make
89 up the converted part of the wide-character string, not including the
90 terminating null wide character.
91 If an invalid multibyte sequence was
92 encountered,
93 .I (size_t)\ \-1
94 is returned.
95 .SH ATTRIBUTES
96 For an explanation of the terms used in this section, see
97 .BR attributes (7).
98 .TS
99 allbox;
100 lb lb lb
101 l l l.
102 Interface Attribute Value
103 T{
104 .BR mbstowcs ()
105 T} Thread safety MT-Safe
106 .TE
107 .SH CONFORMING TO
108 POSIX.1-2001, POSIX.1-2008, C99.
109 .SH NOTES
110 The behavior of
111 .BR mbstowcs ()
112 depends on the
113 .B LC_CTYPE
114 category of the
115 current locale.
116 .PP
117 The function
118 .BR mbsrtowcs (3)
119 provides a better interface to the same
120 functionality.
121 .SH EXAMPLE
122 The program below illustrates the use of
123 .BR mbstowcs (),
124 as well as some of the wide character classification functions.
125 An example run is the following:
126 .in +4n
127 .nf
128
129 $ ./t_mbstowcs de_DE.UTF\-8 Grüße!
130 Length of source string (excluding terminator):
131 8 bytes
132 6 multibyte characters
133
134 Wide character string is: Grüße! (6 characters)
135 G alpha upper
136 r alpha lower
137 ü alpha lower
138 ß alpha lower
139 e alpha lower
140 ! !alpha
141 .fi
142 .in
143 .SS Program source
144 .nf
145 #include <wctype.h>
146 #include <locale.h>
147 #include <wchar.h>
148 #include <stdio.h>
149 #include <string.h>
150 #include <stdlib.h>
151
152 int
153 main(int argc, char *argv[])
154 {
155 size_t mbslen; /* Number of multibyte characters in source */
156 wchar_t *wcs; /* Pointer to converted wide character string */
157 wchar_t *wp;
158
159 if (argc < 3) {
160 fprintf(stderr, "Usage: %s <locale> <string>\\n", argv[0]);
161 exit(EXIT_FAILURE);
162 }
163
164 /* Apply the specified locale */
165
166 if (setlocale(LC_ALL, argv[1]) == NULL) {
167 perror("setlocale");
168 exit(EXIT_FAILURE);
169 }
170
171 /* Calculate the length required to hold argv[2] converted to
172 a wide character string */
173
174 mbslen = mbstowcs(NULL, argv[2], 0);
175 if (mbslen == (size_t) \-1) {
176 perror("mbstowcs");
177 exit(EXIT_FAILURE);
178 }
179
180 /* Describe the source string to the user */
181
182 printf("Length of source string (excluding terminator):\\n");
183 printf(" %zu bytes\\n", strlen(argv[2]));
184 printf(" %zu multibyte characters\\n\\n", mbslen);
185
186 /* Allocate wide character string of the desired size. Add 1
187 to allow for terminating null wide character (L\(aq\\0\(aq). */
188
189 wcs = calloc(mbslen + 1, sizeof(wchar_t));
190 if (wcs == NULL) {
191 perror("calloc");
192 exit(EXIT_FAILURE);
193 }
194
195 /* Convert the multibyte character string in argv[2] to a
196 wide character string */
197
198 if (mbstowcs(wcs, argv[2], mbslen + 1) == (size_t) \-1) {
199 perror("mbstowcs");
200 exit(EXIT_FAILURE);
201 }
202
203 printf("Wide character string is: %ls (%zu characters)\\n",
204 wcs, mbslen);
205
206 /* Now do some inspection of the classes of the characters in
207 the wide character string */
208
209 for (wp = wcs; *wp != 0; wp++) {
210 printf(" %lc ", (wint_t) *wp);
211
212 if (!iswalpha(*wp))
213 printf("!");
214 printf("alpha ");
215
216 if (iswalpha(*wp)) {
217 if (iswupper(*wp))
218 printf("upper ");
219
220 if (iswlower(*wp))
221 printf("lower ");
222 }
223
224 putchar(\(aq\\n\(aq);
225 }
226
227 exit(EXIT_SUCCESS);
228 }
229 .fi
230 .SH SEE ALSO
231 .BR mblen (3),
232 .BR mbsrtowcs (3),
233 .BR mbtowc (3),
234 .BR wcstombs (3),
235 .BR wctomb (3)