libexplain  1.4.D001
libexplain/sprintf_or_die.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2010, 2012, 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/stdio.h>
00021 
00022 #include <libexplain/common_message_buffer.h>
00023 #include <libexplain/output.h>
00024 #include <libexplain/sprintf.h>
00025 #include <libexplain/buffer/errno/sprintf.h>
00026 
00027 
00028 #undef vsprintf
00029 
00030 
00031 int
00032 explain_sprintf_or_die(char *data, const char *format, ...)
00033 {
00034     int             hold_errno;
00035     va_list         ap;
00036     va_list         ap2;
00037     int             result;
00038 
00039     va_start(ap, format);
00040     va_copy(ap2, ap); /* See comment in libexplain/fprintf_or_die.c */
00041     hold_errno = errno;
00042     errno = EINVAL;
00043     result = vsprintf(data, format, ap);
00044     if (result < 0)
00045     {
00046         explain_string_buffer_t sb;
00047 
00048         hold_errno = errno;
00049         explain_string_buffer_init
00050         (
00051             &sb,
00052             explain_common_message_buffer,
00053             explain_common_message_buffer_size
00054         );
00055         /* can't re-use "ap" here, this is why we prepped "ap2" earlier */
00056         explain_buffer_errno_sprintf(&sb, hold_errno, data, format, ap2);
00057         explain_output_error("%s", explain_common_message_buffer);
00058         explain_output_exit_failure();
00059     }
00060     va_end(ap2); /* yes, both of them */
00061     va_end(ap);
00062     errno = hold_errno;
00063     return result;
00064 }
00065 
00066 
00067 int
00068 explain_sprintf_on_error(char *data, const char *format, ...)
00069 {
00070     int             hold_errno;
00071     int             result;
00072     va_list         ap;
00073     va_list         ap2;
00074 
00075     va_start(ap, format);
00076     va_copy(ap2, ap); /* see comments in explain_fprintf_or_die */
00077 
00078     hold_errno = errno;
00079     errno = EINVAL;
00080     result = vsprintf(data, format, ap);
00081     if (result < 0)
00082     {
00083         explain_string_buffer_t sb;
00084 
00085         hold_errno = errno;
00086         explain_string_buffer_init
00087         (
00088             &sb,
00089             explain_common_message_buffer,
00090             explain_common_message_buffer_size
00091         );
00092         explain_buffer_errno_sprintf(&sb, hold_errno, data, format, ap2);
00093         explain_output_error("%s", explain_common_message_buffer);
00094     }
00095     va_end(ap2); /* yes, both of them */
00096     va_end(ap);
00097     errno = hold_errno;
00098     return result;
00099 }
00100 
00101 
00102 /* vim: set ts=8 sw=4 et : */