libexplain  1.4.D001
libexplain/buffer/errno/opendir.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  * 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/unistd.h>
00023 
00024 #include <libexplain/buffer/enomem.h>
00025 #include <libexplain/buffer/errno/opendir.h>
00026 #include <libexplain/buffer/errno/open.h>
00027 #include <libexplain/buffer/pointer.h>
00028 #include <libexplain/explanation.h>
00029 
00030 
00031 static void
00032 explain_buffer_errno_opendir_system_call(explain_string_buffer_t *sb,
00033     int errnum, const char *pathname)
00034 {
00035     explain_string_buffer_printf(sb, "opendir(pathname = ");
00036     if (errnum == EFAULT)
00037         explain_buffer_pointer(sb, pathname);
00038     else
00039         explain_string_buffer_puts_quoted(sb, pathname);
00040     explain_string_buffer_putc(sb, ')');
00041 }
00042 
00043 
00044 static void
00045 explain_buffer_errno_opendir_explanation(explain_string_buffer_t *sb,
00046     int errnum, const char *syscall_name, const char *pathname)
00047 {
00048     switch (errnum)
00049     {
00050     case 0:
00051         break;
00052 
00053     case ENOMEM:
00054         {
00055             int             fd;
00056 
00057             errno = 0;
00058             fd = open(pathname, O_RDONLY + O_DIRECTORY);
00059             if (fd < 0)
00060             {
00061                 if (errno == ENOMEM)
00062                 {
00063                     explain_buffer_enomem_kernel(sb);
00064                     return;
00065                 }
00066                 explain_buffer_enomem_kernel_or_user(sb);
00067                 return;
00068             }
00069             close(fd);
00070             explain_buffer_enomem_user(sb, 0);
00071         }
00072         break;
00073 
00074     default:
00075         explain_buffer_errno_open_explanation
00076         (
00077             sb,
00078             errnum,
00079             syscall_name,
00080             pathname,
00081             O_RDONLY + O_DIRECTORY,
00082             0
00083         );
00084         break;
00085     }
00086 }
00087 
00088 
00089 void
00090 explain_buffer_errno_opendir(explain_string_buffer_t *sb, int errnum,
00091     const char *pathname)
00092 {
00093     explain_explanation_t exp;
00094 
00095     explain_explanation_init(&exp, errnum);
00096     explain_buffer_errno_opendir_system_call
00097     (
00098         &exp.system_call_sb,
00099         errnum,
00100         pathname
00101     );
00102     explain_buffer_errno_opendir_explanation
00103     (
00104         &exp.explanation_sb,
00105         errnum,
00106         "opendir",
00107         pathname
00108     );
00109     explain_explanation_assemble(&exp, sb);
00110 }
00111 
00112 
00113 /* vim: set ts=8 sw=4 et : */