+#endif
+
+ if (strcmp (to_codeset, from_codeset) == 0)
+ return strdup (str);
+
+#if HAVE_ICONV
+ cd = iconv_open (to_codeset, from_codeset);
+ if (cd == (iconv_t) -1)
+ return NULL;
+
+ dest = iconv_alloc (cd, str);
+
+ if (dest == NULL)
+ {
+ int saved_errno = errno;
+ iconv_close (cd);
+ errno = saved_errno;
+ }
+ else
+ {
+ if (iconv_close (cd) < 0)
+ {
+ int saved_errno2 = errno;
+ /* If we didn't have a real error before, make sure we restore
+ the iconv_close error below. */
+ free (dest);
+ dest = NULL;
+ errno = saved_errno2;
+ }
+ }
+#else
+ errno = ENOSYS;
+#endif
+
+ return dest;
+}
+
+/* Convert a zero-terminated string STR using iconv descriptor CD.
+ The returned string is allocated using malloc, and must be
+ dellocated by the caller using free. On failure, NULL is returned
+ and errno holds the error reason. Note that if the target
+ character set uses \0 for anything but to terminate the string,
+ the caller of this function may have difficulties finding
+ out the length of the output string. */
+#if HAVE_ICONV
+char *
+iconv_alloc (iconv_t cd, const char *str)
+{
+ char *dest;