libexplain  1.4.D001
libexplain/buffer/errno/fgets.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/generic.h>
00023 #include <libexplain/buffer/errno/fgets.h>
00024 #include <libexplain/buffer/errno/read.h>
00025 #include <libexplain/buffer/gettext.h>
00026 #include <libexplain/buffer/is_the_null_pointer.h>
00027 #include <libexplain/buffer/pointer.h>
00028 #include <libexplain/buffer/software_error.h>
00029 #include <libexplain/buffer/stream.h>
00030 #include <libexplain/explanation.h>
00031 #include <libexplain/libio.h>
00032 #include <libexplain/stream_to_fildes.h>
00033 
00034 
00035 static void
00036 explain_buffer_errno_fgets_system_call(explain_string_buffer_t *sb,
00037     int errnum, char *data, int data_size, FILE *fp)
00038 {
00039     (void)errnum;
00040     explain_string_buffer_puts(sb, "fgets(data = ");
00041     explain_buffer_pointer(sb, data);
00042     explain_string_buffer_printf(sb, ", data_size = %d", data_size);
00043     explain_string_buffer_puts(sb, ", fp = ");
00044     explain_buffer_stream(sb, fp);
00045     explain_string_buffer_putc(sb, ')');
00046 }
00047 
00048 
00049 static void
00050 explain_buffer_errno_fgets_explanation(explain_string_buffer_t *sb,
00051     int errnum, char *data, int data_size, FILE *fp)
00052 {
00053     if (!fp)
00054     {
00055         explain_buffer_is_the_null_pointer(sb, "fp");
00056         return;
00057     }
00058 
00059     if (errnum == EBADF)
00060     {
00061         /*
00062          * The underlying fildes could be open read/write but the FILE
00063          * may not be open for writing.
00064          */
00065         if (explain_libio_no_reads(fp))
00066         {
00067             explain_buffer_ebadf_not_open_for_reading(sb, "fp", -1);
00068             explain_buffer_software_error(sb);
00069             return;
00070         }
00071     }
00072 
00073     explain_buffer_errno_read_explanation
00074     (
00075         sb,
00076         errnum,
00077         "fgets",
00078         explain_stream_to_fildes(fp),
00079         data,
00080         data_size
00081     );
00082 }
00083 
00084 
00085 void
00086 explain_buffer_errno_fgets(explain_string_buffer_t *sb, int errnum,
00087     char *data, int data_size, FILE *fp)
00088 {
00089     explain_explanation_t exp;
00090 
00091     explain_explanation_init(&exp, errnum);
00092     explain_buffer_errno_fgets_system_call
00093     (
00094         &exp.system_call_sb,
00095         errnum,
00096         data,
00097         data_size,
00098         fp
00099     );
00100     explain_buffer_errno_fgets_explanation
00101     (
00102         &exp.explanation_sb,
00103         errnum,
00104         data,
00105         data_size,
00106         fp
00107     );
00108     explain_explanation_assemble(&exp, sb);
00109 }
00110 
00111 
00112 /* vim: set ts=8 sw=4 et : */