libexplain  1.4.D001
libexplain/buffer/errno/fdopen.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/enomem.h>
00023 #include <libexplain/buffer/errno/generic.h>
00024 #include <libexplain/buffer/errno/fdopen.h>
00025 #include <libexplain/buffer/fildes_to_pathname.h>
00026 #include <libexplain/buffer/open_flags.h>
00027 #include <libexplain/explanation.h>
00028 #include <libexplain/string_flags.h>
00029 
00030 
00031 static void
00032 explain_buffer_errno_fdopen_system_call(explain_string_buffer_t *sb,
00033     int errnum, int fildes, const char *flags)
00034 {
00035     (void)errnum;
00036     explain_string_buffer_printf(sb, "fdopen(fildes = %d", fildes);
00037     explain_buffer_fildes_to_pathname(sb, fildes);
00038     explain_string_buffer_puts(sb, ", flags = ");
00039     explain_string_buffer_puts_quoted(sb, flags);
00040     explain_string_buffer_putc(sb, ')');
00041 }
00042 
00043 
00044 static void
00045 explain_buffer_errno_fdopen_explanation(explain_string_buffer_t *sb,
00046     int errnum, int fildes, const char *flags)
00047 {
00048     explain_string_flags_t sf;
00049     int             xflags;
00050 
00051     explain_string_flags_init(&sf, flags);
00052     sf.flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
00053 
00054     /*
00055      * http://www.opengroup.org/onlinepubs/009695399/functions/fdopen.html
00056      */
00057     switch (errnum)
00058     {
00059     case EINVAL:
00060         explain_string_flags_einval(&sf, sb, "flags");
00061         xflags = fcntl(fildes, F_GETFL);
00062         if (xflags >= 0 && xflags != sf.flags)
00063         {
00064             explain_string_buffer_puts(sb, ", the file descriptor flags (");
00065             explain_buffer_open_flags(sb, xflags);
00066             explain_string_buffer_puts
00067             (
00068                 sb,
00069                 ") do not match the flags specified ("
00070             );
00071             explain_buffer_open_flags(sb, sf.flags);
00072             explain_string_buffer_putc(sb, ')');
00073         }
00074         break;
00075 
00076     case ENOMEM:
00077         explain_buffer_enomem_user(sb, 0);
00078         break;
00079 
00080     default:
00081         explain_buffer_errno_generic(sb, errnum, "fdopen");
00082         break;
00083     }
00084 }
00085 
00086 
00087 void
00088 explain_buffer_errno_fdopen(explain_string_buffer_t *sb, int errnum,
00089     int fildes, const char *flags)
00090 {
00091     explain_explanation_t exp;
00092 
00093     explain_explanation_init(&exp, errnum);
00094     explain_buffer_errno_fdopen_system_call
00095     (
00096         &exp.system_call_sb,
00097         errnum,
00098         fildes,
00099         flags
00100     );
00101     explain_buffer_errno_fdopen_explanation
00102     (
00103         &exp.explanation_sb,
00104         errnum,
00105         fildes,
00106         flags
00107     );
00108     explain_explanation_assemble(&exp, sb);
00109 }
00110 
00111 
00112 /* vim: set ts=8 sw=4 et : */