libexplain  1.4.D001
libexplain/buffer/errno/strtoul.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/strtoul.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_strtoul_system_call(explain_string_buffer_t *sb,
00035     int errnum, const char *nptr, char **endptr, int base)
00036 {
00037     (void)errnum;
00038     explain_string_buffer_puts(sb, "strtoul(nptr = ");
00039     explain_buffer_pathname(sb, nptr);
00040     explain_string_buffer_puts(sb, ", endptr = ");
00041     explain_buffer_pointer(sb, endptr);
00042     explain_string_buffer_puts(sb, ", base = ");
00043     explain_string_buffer_printf(sb, "%d", base);
00044     explain_string_buffer_putc(sb, ')');
00045 }
00046 
00047 
00048 static void
00049 explain_buffer_errno_strtoul_explanation(explain_string_buffer_t *sb,
00050     int errnum, const char *nptr, char **endptr, int base)
00051 {
00052     /*
00053      * http://www.opengroup.org/onlinepubs/009695399/functions/strtoul.html
00054      */
00055     switch (errnum)
00056     {
00057     case EINVAL:
00058         if (base != 0 && (base < 2 || base > 26))
00059         {
00060             explain_buffer_einval_vague(sb, "base");
00061             break;
00062         }
00063         if (endptr && !explain_is_efault_pointer(endptr, sizeof(*endptr)))
00064         {
00065             if (*endptr == nptr)
00066             {
00067                 explain_buffer_einval_not_a_number(sb, "nptr");
00068                 break;
00069             }
00070         }
00071         else
00072         {
00073             char            *ep;
00074             int             err;
00075 
00076             /*
00077              * Do the conversion again, this time *with* an end pointer,
00078              * to see if there were any digits recognised.
00079              */
00080             ep = 0;
00081             err = errno;
00082             if (strtoul(nptr, &ep, base))
00083             {
00084                 /*
00085                  * Ordinarily, the warning "ignoring return value of function"
00086                  * is a great way of discovering bugs.  In this case, however,
00087                  * we aren't concerned with the actual return value, but with
00088                  * the error that results from the call.
00089                  */
00090             }
00091             errno = err;
00092             if (ep == nptr)
00093             {
00094                 explain_buffer_einval_not_a_number(sb, "nptr");
00095                 break;
00096             }
00097         }
00098         goto generic;
00099 
00100     case ERANGE:
00101         explain_buffer_erange(sb);
00102         break;
00103 
00104     default:
00105         generic:
00106         explain_buffer_errno_generic(sb, errnum, "strtoul");
00107         break;
00108     }
00109 }
00110 
00111 
00112 void
00113 explain_buffer_errno_strtoul(explain_string_buffer_t *sb, int errnum,
00114     const char *nptr, char **endptr, int base)
00115 {
00116     explain_explanation_t exp;
00117 
00118     explain_explanation_init(&exp, errnum);
00119     explain_buffer_errno_strtoul_system_call(&exp.system_call_sb, errnum, nptr,
00120         endptr, base);
00121     explain_buffer_errno_strtoul_explanation(&exp.explanation_sb, errnum, nptr,
00122         endptr, base);
00123     explain_explanation_assemble(&exp, sb);
00124 }
00125 
00126 
00127 /* vim: set ts=8 sw=4 et : */