libexplain  1.4.D001
libexplain/buffer/errno/tempnam.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2009, 2010, 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,but
00011  * WITHOUT ANY WARRANTY; without even the implied warranty
00012  * ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNULesser
00013  * 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/stdio.h>
00021 
00022 #include <libexplain/buffer/dangerous.h>
00023 #include <libexplain/buffer/eexist.h>
00024 #include <libexplain/buffer/efault.h>
00025 #include <libexplain/buffer/einval.h>
00026 #include <libexplain/buffer/enomem.h>
00027 #include <libexplain/buffer/errno/lstat.h>
00028 #include <libexplain/buffer/errno/tempnam.h>
00029 #include <libexplain/buffer/pathname.h>
00030 #include <libexplain/explanation.h>
00031 #include <libexplain/path_search.h>
00032 
00033 
00034 static void
00035 explain_buffer_errno_tempnam_system_call(explain_string_buffer_t *sb,
00036     int errnum, const char *dir, const char *prefix)
00037 {
00038     (void)errnum;
00039     explain_string_buffer_puts(sb, "tempnam(dir = ");
00040     explain_buffer_pathname(sb, dir);
00041     explain_string_buffer_puts(sb, ", prefix = ");
00042     explain_buffer_pathname(sb, prefix);
00043     explain_string_buffer_putc(sb, ')');
00044 }
00045 
00046 
00047 void
00048 explain_buffer_errno_tempnam_explanation(explain_string_buffer_t *sb,
00049     int errnum, const char *syscall_name, const char *dir, const char *prefix)
00050 {
00051     /*
00052      * We use the same array size that [e]glibc uses.
00053      */
00054     char            pathname[FILENAME_MAX];
00055 
00056     /*
00057      * Do what [e]glibc would do (probably did).
00058      */
00059     if
00060     (
00061         errnum != EFAULT
00062     &&
00063         explain_path_search(pathname, sizeof(pathname), dir, prefix, 1) < 0
00064     &&
00065         errno == errnum
00066     &&
00067         explain_path_search_explanation(sb, errno, dir, 1) >= 0
00068     )
00069     {
00070         return;
00071     }
00072 
00073     /*
00074      * http://www.opengroup.org/onlinepubs/009695399/functions/tempnam.html
00075      */
00076     switch (errnum)
00077     {
00078     case EFAULT:
00079         explain_buffer_efault(sb, "pathname");
00080         break;
00081 
00082     case EINVAL:
00083         /*
00084          * this is particularly weird, but it can happen if the "dir" argument
00085          * or "prefix" argument is too large.
00086          */
00087         explain_buffer_einval_mkstemp(sb, pathname, "pathname");
00088         break;
00089 
00090     case ENOMEM:
00091         /*
00092          * This would be the result of the strdup used to copy the
00093          * generated pathname.
00094          */
00095         explain_buffer_enomem_user(sb, 0);
00096         break;
00097 
00098     case EEXIST:
00099         explain_buffer_eexist_tempname_dirname(sb, pathname);
00100         break;
00101 
00102     default:
00103         explain_buffer_errno_lstat_explanation
00104         (
00105             sb,
00106             errnum,
00107             syscall_name,
00108             pathname,
00109             0
00110         );
00111         break;
00112     }
00113     explain_buffer_dangerous_system_call(sb, syscall_name);
00114 }
00115 
00116 
00117 void
00118 explain_buffer_errno_tempnam(explain_string_buffer_t *sb, int errnum,
00119     const char *dir, const char *prefix)
00120 {
00121     explain_explanation_t exp;
00122 
00123     explain_explanation_init(&exp, errnum);
00124     explain_buffer_errno_tempnam_system_call(&exp.system_call_sb, errnum, dir,
00125         prefix);
00126     explain_buffer_errno_tempnam_explanation(&exp.explanation_sb, errnum,
00127         "tempnam", dir, prefix);
00128     explain_explanation_assemble(&exp, sb);
00129 }
00130 
00131 
00132 /* vim: set ts=8 sw=4 et : */