mount_setattr.2: Further tweaks after feedback from Christian Brauner
[man-pages.git] / man3 / mbstowcs.3
blob503f5f75b1468cbc1b71378c457ac7858911c545
1 .\" Copyright (c) Bruno Haible <haible@clisp.cons.org>
2 .\" and Copyright 2014 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(GPLv2+_DOC_ONEPARA)
5 .\" This is free documentation; you can redistribute it and/or
6 .\" modify it under the terms of the GNU General Public License as
7 .\" published by the Free Software Foundation; either version 2 of
8 .\" the License, or (at your option) any later version.
9 .\" %%%LICENSE_END
10 .\"
11 .\" References consulted:
12 .\"   GNU glibc-2 source code and manual
13 .\"   Dinkumware C library reference http://www.dinkumware.com/
14 .\"   OpenGroup's Single UNIX specification http://www.UNIX-systems.org/online.html
15 .\"   ISO/IEC 9899:1999
16 .\"
17 .TH MBSTOWCS 3  2021-03-22 "GNU" "Linux Programmer's Manual"
18 .SH NAME
19 mbstowcs \- convert a multibyte string to a wide-character string
20 .SH SYNOPSIS
21 .nf
22 .B #include <stdlib.h>
23 .PP
24 .BI "size_t mbstowcs(wchar_t *restrict " dest ", const char *restrict " src ,
25 .BI "                size_t " n );
26 .fi
27 .SH DESCRIPTION
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\e0\(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\e0\(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
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
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 .ad l
99 .nh
101 allbox;
102 lbx lb lb
103 l l l.
104 Interface       Attribute       Value
106 .BR mbstowcs ()
107 T}      Thread safety   MT-Safe
111 .sp 1
112 .SH CONFORMING TO
113 POSIX.1-2001, POSIX.1-2008, C99.
114 .SH NOTES
115 The behavior of
116 .BR mbstowcs ()
117 depends on the
118 .B LC_CTYPE
119 category of the
120 current locale.
122 The function
123 .BR mbsrtowcs (3)
124 provides a better interface to the same
125 functionality.
126 .SH EXAMPLES
127 The program below illustrates the use of
128 .BR mbstowcs (),
129 as well as some of the wide character classification functions.
130 An example run is the following:
132 .in +4n
134 $ ./t_mbstowcs de_DE.UTF\-8 Grüße!
135 Length of source string (excluding terminator):
136     8 bytes
137     6 multibyte characters
139 Wide character string is: Grüße! (6 characters)
140     G alpha upper
141     r alpha lower
142     ü alpha lower
143     ß alpha lower
144     e alpha lower
145     ! !alpha
148 .SS Program source
151 #include <wctype.h>
152 #include <locale.h>
153 #include <wchar.h>
154 #include <stdio.h>
155 #include <string.h>
156 #include <stdlib.h>
159 main(int argc, char *argv[])
161     size_t mbslen;      /* Number of multibyte characters in source */
162     wchar_t *wcs;       /* Pointer to converted wide character string */
164     if (argc < 3) {
165         fprintf(stderr, "Usage: %s <locale> <string>\en", argv[0]);
166         exit(EXIT_FAILURE);
167     }
169     /* Apply the specified locale. */
171     if (setlocale(LC_ALL, argv[1]) == NULL) {
172         perror("setlocale");
173         exit(EXIT_FAILURE);
174     }
176     /* Calculate the length required to hold argv[2] converted to
177        a wide character string. */
179     mbslen = mbstowcs(NULL, argv[2], 0);
180     if (mbslen == (size_t) \-1) {
181         perror("mbstowcs");
182         exit(EXIT_FAILURE);
183     }
185     /* Describe the source string to the user. */
187     printf("Length of source string (excluding terminator):\en");
188     printf("    %zu bytes\en", strlen(argv[2]));
189     printf("    %zu multibyte characters\en\en", mbslen);
191     /* Allocate wide character string of the desired size.  Add 1
192        to allow for terminating null wide character (L\(aq\e0\(aq). */
194     wcs = calloc(mbslen + 1, sizeof(*wcs));
195     if (wcs == NULL) {
196         perror("calloc");
197         exit(EXIT_FAILURE);
198     }
200     /* Convert the multibyte character string in argv[2] to a
201        wide character string. */
203     if (mbstowcs(wcs, argv[2], mbslen + 1) == (size_t) \-1) {
204         perror("mbstowcs");
205         exit(EXIT_FAILURE);
206     }
208     printf("Wide character string is: %ls (%zu characters)\en",
209             wcs, mbslen);
211     /* Now do some inspection of the classes of the characters in
212        the wide character string. */
214     for (wchar_t *wp = wcs; *wp != 0; wp++) {
215         printf("    %lc ", (wint_t) *wp);
217         if (!iswalpha(*wp))
218             printf("!");
219         printf("alpha ");
221         if (iswalpha(*wp)) {
222             if (iswupper(*wp))
223                 printf("upper ");
225             if (iswlower(*wp))
226                 printf("lower ");
227         }
229         putchar(\(aq\en\(aq);
230     }
232     exit(EXIT_SUCCESS);
235 .SH SEE ALSO
236 .BR mblen (3),
237 .BR mbsrtowcs (3),
238 .BR mbtowc (3),
239 .BR wcstombs (3),
240 .BR wctomb (3)