Hi On Tue, Jul 05, 2011 at 08:12:44PM +0200, Stefan Sperling wrote: > On Tue, Jul 05, 2011 at 01:52:06PM -0400, Todd C. Miller wrote: > > Also, what is that > > 'L' doing there before the "foobar"? Is it some sort of wide char > > thing I'm not aware of? > > > > L"" is C99 notation for wide character string literals. > wchar_t is an int, so a pointer increment will consume 4 bytes, not 1. > L"foobar" results in the byte string: > "f\0\0\0o\0\0\0o\0\0\0b\0\0\0a\0\0\0r\0\0\0\0\0\0\0" > > The standard says the multibyte-encoding used by L"" is > implementation-defined. I am not entirely sure what rules our various > gcc versions apply. L"" might not work in all locales on OpenBSD. > So generally it's better to use something like mbstowcs() instead of L"". > Conversion performed at runtime will always be correct for the active locale.
Something like this? Index: wcsdup.3 =================================================================== RCS file: /cvs/src/lib/libc/string/wcsdup.3,v retrieving revision 1.2 diff -u -p -r1.2 wcsdup.3 --- wcsdup.3 5 Jul 2011 19:01:31 -0000 1.2 +++ wcsdup.3 8 Jul 2011 02:01:22 -0000 @@ -61,9 +61,14 @@ The following will point to an allocated area of memory containing the nul-terminated string .Qq foobar : .Bd -literal -offset indent -wchar_t *p; +const char *o = "foobar"; +wchar_t *p, b[32]; -if ((p = wcsdup(L"foobar")) == NULL) { +if (mbstowcs(b, o, sizeof(b)) == (size_t)-1) { + fprintf(stderr, "Failed to convert string.\en"); + exit(1); +} +if ((p = wcsdup(b)) == NULL) { fprintf(stderr, "Out of memory.\en"); exit(1); }