libexplain  1.4.D001
libexplain/buffer/errno/asprintf.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 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 #include <libexplain/ac/stdarg.h>
00021 
00022 #include <libexplain/buffer/errno/asprintf.h>
00023 #include <libexplain/buffer/errno/vsnprintf.h>
00024 #include <libexplain/buffer/is_the_null_pointer.h>
00025 #include <libexplain/buffer/pathname.h>
00026 #include <libexplain/buffer/pointer.h>
00027 #include <libexplain/explanation.h>
00028 
00029 
00030 static void
00031 explain_buffer_errno_asprintf_system_call(explain_string_buffer_t *sb,
00032     int errnum, char **data, const char *format, va_list ap)
00033 {
00034     (void)errnum;
00035     (void)ap;
00036     explain_string_buffer_puts(sb, "asprintf(data = ");
00037     explain_buffer_pointer(sb, data);
00038     explain_string_buffer_puts(sb, ", format = ");
00039     explain_buffer_pathname(sb, format);
00040 
00041     /*
00042      * In theory we could print all of the arguments, because we have the
00043      * format string to tell us their types.  But that's a lot of work,
00044      * I'll do if someone-asks nicely.
00045      */
00046     explain_string_buffer_puts(sb, ", ...");
00047 }
00048 
00049 
00050 void
00051 explain_buffer_errno_asprintf_explanation(explain_string_buffer_t *sb,
00052     int errnum, const char *syscall_name, char **data, const char *format,
00053     va_list ap)
00054 {
00055     char            dummy[20];
00056 
00057     if (!data)
00058     {
00059         explain_buffer_is_the_null_pointer(sb, "data");
00060         return;
00061         }
00062 
00063 /*/
00064     case ERANGE: stupid wiht or precision
00065     case ENOMEM: malloc barfed
00066     case EINVAL: data or format was NULL
00067     case EBADF: never, but happens when pint to a file, in the common code
00068 /*/
00069 
00070     explain_buffer_errno_vsnprintf_explanation
00071     (
00072         sb,
00073         errnum,
00074         syscall_name,
00075         dummy,
00076         sizeof(dummy),
00077         format,
00078         ap
00079     );
00080 }
00081 
00082 
00083 void
00084 explain_buffer_errno_asprintf(explain_string_buffer_t *sb, int errnum,
00085     char **data, const char *format, va_list ap)
00086 {
00087     explain_explanation_t exp;
00088 
00089     explain_explanation_init(&exp, errnum);
00090     explain_buffer_errno_asprintf_system_call
00091     (
00092         &exp.system_call_sb,
00093         errnum,
00094         data,
00095         format,
00096         ap
00097     );
00098     explain_buffer_errno_asprintf_explanation
00099     (
00100         &exp.explanation_sb,
00101         errnum,
00102         "asprintf",
00103         data,
00104         format,
00105         ap
00106     );
00107     explain_explanation_assemble(&exp, sb);
00108 }
00109 
00110 
00111 /* vim: set ts=8 sw=4 et : */