libexplain  1.4.D001
libexplain/buffer/errno/fwrite.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2008, 2009, 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/ebadf.h>
00022 #include <libexplain/buffer/errno/fwrite.h>
00023 #include <libexplain/buffer/errno/write.h>
00024 #include <libexplain/buffer/pointer.h>
00025 #include <libexplain/buffer/software_error.h>
00026 #include <libexplain/buffer/stream.h>
00027 #include <libexplain/explanation.h>
00028 #include <libexplain/libio.h>
00029 #include <libexplain/stream_to_fildes.h>
00030 
00031 
00032 static void
00033 explain_buffer_errno_fwrite_system_call(explain_string_buffer_t *sb,
00034     int errnum, const void *ptr, size_t size, size_t nmemb, FILE *fp)
00035 {
00036     (void)errnum;
00037     explain_string_buffer_puts(sb, "fwrite(ptr = ");
00038     explain_buffer_pointer(sb, ptr);
00039     explain_string_buffer_printf(sb, ", size = %ld", (long)size);
00040     explain_string_buffer_printf(sb, ", nmemb = %ld", (long)nmemb);
00041     explain_string_buffer_puts(sb, ", fp = ");
00042     explain_buffer_stream(sb, fp);
00043     explain_string_buffer_putc(sb, ')');
00044 }
00045 
00046 
00047 static void
00048 explain_buffer_errno_fwrite_explanation(explain_string_buffer_t *sb,
00049     int errnum, const void *ptr, size_t size, size_t nmemb, FILE *fp)
00050 {
00051     int             fildes;
00052 
00053     if (errnum == EBADF)
00054     {
00055         /*
00056          * The underlying fildes could be open read/write but the FILE
00057          * may not be open for writing.
00058          */
00059         if (explain_libio_no_writes(fp))
00060         {
00061             explain_buffer_ebadf_not_open_for_writing(sb, "fp", -1);
00062             explain_buffer_software_error(sb);
00063             return;
00064         }
00065     }
00066 
00067     fildes = explain_stream_to_fildes(fp);
00068     explain_buffer_errno_write_explanation
00069     (
00070         sb,
00071         errnum,
00072         "fwrite",
00073         fildes,
00074         ptr,
00075         size * nmemb
00076     );
00077 }
00078 
00079 
00080 void
00081 explain_buffer_errno_fwrite(explain_string_buffer_t *sb, int errnum,
00082     const void *ptr, size_t size, size_t nmemb, FILE *fp)
00083 {
00084     explain_explanation_t exp;
00085 
00086     explain_explanation_init(&exp, errnum);
00087     explain_buffer_errno_fwrite_system_call
00088     (
00089         &exp.system_call_sb,
00090         errnum,
00091         ptr,
00092         size,
00093         nmemb,
00094         fp
00095     );
00096     explain_buffer_errno_fwrite_explanation
00097     (
00098         &exp.explanation_sb,
00099         errnum,
00100         ptr,
00101         size,
00102         nmemb,
00103         fp
00104     );
00105     explain_explanation_assemble(&exp, sb);
00106 }
00107 
00108 
00109 /* vim: set ts=8 sw=4 et : */