libexplain  1.4.D001
libexplain/buffer/errno/sprintf.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/limits.h>
00020 
00021 #include <libexplain/buffer/dangerous.h>
00022 #include <libexplain/buffer/errno/snprintf.h>
00023 #include <libexplain/buffer/errno/sprintf.h>
00024 #include <libexplain/buffer/pathname.h>
00025 #include <libexplain/buffer/pointer.h>
00026 #include <libexplain/explanation.h>
00027 #include <libexplain/is_efault.h>
00028 #include <libexplain/printf_format.h>
00029 
00030 
00031 static void
00032 explain_buffer_errno_sprintf_system_call(explain_string_buffer_t *sb, int
00033     errnum, char *data, const char *format, va_list ap)
00034 {
00035     (void)errnum;
00036     explain_string_buffer_puts(sb, "sprintf(data = ");
00037     explain_buffer_pointer(sb, data);
00038     explain_string_buffer_puts(sb, ", format = ");
00039     explain_buffer_pathname(sb, format);
00040     if (format && !explain_is_efault_string(format))
00041         explain_printf_format_representation(sb, format, ap);
00042     explain_string_buffer_putc(sb, ')');
00043 
00044     explain_buffer_dangerous_system_call2(sb->footnotes, "sprintf", "snprintf");
00045 }
00046 
00047 
00048 void
00049 explain_buffer_errno_sprintf_explanation(explain_string_buffer_t *sb, int
00050     errnum, const char *syscall_name, char *data, const char *format,
00051     va_list ap)
00052 {
00053     /*
00054      * http://www.opengroup.org/onlinepubs/009695399/functions/sprintf.html
00055      */
00056     explain_buffer_errno_snprintf_explanation
00057     (
00058         sb,
00059         errnum,
00060         syscall_name,
00061         data,
00062         INT_MAX, /* this is why it's dangerous */
00063         format,
00064         ap
00065     );
00066 }
00067 
00068 
00069 void
00070 explain_buffer_errno_sprintf(explain_string_buffer_t *sb, int errnum, char
00071     *data, const char *format, va_list ap)
00072 {
00073     explain_explanation_t exp;
00074 
00075     explain_explanation_init(&exp, errnum);
00076     explain_buffer_errno_sprintf_system_call
00077     (
00078         &exp.system_call_sb,
00079         errnum,
00080         data,
00081         format,
00082         ap
00083     );
00084     explain_buffer_errno_sprintf_explanation
00085     (
00086         &exp.explanation_sb,
00087         errnum,
00088         "sprintf",
00089         data,
00090         format,
00091         ap
00092     );
00093     explain_explanation_assemble(&exp, sb);
00094 }
00095 
00096 
00097 /* vim: set ts=8 sw=4 et : */