libexplain  1.4.D001
libexplain/buffer/errno/strtod.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2009, 2011, 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,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser 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/stdlib.h>
00021 
00022 #include <libexplain/buffer/einval.h>
00023 #include <libexplain/buffer/erange.h>
00024 #include <libexplain/buffer/errno/generic.h>
00025 #include <libexplain/buffer/errno/strtod.h>
00026 #include <libexplain/buffer/gettext.h>
00027 #include <libexplain/buffer/pathname.h>
00028 #include <libexplain/buffer/pointer.h>
00029 #include <libexplain/explanation.h>
00030 #include <libexplain/is_efault.h>
00031 
00032 
00033 static void
00034 explain_buffer_errno_strtod_system_call(explain_string_buffer_t *sb, int errnum,
00035     const char *nptr, char **endptr)
00036 {
00037     (void)errnum;
00038     explain_string_buffer_puts(sb, "strtod(nptr = ");
00039     explain_buffer_pathname(sb, nptr);
00040     explain_string_buffer_puts(sb, ", endptr = ");
00041     explain_buffer_pointer(sb, endptr);
00042     explain_string_buffer_putc(sb, ')');
00043 }
00044 
00045 
00046 static void
00047 explain_buffer_errno_strtod_explanation(explain_string_buffer_t *sb, int errnum,
00048     const char *nptr, char **endptr)
00049 {
00050     /*
00051      * http://www.opengroup.org/onlinepubs/009695399/functions/strtod.html
00052      */
00053     switch (errnum)
00054     {
00055     case EINVAL:
00056         if (endptr && !explain_is_efault_pointer(endptr, sizeof(*endptr)))
00057         {
00058             if (*endptr == nptr)
00059             {
00060                 explain_buffer_einval_not_a_number(sb, "nptr");
00061                 break;
00062             }
00063         }
00064         else
00065         {
00066             char            *ep;
00067             int             err;
00068 
00069             /*
00070              * Do the conversion again, this time *with* an end pointer,
00071              * to see if there were any digits recognised.
00072              */
00073             ep = 0;
00074             err = errno;
00075             if (strtod(nptr, &ep))
00076             {
00077                 /*
00078                  * Ordinarily, the warning "ignoring return value of function"
00079                  * is a great way of discovering bugs.  In this case, however,
00080                  * we aren't concerned with the actual return value, but with
00081                  * the error that results from the call.
00082                  */
00083             }
00084             errno = err;
00085             if (ep == nptr)
00086             {
00087                 explain_buffer_einval_not_a_number(sb, "nptr");
00088                 break;
00089             }
00090         }
00091         goto generic;
00092 
00093     case ERANGE:
00094         explain_buffer_erange(sb);
00095         break;
00096 
00097     default:
00098         generic:
00099         explain_buffer_errno_generic(sb, errnum, "strtod");
00100         break;
00101     }
00102 }
00103 
00104 
00105 void
00106 explain_buffer_errno_strtod(explain_string_buffer_t *sb, int errnum, const char
00107     *nptr, char **endptr)
00108 {
00109     explain_explanation_t exp;
00110 
00111     explain_explanation_init(&exp, errnum);
00112     explain_buffer_errno_strtod_system_call(&exp.system_call_sb, errnum, nptr,
00113         endptr);
00114     explain_buffer_errno_strtod_explanation(&exp.explanation_sb, errnum, nptr,
00115         endptr);
00116     explain_explanation_assemble(&exp, sb);
00117 }
00118 
00119 
00120 /* vim: set ts=8 sw=4 et : */