libexplain  1.4.D001
libexplain/buffer/errno/fread.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/fread.h>
00023 #include <libexplain/buffer/errno/read.h>
00024 #include <libexplain/buffer/is_the_null_pointer.h>
00025 #include <libexplain/buffer/pointer.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_fread_system_call(explain_string_buffer_t *sb,
00035     int errnum, void *ptr, size_t size, size_t nmemb, FILE *fp)
00036 {
00037     (void)errnum;
00038     explain_string_buffer_puts(sb, "fread(ptr = ");
00039     explain_buffer_pointer(sb, ptr);
00040     explain_string_buffer_printf(sb, ", size = %ld", (long)size);
00041     explain_string_buffer_printf(sb, ", nmemb = %ld", (long)nmemb);
00042     explain_string_buffer_puts(sb, ", fp = ");
00043     explain_buffer_stream(sb, fp);
00044     explain_string_buffer_putc(sb, ')');
00045 }
00046 
00047 
00048 static void
00049 explain_buffer_errno_fread_explanation(explain_string_buffer_t *sb,
00050     int errnum, void *ptr, size_t size, size_t nmemb, FILE *fp)
00051 {
00052     int             fildes;
00053     size_t          nbytes;
00054 
00055     if (fp == NULL)
00056     {
00057         explain_buffer_is_the_null_pointer(sb, "fp");
00058         return;
00059     }
00060 
00061     fildes = explain_stream_to_fildes(fp);
00062     if (errnum == EBADF)
00063     {
00064         /*
00065          * The underlying fildes could be open read/write but the FILE
00066          * may not be open for writing.
00067          */
00068         if (explain_libio_no_writes(fp))
00069         {
00070             explain_buffer_ebadf_not_open_for_writing(sb, "fp", -1);
00071             explain_buffer_software_error(sb);
00072             return;
00073         }
00074         explain_buffer_ebadf(sb, fildes, "fp");
00075         return;
00076     }
00077 
00078     nbytes = size * nmemb;
00079     explain_buffer_errno_read_explanation
00080     (
00081         sb,
00082         errnum,
00083         "fread",
00084         fildes,
00085         ptr,
00086         nbytes
00087     );
00088 }
00089 
00090 
00091 void
00092 explain_buffer_errno_fread(explain_string_buffer_t *sb, int errnum,
00093     void *ptr, size_t size, size_t nmemb, FILE *fp)
00094 {
00095     explain_explanation_t exp;
00096 
00097     explain_explanation_init(&exp, errnum);
00098     explain_buffer_errno_fread_system_call
00099     (
00100         &exp.system_call_sb,
00101         errnum,
00102         ptr,
00103         size,
00104         nmemb,
00105         fp
00106     );
00107     explain_buffer_errno_fread_explanation
00108     (
00109         &exp.explanation_sb,
00110         errnum,
00111         ptr,
00112         size,
00113         nmemb,
00114         fp
00115     );
00116     explain_explanation_assemble(&exp, sb);
00117 }
00118 
00119 
00120 /* vim: set ts=8 sw=4 et : */