Mercurial > hg > octave-kai > gnulib-hg
changeset 6153:ccf4444bbac6
* iconvme.c: Fix errno.
* iconvme.c: Split iconv_string into iconv_alloc.
author | Simon Josefsson <simon@josefsson.org> |
---|---|
date | Mon, 29 Aug 2005 08:09:21 +0000 |
parents | f9d51eb3bf02 |
children | 201b3d9b99f1 |
files | lib/ChangeLog lib/iconvme.c |
diffstat | 2 files changed, 65 insertions(+), 33 deletions(-) [+] |
line wrap: on
line diff
--- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,11 @@ +2005-08-29 Simon Josefsson <jas@extundo.com> + + * iconvme.c: Fix errno. + +2005-08-29 "Oskar Liljeblad" <oskar@osk.mine.nu> + + * iconvme.c: Split iconv_string into iconv_alloc. + 2005-08-27 Jim Meyering <jim@meyering.net> * fopen-safer.c: Merge minor changes from coreutils.
--- a/lib/iconvme.c +++ b/lib/iconvme.c @@ -63,8 +63,54 @@ char *dest = NULL; #if HAVE_ICONV iconv_t cd; +#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); + + { + int save_errno = errno; + + if (iconv_close (cd) < 0 && dest) + { + int save_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 = save_errno2; + } + else + errno = save_errno; + } +#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; + char *p = (char *) str; char *outp; - char *p = (char *) str; size_t inbytes_remaining = strlen (p); /* Guess the maximum length the output string can have. */ size_t outbuf_size = inbytes_remaining + 1; @@ -79,24 +125,15 @@ if (outbuf_size <= approx_sqrt_SIZE_MAX / MB_LEN_MAX) outbuf_size *= MB_LEN_MAX; outbytes_remaining = outbuf_size - 1; -#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; outp = dest = (char *) malloc (outbuf_size); if (dest == NULL) - goto out; + return NULL; again: err = iconv (cd, &p, &inbytes_remaining, &outp, &outbytes_remaining); - if (err == (size_t) - 1) + if (err == (size_t) -1) { switch (errno) { @@ -145,27 +182,14 @@ *outp = '\0'; out: - { - int save_errno = errno; - - if (iconv_close (cd) < 0 && !have_error) - { - /* If we didn't have a real error before, make sure we restore - the iconv_close error below. */ - save_errno = errno; - have_error = 1; - } - - if (have_error && dest) - { - free (dest); - dest = NULL; - errno = save_errno; - } - } -#else - errno = ENOSYS; -#endif + if (have_error && dest) + { + int save_errno = errno; + free(dest); + errno = save_errno; + dest = NULL; + } return dest; } +#endif