libexplain
1.4.D001
|
00001 /* 00002 * libexplain - Explain errno values returned by libc functions 00003 * Copyright (C) 2008-2010, 2012, 2013 Peter Miller 00004 * Written by Peter Miller <pmiller@opensource.org.au> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU Lesser General Public License as 00008 * published by the Free Software Foundation; either version 3 of the 00009 * License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public License 00017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 #include <libexplain/ac/errno.h> 00021 #include <libexplain/ac/fcntl.h> 00022 #include <libexplain/ac/stdio.h> 00023 00024 #include <libexplain/fclose.h> 00025 #include <libexplain/fflush.h> 00026 #include <libexplain/output.h> 00027 #include <libexplain/stream_to_fildes.h> 00028 00029 00030 static int 00031 open_for_writing(FILE *fp) 00032 { 00033 int fildes; 00034 int flags; 00035 00036 fildes = explain_stream_to_fildes(fp); 00037 flags = fcntl(fildes, F_GETFL); 00038 if (flags < 0) 00039 return 0; 00040 switch (flags & O_ACCMODE) 00041 { 00042 case O_WRONLY: 00043 case O_RDWR: 00044 return 1; 00045 00046 default: 00047 break; 00048 } 00049 return 0; 00050 } 00051 00052 00053 int 00054 explain_fclose_on_error(FILE *fp) 00055 { 00056 int result; 00057 00058 if (open_for_writing(fp)) 00059 { 00060 /* only flush output streams */ 00061 result = explain_fflush_on_error(fp); 00062 if (result < 0) 00063 { 00064 int hold_errno; 00065 00066 /* 00067 * FIXME: it would be better if we could nominate a different 00068 * syscall_name for this error message, rather than surprising 00069 * the user (or developer reading the bug report) with "fflush". 00070 */ 00071 hold_errno = errno; 00072 explain_output_error("%s", explain_errno_fflush(hold_errno, fp)); 00073 fclose(fp); 00074 errno = hold_errno; 00075 return result; 00076 } 00077 } 00078 00079 result = fclose(fp); 00080 if (result < 0) 00081 { 00082 int hold_errno; 00083 00084 hold_errno = errno; 00085 explain_output_error("%s", explain_errno_fclose(hold_errno, fp)); 00086 errno = hold_errno; 00087 } 00088 return result; 00089 } 00090 00091 00092 void 00093 explain_fclose_or_die(FILE *fp) 00094 { 00095 if (explain_fclose_on_error(fp)) 00096 { 00097 explain_output_exit_failure(); 00098 } 00099 } 00100 00101 00102 /* vim: set ts=8 sw=4 et : */