mirror of
https://sourceware.org/git/glibc.git
synced 2025-10-27 12:15:39 +03:00
Previously this file would fail to compile with the following error:
$ gcc manual/examples/mbstouwcs.c
manual/examples/mbstouwcs.c: In function ‘mbstouwcs’:
manual/examples/mbstouwcs.c:34:11: error: ‘errno’ undeclared (first use in this function)
34 | errno = EILSEQ;
| ^~~~~
manual/examples/mbstouwcs.c:5:1: note: ‘errno’ is defined in header ‘<errno.h>’; this is probably fixable by adding ‘#include <errno.h>’
4 | #include <wchar.h>
+++ |+#include <errno.h>
5 |
manual/examples/mbstouwcs.c:34:11: note: each undeclared identifier is reported only once for each function it appears in
34 | errno = EILSEQ;
| ^~~~~
manual/examples/mbstouwcs.c:34:19: error: ‘EILSEQ’ undeclared (first use in this function)
34 | errno = EILSEQ;
| ^~~~~~
manual/examples/mbstouwcs.c:47:20: error: implicit declaration of function ‘towupper’ [-Wimplicit-function-declaration]
47 | *wcp++ = towupper (wc);
| ^~~~~~~~
manual/examples/mbstouwcs.c:5:1: note: include ‘<wctype.h>’ or provide a declaration of ‘towupper’
4 | #include <wchar.h>
+++ |+#include <wctype.h>
5 |
manual/examples/mbstouwcs.c:47:20: warning: incompatible implicit declaration of built-in function ‘towupper’ [-Wbuiltin-declaration-mismatch]
47 | *wcp++ = towupper (wc);
| ^~~~~~~~
manual/examples/mbstouwcs.c:47:20: note: include ‘<wctype.h>’ or provide a declaration of ‘towupper’
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
56 lines
1.2 KiB
C
56 lines
1.2 KiB
C
#include <errno.h>
|
|
#include <stdbool.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <wchar.h>
|
|
#include <wctype.h>
|
|
|
|
/* Do not include the above headers in the example.
|
|
*/
|
|
wchar_t *
|
|
mbstouwcs (const char *s)
|
|
{
|
|
/* Include the null terminator in the conversion. */
|
|
size_t len = strlen (s) + 1;
|
|
wchar_t *result = reallocarray (NULL, len, sizeof (wchar_t));
|
|
if (result == NULL)
|
|
return NULL;
|
|
|
|
wchar_t *wcp = result;
|
|
mbstate_t state;
|
|
memset (&state, '\0', sizeof (state));
|
|
|
|
while (true)
|
|
{
|
|
wchar_t wc;
|
|
size_t nbytes = mbrtowc (&wc, s, len, &state);
|
|
if (nbytes == 0)
|
|
{
|
|
/* Terminate the result string. */
|
|
*wcp = L'\0';
|
|
break;
|
|
}
|
|
else if (nbytes == (size_t) -2)
|
|
{
|
|
/* Truncated input string. */
|
|
errno = EILSEQ;
|
|
free (result);
|
|
return NULL;
|
|
}
|
|
else if (nbytes == (size_t) -1)
|
|
{
|
|
/* Some other error (including EILSEQ). */
|
|
free (result);
|
|
return NULL;
|
|
}
|
|
else
|
|
{
|
|
/* A character was converted. */
|
|
*wcp++ = towupper (wc);
|
|
len -= nbytes;
|
|
s += nbytes;
|
|
}
|
|
}
|
|
return result;
|
|
}
|