19 static char *locale_encoding = NULL;
20 static int locale_encoding_is_utf8 = 0;
22 size_t src_size, dest_size;
26 if (locale_encoding == NULL) {
27 #ifdef HAVE_LANGINFO_H
28 const char *encoding = nl_langinfo(CODESET);
30 const char *encoding =
"char";
32 locale_encoding = (
char *)
xmalloc(strlen(encoding) + 11);
33 sprintf(locale_encoding,
"%s//TRANSLIT", encoding);
34 locale_encoding_is_utf8 = strcasecmp(encoding,
"UTF-8") == 0;
37 if (!str || !*str || locale_encoding_is_utf8)
40 cd = iconv_open(locale_encoding,
"UTF-8");
41 if (cd == (iconv_t)-1)
44 src_size = strlen(str);
45 dest_size = src_size + 1;
46 result = (
char *)
xmalloc(dest_size);
50 size_t status = iconv(cd, (
char **)&src, &src_size, &dest, &dest_size);
51 if (status == (
size_t)-1) {
55 (void) iconv_close(cd);
58 dest_offset = dest - result;
60 result = (
char *)
xrealloc(result, dest_offset + dest_size);
61 dest = result + dest_offset;
62 }
else if (src_size == 0) {
63 if (src == NULL)
break;
67 (void) iconv_close(cd);
70 size_t dest_offset = dest - result;
71 result = (
char *)
xrealloc(result, dest_offset + 1);
72 dest = result + dest_offset;
const char * xstrtolocale(const char *str)
Force encoding of string.