libexplain
1.4.D001
|
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/char.h> 00022 #include <libexplain/buffer/ebadf.h> 00023 #include <libexplain/buffer/errno/generic.h> 00024 #include <libexplain/buffer/errno/putc.h> 00025 #include <libexplain/buffer/errno/write.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_putc_system_call(explain_string_buffer_t *sb, 00035 int errnum, int c, FILE *fp) 00036 { 00037 (void)errnum; 00038 explain_string_buffer_puts(sb, "putc(c = "); 00039 explain_buffer_char(sb, c); 00040 explain_string_buffer_puts(sb, ", fp = "); 00041 explain_buffer_stream(sb, fp); 00042 explain_string_buffer_putc(sb, ')'); 00043 } 00044 00045 00046 static void 00047 explain_buffer_errno_putc_explanation(explain_string_buffer_t *sb, 00048 int errnum, int c, FILE *fp) 00049 { 00050 int fildes; 00051 00052 /* 00053 * http://www.opengroup.org/onlinepubs/009695399/functions/putc.html 00054 */ 00055 (void)c; 00056 00057 if (errnum == EBADF) 00058 { 00059 /* 00060 * The underlying fildes could be open read/write but the FILE 00061 * may not be open for writing. 00062 */ 00063 if (explain_libio_no_writes(fp)) 00064 { 00065 explain_buffer_ebadf_not_open_for_writing(sb, "fp", -1); 00066 explain_buffer_software_error(sb); 00067 return; 00068 } 00069 } 00070 00071 fildes = explain_stream_to_fildes(fp); 00072 explain_buffer_errno_write_explanation(sb, errnum, "putc", fildes, NULL, 0); 00073 } 00074 00075 00076 void 00077 explain_buffer_errno_putc(explain_string_buffer_t *sb, int errnum, int c, 00078 FILE *fp) 00079 { 00080 explain_explanation_t exp; 00081 00082 explain_explanation_init(&exp, errnum); 00083 explain_buffer_errno_putc_system_call 00084 ( 00085 &exp.system_call_sb, 00086 errnum, 00087 c, 00088 fp 00089 ); 00090 explain_buffer_errno_putc_explanation 00091 ( 00092 &exp.explanation_sb, 00093 errnum, 00094 c, 00095 fp 00096 ); 00097 explain_explanation_assemble(&exp, sb); 00098 } 00099 00100 00101 /* vim: set ts=8 sw=4 et : */