libexplain  1.4.D001
libexplain/snprintf_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/snprintf.h>
00025 #include <libexplain/buffer/errno/snprintf.h>
00026 
00027 
00028 int
00029 explain_snprintf_or_die(char *data, size_t data_size, const char *format, ...)
00030 {
00031     int             hold_errno;
00032     va_list         ap;
00033     va_list         ap2;
00034     int             result;
00035 
00036     va_start(ap, format);
00037     va_copy(ap2, ap); /* See comment in libexplain/fprintf_or_die.c */
00038     hold_errno = errno;
00039     errno = EINVAL;
00040     result = vsnprintf(data, data_size, format, ap);
00041     if (result < 0)
00042     {
00043         explain_string_buffer_t sb;
00044 
00045         hold_errno = errno;
00046         explain_string_buffer_init
00047         (
00048             &sb,
00049             explain_common_message_buffer,
00050             explain_common_message_buffer_size
00051         );
00052         /* can't re-use "ap" here, this is why we prepped "ap2" earlier */
00053         explain_buffer_errno_snprintf
00054         (
00055             &sb,
00056             hold_errno,
00057             data,
00058             data_size,
00059             format,
00060             ap2
00061         );
00062         explain_output_error("%s", explain_common_message_buffer);
00063         explain_output_exit_failure();
00064     }
00065     va_end(ap2); /* yes, both of them */
00066     va_end(ap);
00067     errno = hold_errno;
00068     return result;
00069 }
00070 
00071 
00072 int
00073 explain_snprintf_on_error(char *data, size_t data_size, const char *format, ...)
00074 {
00075     int             hold_errno;
00076     int             result;
00077     va_list         ap;
00078     va_list         ap2;
00079 
00080     va_start(ap, format);
00081     va_copy(ap2, ap); /* see comments in explain_fprintf_or_die */
00082 
00083     hold_errno = errno;
00084     errno = EINVAL;
00085     result = vsnprintf(data, data_size, format, ap);
00086     if (result < 0)
00087     {
00088         explain_string_buffer_t sb;
00089 
00090         hold_errno = errno;
00091         explain_string_buffer_init
00092         (
00093             &sb,
00094             explain_common_message_buffer,
00095             explain_common_message_buffer_size
00096         );
00097         explain_buffer_errno_snprintf
00098         (
00099             &sb,
00100             hold_errno,
00101             data,
00102             data_size,
00103             format,
00104             ap2
00105         );
00106         explain_output_error("%s", explain_common_message_buffer);
00107     }
00108     va_end(ap2); /* yes, both of them */
00109     va_end(ap);
00110     errno = hold_errno;
00111     return result;
00112 }
00113 
00114 
00115 /* vim: set ts=8 sw=4 et : */