libexplain  1.4.D001
libexplain/buffer/errno/tmpnam.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 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/errno/lstat.h>
00027 #include <libexplain/buffer/errno/tmpnam.h>
00028 #include <libexplain/buffer/pathname.h>
00029 #include <libexplain/explanation.h>
00030 #include <libexplain/path_search.h>
00031 
00032 
00033 static void
00034 explain_buffer_errno_tmpnam_system_call(explain_string_buffer_t *sb, int errnum,
00035     char *pathname)
00036 {
00037     (void)errnum;
00038     explain_string_buffer_puts(sb, "tmpnam(pathname = ");
00039     explain_buffer_pathname(sb, pathname);
00040     explain_string_buffer_putc(sb, ')');
00041 }
00042 
00043 
00044 void
00045 explain_buffer_errno_tmpnam_explanation(explain_string_buffer_t *sb, int errnum,
00046     const char *syscall_name, char *pathname)
00047 {
00048     char            *tmpbuf;
00049     const char      *dir;
00050     const char      *prefix;
00051     int             try_tmpdir;
00052     char            tmpbufmem[L_tmpnam];
00053 
00054     tmpbuf = pathname ? pathname : tmpbufmem;
00055     dir = NULL;
00056     prefix = 0;
00057     try_tmpdir = 0;
00058     if
00059     (
00060         errnum != EFAULT
00061     &&
00062         explain_path_search(tmpbuf, L_tmpnam, dir, prefix, try_tmpdir) < 0
00063     &&
00064         errno == errnum
00065     &&
00066         explain_path_search_explanation(sb, errnum, dir, try_tmpdir)
00067     )
00068     {
00069         return;
00070     }
00071 
00072     /*
00073      * http://www.opengroup.org/onlinepubs/009695399/functions/tmpnam.html
00074      */
00075     switch (errnum)
00076     {
00077     case EFAULT:
00078         explain_buffer_efault(sb, "pathname");
00079         break;
00080 
00081     case EEXIST:
00082         explain_buffer_eexist_tempname_dirname(sb, tmpbuf);
00083         break;
00084 
00085     case EINVAL:
00086         explain_buffer_einval_mkstemp(sb, tmpbuf, "pathname");
00087         break;
00088 
00089     default:
00090         explain_buffer_errno_lstat_explanation
00091         (
00092             sb,
00093             errnum,
00094             syscall_name,
00095             tmpbuf,
00096             0
00097         );
00098         break;
00099     }
00100     explain_buffer_dangerous_system_call(sb, syscall_name);
00101 }
00102 
00103 
00104 void
00105 explain_buffer_errno_tmpnam(explain_string_buffer_t *sb, int errnum, char
00106     *pathname)
00107 {
00108     explain_explanation_t exp;
00109 
00110     explain_explanation_init(&exp, errnum);
00111     explain_buffer_errno_tmpnam_system_call(&exp.system_call_sb, errnum,
00112         pathname);
00113     explain_buffer_errno_tmpnam_explanation(&exp.explanation_sb, errnum,
00114         "tmpnam", pathname);
00115     explain_explanation_assemble(&exp, sb);
00116 }
00117 
00118 
00119 /* vim: set ts=8 sw=4 et : */