Use common bits/shm.h for more architectures.
[glibc.git] / manual / examples / mbstouwcs.c
blobc94e1fa7900f7782db0a7e10326c2abd55327cf0
1 #include <stdbool.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <wchar.h>
6 /* Do not include the above headers in the example.
7 */
8 wchar_t *
9 mbstouwcs (const char *s)
11 /* Include the null terminator in the conversion. */
12 size_t len = strlen (s) + 1;
13 wchar_t *result = reallocarray (NULL, len, sizeof (wchar_t));
14 if (result == NULL)
15 return NULL;
17 wchar_t *wcp = result;
18 mbstate_t state;
19 memset (&state, '\0', sizeof (state));
21 while (true)
23 wchar_t wc;
24 size_t nbytes = mbrtowc (&wc, s, len, &state);
25 if (nbytes == 0)
27 /* Terminate the result string. */
28 *wcp = L'\0';
29 break;
31 else if (nbytes == (size_t) -2)
33 /* Truncated input string. */
34 errno = EILSEQ;
35 free (result);
36 return NULL;
38 else if (nbytes == (size_t) -1)
40 /* Some other error (including EILSEQ). */
41 free (result);
42 return NULL;
44 else
46 /* A character was converted. */
47 *wcp++ = towupper (wc);
48 len -= nbytes;
49 s += nbytes;
52 return result;