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