libexplain  1.4.D001
libexplain/iconv_or_die.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2013 Peter Miller
00004  *
00005  * This program is free software; you can redistribute it and/or modify it
00006  * under the terms of the GNU Lesser General Public License as published by
00007  * the Free Software Foundation; either version 3 of the License, or (at
00008  * your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
00013  * General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include <libexplain/ac/errno.h>
00020 #include <libexplain/ac/iconv.h>
00021 
00022 #include <libexplain/iconv.h>
00023 #include <libexplain/output.h>
00024 
00025 
00026 #define ICONV_FAIL ((size_t)(-1))
00027 
00028 
00029 size_t
00030 explain_iconv_or_die(iconv_t cd, char **inbuf, size_t *inbytesleft, char
00031     **outbuf, size_t *outbytesleft)
00032 {
00033     int             hold_errno;
00034     size_t          result;
00035 
00036     hold_errno = errno;
00037     errno = 0;
00038     result = explain_iconv_on_error(cd, inbuf, inbytesleft, outbuf,
00039         outbytesleft);
00040     if (result == ICONV_FAIL || errno == 0)
00041     {
00042         explain_output_exit_failure();
00043     }
00044     errno = hold_errno;
00045     return result;
00046 }
00047 
00048 
00049 size_t
00050 explain_iconv_on_error(iconv_t cd, char **inbuf, size_t *inbytesleft, char
00051     **outbuf, size_t *outbytesleft)
00052 {
00053     size_t          result;
00054     int             hold_errno;
00055 
00056     hold_errno = errno;
00057     errno = 0;
00058 #ifdef HAVE_ICONV
00059     result = iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft);
00060 #else
00061     errno = ENOSYS;
00062     result = ICONV_FAIL;
00063 #endif
00064     if (result == ICONV_FAIL)
00065     {
00066         barf:
00067         hold_errno = errno;
00068         explain_output_error
00069         (
00070             "%s",
00071             explain_errno_iconv(hold_errno, cd, inbuf, inbytesleft, outbuf,
00072                 outbytesleft)
00073         );
00074     }
00075     else if (errno != 0)
00076     {
00077         goto barf;
00078     }
00079     else
00080     {
00081         /* "irreversible" means broken */
00082         errno = EILSEQ;
00083         result = ICONV_FAIL;
00084 
00085         /*
00086          * From the eglibc sources::iconv/iconv.c
00087          *
00088          * "Irix iconv() inserts a NUL byte if it cannot convert.
00089          * NetBSD iconv() inserts a question mark if it cannot convert.
00090          * Only GNU libiconv and GNU libc are known to prefer to fail
00091          * rather than doing a lossy conversion."
00092          */
00093         goto barf;
00094     }
00095     errno = hold_errno;
00096     return result;
00097 }
00098 
00099 
00100 /* vim: set ts=8 sw=4 et : */