libexplain  1.4.D001
libexplain/output/error_and_die.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - a library of system-call-specific strerror replacements
00003  * Copyright (C) 2010-2013 Peter Miller
00004  * Written by Peter Miller <pmiller@opensource.org.au>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU Lesser General Public License as
00008  * published by the Free Software Foundation; either version 3 of the
00009  * License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #include <libexplain/ac/limits.h> /* for PATH_MAX on Solaris */
00021 #include <libexplain/ac/stdarg.h>
00022 #include <libexplain/ac/stdio.h>
00023 #include <libexplain/ac/string.h>
00024 #include <libexplain/ac/sys/param.h> /* for PATH_MAX except Solaris */
00025 
00026 #include <libexplain/option.h>
00027 #include <libexplain/output.h>
00028 #include <libexplain/program_name.h>
00029 #include <libexplain/string_buffer.h>
00030 
00031 
00032 void
00033 explain_output_error_and_die(const char *format, ...)
00034 {
00035     va_list         ap;
00036     explain_string_buffer_t sb;
00037 
00038     /*
00039      * Note: we can't use explain_common_message_buffer, just in case
00040      * one of the format argumnets *is* explain_common_message_buffer.
00041      * And for the same reason, we need to be about the same size.
00042      */
00043     char buf[PATH_MAX * 2 + 200];
00044 
00045     /*
00046      * See if we can just pass the text through, unchanged.
00047      */
00048     if (0 == strcmp(format, "%s") && !explain_option_assemble_program_name())
00049     {
00050         const char      *text;
00051 
00052         va_start(ap, format);
00053         text = va_arg(ap, const char *);
00054         va_end(ap);
00055         explain_output_message(text);
00056         explain_output_exit_failure();
00057         /* NOTREACHED */
00058     }
00059 
00060     explain_string_buffer_init(&sb, buf, sizeof(buf));
00061     if (explain_option_assemble_program_name())
00062     {
00063         const char      *prog;
00064 
00065         prog = explain_program_name_get();
00066         if (prog && *prog)
00067         {
00068             explain_string_buffer_puts(&sb, prog);
00069             explain_string_buffer_puts(&sb, ": ");
00070         }
00071     }
00072 
00073     va_start(ap, format);
00074     explain_string_buffer_vprintf(&sb, format, ap);
00075     va_end(ap);
00076 
00077     explain_output_message(buf);
00078     explain_output_exit_failure();
00079 }
00080 
00081 
00082 /* vim: set ts=8 sw=4 et : */