libexplain
1.4.D001
|
00001 /* 00002 * libexplain - Explain errno values returned by libc functions 00003 * Copyright (C) 2008-2010, 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 00023 #include <libexplain/buffer/ebadf.h> 00024 #include <libexplain/buffer/errno/fclose.h> 00025 #include <libexplain/buffer/errno/fflush.h> 00026 #include <libexplain/buffer/errno/fopen.h> 00027 #include <libexplain/buffer/errno/freopen.h> 00028 #include <libexplain/buffer/note/underlying_fildes_open.h> 00029 #include <libexplain/buffer/stream.h> 00030 #include <libexplain/explanation.h> 00031 #include <libexplain/stream_to_fildes.h> 00032 00033 00034 static void 00035 explain_buffer_errno_freopen_system_call(explain_string_buffer_t *sb, 00036 int errnum, const char *pathname, const char *flags, FILE *fp) 00037 { 00038 (void)errnum; 00039 explain_string_buffer_puts(sb, "freopen(pathname = "); 00040 explain_string_buffer_puts_quoted(sb, pathname); 00041 explain_string_buffer_puts(sb, ", flags = "); 00042 explain_string_buffer_puts_quoted(sb, flags); 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_freopen_explanation(explain_string_buffer_t *sb, 00051 int errnum, const char *syscall_name, const char *pathname, 00052 const char *flags, FILE *fp) 00053 { 00054 switch (errnum) 00055 { 00056 case EFAULT: 00057 case EFBIG: 00058 case ENOSPC: 00059 case EPIPE: 00060 explain_buffer_errno_fflush_explanation(sb, errnum, syscall_name, fp); 00061 return; 00062 00063 case EBADF: 00064 explain_buffer_ebadf(sb, explain_stream_to_fildes(fp), "fp"); 00065 break; 00066 00067 case EINTR: 00068 case EIO: 00069 explain_buffer_errno_fclose_explanation(sb, errnum, syscall_name, fp); 00070 return; 00071 00072 default: 00073 explain_buffer_errno_fopen_explanation 00074 ( 00075 sb, 00076 errnum, 00077 syscall_name, 00078 pathname, 00079 flags 00080 ); 00081 break; 00082 } 00083 00084 explain_buffer_note_underlying_fildes_open(sb); 00085 } 00086 00087 00088 void 00089 explain_buffer_errno_freopen(struct explain_string_buffer_t *sb, 00090 int errnum, const char *pathname, const char *flags, FILE *fp) 00091 { 00092 explain_explanation_t exp; 00093 00094 explain_explanation_init(&exp, errnum); 00095 explain_buffer_errno_freopen_system_call 00096 ( 00097 &exp.system_call_sb, 00098 errnum, 00099 pathname, 00100 flags, 00101 fp 00102 ); 00103 explain_buffer_errno_freopen_explanation 00104 ( 00105 &exp.explanation_sb, 00106 errnum, 00107 "freopen", 00108 pathname, 00109 flags, 00110 fp 00111 ); 00112 explain_explanation_assemble(&exp, sb); 00113 } 00114 00115 00116 /* vim: set ts=8 sw=4 et : */