libexplain  1.4.D001
libexplain/buffer/errno/fputc.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2008-2011, 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,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser 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 
00021 #include <libexplain/buffer/char.h>
00022 #include <libexplain/buffer/ebadf.h>
00023 #include <libexplain/buffer/errno/generic.h>
00024 #include <libexplain/buffer/errno/fputc.h>
00025 #include <libexplain/buffer/errno/write.h>
00026 #include <libexplain/buffer/software_error.h>
00027 #include <libexplain/buffer/stream.h>
00028 #include <libexplain/explanation.h>
00029 #include <libexplain/libio.h>
00030 #include <libexplain/stream_to_fildes.h>
00031 
00032 
00033 static void
00034 explain_buffer_errno_fputc_system_call(explain_string_buffer_t *sb,
00035     int errnum, int c, FILE *fp)
00036 {
00037     (void)errnum;
00038     explain_string_buffer_puts(sb, "fputc(c = ");
00039     explain_buffer_char(sb, c);
00040     explain_string_buffer_puts(sb, ", fp = ");
00041     explain_buffer_stream(sb, fp);
00042     explain_string_buffer_putc(sb, ')');
00043 }
00044 
00045 
00046 void
00047 explain_buffer_errno_fputc_explanation(explain_string_buffer_t *sb,
00048     int errnum, const char *syscall_name, int c, FILE *fp)
00049 {
00050     int             fildes;
00051 
00052     /*
00053      * http://www.opengroup.org/onlinepubs/009695399/functions/fputc.html
00054      */
00055     (void)c;
00056 
00057     if (errnum == EBADF)
00058     {
00059         /*
00060          * The underlying fildes could be open read/write but the FILE
00061          * may not be open for writing.
00062          */
00063         if (explain_libio_no_writes(fp))
00064         {
00065             explain_buffer_ebadf_not_open_for_writing(sb, "fp", -1);
00066             explain_buffer_software_error(sb);
00067             return;
00068         }
00069     }
00070 
00071     fildes = explain_stream_to_fildes(fp);
00072     explain_buffer_errno_write_explanation
00073     (
00074         sb,
00075         errnum,
00076         syscall_name,
00077         fildes,
00078         NULL,
00079         0
00080     );
00081 }
00082 
00083 
00084 void
00085 explain_buffer_errno_fputc(explain_string_buffer_t *sb, int errnum, int c,
00086     FILE *fp)
00087 {
00088     explain_explanation_t exp;
00089 
00090     explain_explanation_init(&exp, errnum);
00091     explain_buffer_errno_fputc_system_call
00092     (
00093         &exp.system_call_sb,
00094         errnum,
00095         c,
00096         fp
00097     );
00098     explain_buffer_errno_fputc_explanation
00099     (
00100         &exp.explanation_sb,
00101         errnum,
00102         "fputc",
00103         c,
00104         fp
00105     );
00106     explain_explanation_assemble(&exp, sb);
00107 }
00108 
00109 
00110 /* vim: set ts=8 sw=4 et : */