libexplain  1.4.D001
libexplain/printf_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/stdarg.h>
00021 #include <libexplain/ac/stdio.h>
00022 
00023 #include <libexplain/buffer/errno/printf.h>
00024 #include <libexplain/common_message_buffer.h>
00025 #include <libexplain/printf.h>
00026 #include <libexplain/output.h>
00027 #include <libexplain/string_buffer.h>
00028 
00029 
00030 int
00031 explain_printf_or_die(const char *format, ...)
00032 {
00033     int             hold_errno;
00034     va_list         ap;
00035     va_list         ap2;
00036     int             result;
00037 
00038     va_start(ap, format);
00039     va_copy(ap2, ap); /* see comments in explain_fprintf_or_die */
00040 
00041     hold_errno = errno;
00042     errno = EINVAL;
00043     result = vprintf(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_printf(&sb, hold_errno, 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_printf_on_error(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 = vprintf(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_printf(&sb, hold_errno, 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 : */