libexplain  1.4.D001
libexplain/buffer/errno/snprintf.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2010, 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, 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 
00021 #include <libexplain/buffer/efault.h>
00022 #include <libexplain/buffer/einval.h>
00023 #include <libexplain/buffer/errno/generic.h>
00024 #include <libexplain/buffer/errno/snprintf.h>
00025 #include <libexplain/buffer/is_the_null_pointer.h>
00026 #include <libexplain/buffer/pathname.h>
00027 #include <libexplain/buffer/pointer.h>
00028 #include <libexplain/buffer/size_t.h>
00029 #include <libexplain/explanation.h>
00030 #include <libexplain/is_efault.h>
00031 #include <libexplain/printf_format.h>
00032 
00033 
00034 static void
00035 explain_buffer_errno_snprintf_system_call(explain_string_buffer_t *sb,
00036     int errnum, char *data, size_t data_size, const char *format, va_list ap)
00037 {
00038     (void)errnum;
00039     explain_string_buffer_puts(sb, "snprintf(data = ");
00040     explain_buffer_pointer(sb, data);
00041     explain_string_buffer_puts(sb, ", data_size = ");
00042     explain_buffer_size_t(sb, data_size);
00043     explain_string_buffer_puts(sb, ", format = ");
00044     explain_buffer_pathname(sb, format);
00045     if (format && !explain_is_efault_string(format))
00046         explain_printf_format_representation(sb, format, ap);
00047     explain_string_buffer_putc(sb, ')');
00048 }
00049 
00050 
00051 void
00052 explain_buffer_errno_snprintf_explanation(explain_string_buffer_t *sb,
00053     int errnum, const char *syscall_name, char *data, size_t data_size,
00054     const char *format, va_list ap)
00055 {
00056     int             cur_idx;
00057     size_t          j;
00058     explain_printf_format_list_t specs;
00059     size_t          errpos;
00060 
00061     /*
00062      * http://www.opengroup.org/onlinepubs/009695399/functions/snprintf.html
00063      */
00064     (void)errnum;
00065     (void)syscall_name;
00066     (void)data_size;
00067     (void)ap;
00068     if (!data)
00069     {
00070         explain_buffer_is_the_null_pointer(sb, "data");
00071         return;
00072     }
00073     if (!format)
00074     {
00075         explain_buffer_is_the_null_pointer(sb, "format");
00076         return;
00077     }
00078     if (explain_is_efault_string(format))
00079     {
00080         explain_buffer_efault(sb, "format");
00081         return;
00082     }
00083 
00084     /*
00085      * Check the format string.
00086      * All the fugly stuff is hidden in explain_printf_format().
00087      */
00088     explain_printf_format_list_constructor(&specs);
00089     errpos = explain_printf_format(format, &specs);
00090     if (errpos > 0)
00091     {
00092         explain_buffer_einval_format_string(sb, "format", format, errpos);
00093         explain_printf_format_list_destructor(&specs);
00094         return;
00095     }
00096     explain_printf_format_list_sort(&specs);
00097     /* duplicates are OK, holes are not */
00098     cur_idx = 0;
00099     for (j = 0; j < specs.length; ++j)
00100     {
00101         int             idx;
00102 
00103         idx = specs.list[j].index;
00104         if (idx > cur_idx)
00105         {
00106             /* we found a hole */
00107             explain_buffer_einval_format_string_hole
00108             (
00109                 sb,
00110                 "format",
00111                 cur_idx + 1
00112             );
00113             explain_printf_format_list_destructor(&specs);
00114             return;
00115         }
00116         if (idx == cur_idx)
00117             ++cur_idx;
00118     }
00119     explain_printf_format_list_destructor(&specs);
00120 
00121     /*
00122      * Meh.
00123      */
00124     explain_buffer_einval_vague(sb, "format");
00125 }
00126 
00127 
00128 void
00129 explain_buffer_errno_snprintf(explain_string_buffer_t *sb, int errnum,
00130     char *data, size_t data_size, const char *format, va_list ap)
00131 {
00132     explain_explanation_t exp;
00133 
00134     explain_explanation_init(&exp, errnum);
00135     explain_buffer_errno_snprintf_system_call(&exp.system_call_sb, errnum, data,
00136         data_size, format, ap);
00137     explain_buffer_errno_snprintf_explanation(&exp.explanation_sb, errnum,
00138         "snprintf", data, data_size, format, ap);
00139     explain_explanation_assemble(&exp, sb);
00140 }
00141 
00142 
00143 /* vim: set ts=8 sw=4 et : */