libexplain  1.4.D001
libexplain/buffer/errno/ferror.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2008, 2009, 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 #include <libexplain/ac/fcntl.h>
00021 
00022 #include <libexplain/buffer/errno/ferror.h>
00023 #include <libexplain/buffer/errno/read.h>
00024 #include <libexplain/buffer/errno/write.h>
00025 #include <libexplain/buffer/is_the_null_pointer.h>
00026 #include <libexplain/buffer/stream.h>
00027 #include <libexplain/explanation.h>
00028 #include <libexplain/stream_to_fildes.h>
00029 
00030 
00031 static void
00032 explain_buffer_errno_ferror_system_call(explain_string_buffer_t *sb,
00033     int errnum, FILE *fp)
00034 {
00035     (void)errnum;
00036     explain_string_buffer_puts(sb, "ferror(fp = ");
00037     explain_buffer_stream(sb, fp);
00038     explain_string_buffer_putc(sb, ')');
00039 }
00040 
00041 
00042 static void
00043 explain_buffer_errno_ferror_explanation(explain_string_buffer_t *sb,
00044     int errnum, FILE *fp)
00045 {
00046     int             fildes;
00047     int             flags;
00048 
00049     /*
00050      * http://www.opengroup.org/onlinepubs/009695399/functions/ferror.html
00051      */
00052     if (fp == NULL)
00053     {
00054         explain_buffer_is_the_null_pointer(sb, "fp");
00055         return;
00056     }
00057 
00058     /*
00059      * probably had a problem reading or writing.
00060      * See if we can get a clue from the file flags.
00061      */
00062     fildes = explain_stream_to_fildes(fp);
00063     if (fildes < 0)
00064         goto ambiguous;
00065     flags = fcntl(fildes, F_GETFL);
00066     if (flags < 0)
00067         goto ambiguous;
00068     switch (flags & O_ACCMODE)
00069     {
00070     case O_RDONLY:
00071         read_error:
00072         explain_buffer_errno_read_explanation
00073         (
00074             sb,
00075             errnum,
00076             "ferror",
00077             fildes,
00078             NULL,
00079             0
00080         );
00081         break;
00082 
00083     case O_WRONLY:
00084         write_error:
00085         explain_buffer_errno_write_explanation
00086         (
00087             sb,
00088             errnum,
00089             "ferror",
00090             fildes,
00091             NULL,
00092             0
00093         );
00094         break;
00095 
00096     default:
00097         ambiguous:
00098         switch (errnum)
00099         {
00100         default:
00101             /*
00102              * If we still can't tell, assume they were writing
00103              * as this is more likely to have problems than reading.
00104              */
00105             goto write_error;
00106 
00107         case EAGAIN:
00108         case EBADF:
00109         case EFAULT:
00110         case EFBIG:
00111         case EINTR:
00112         case EINVAL:
00113         case EIO:
00114         case ENOSPC:
00115         case EPIPE:
00116             goto write_error;
00117 
00118 #if 0
00119         case EAGAIN:
00120         case EBADF:
00121         case EFAULT:
00122         case EINTR:
00123         case EINVAL:
00124         case EIO:
00125             /*
00126              * For ambiguous errno values, assume write error, see above
00127              * comment.
00128              */
00129             goto read_error;
00130 #endif
00131 
00132         case EISDIR:
00133             goto read_error;
00134         }
00135         break;
00136     }
00137 }
00138 
00139 
00140 void
00141 explain_buffer_errno_ferror(explain_string_buffer_t *sb, int errnum,
00142     FILE *fp)
00143 {
00144     explain_explanation_t exp;
00145 
00146     explain_explanation_init(&exp, errnum);
00147     explain_buffer_errno_ferror_system_call
00148     (
00149         &exp.system_call_sb,
00150         errnum,
00151         fp
00152     );
00153     explain_buffer_errno_ferror_explanation
00154     (
00155         &exp.explanation_sb,
00156         errnum,
00157         fp
00158     );
00159     explain_explanation_assemble(&exp, sb);
00160 }
00161 
00162 
00163 /* vim: set ts=8 sw=4 et : */