libexplain  1.4.D001
libexplain/buffer/errno/mktemp.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2010, 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,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/string.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/mktemp.h>
00028 #include <libexplain/buffer/pathname.h>
00029 #include <libexplain/explanation.h>
00030 #include <libexplain/is_efault.h>
00031 
00032 
00033 static void
00034 explain_buffer_errno_mktemp_system_call(explain_string_buffer_t *sb, int errnum,
00035     char *pathname)
00036 {
00037     (void)errnum;
00038     explain_string_buffer_puts(sb, "mktemp(pathname = ");
00039     explain_buffer_pathname(sb, pathname);
00040     explain_string_buffer_putc(sb, ')');
00041 }
00042 
00043 
00044 void
00045 explain_buffer_errno_mktemp_explanation(explain_string_buffer_t *sb, int errnum,
00046     const char *syscall_name, char *pathname)
00047 {
00048     /*
00049      * http://www.opengroup.org/onlinepubs/009695399/functions/mktemp.html
00050      */
00051     switch (errnum)
00052     {
00053     case EFAULT:
00054         explain_buffer_efault(sb, "pathname");
00055         break;
00056 
00057     case EINVAL:
00058         explain_buffer_einval_mkstemp(sb, pathname, "pathname");
00059         break;
00060 
00061     case EEXIST:
00062         explain_buffer_eexist_tempname_dirname(sb, pathname);
00063         break;
00064 
00065     default:
00066         explain_buffer_errno_lstat_explanation
00067         (
00068             sb,
00069             errnum,
00070             syscall_name,
00071             pathname,
00072             NULL
00073         );
00074         break;
00075     }
00076     explain_buffer_dangerous_system_call(sb, syscall_name);
00077 }
00078 
00079 
00080 void
00081 explain_buffer_errno_mktemp(explain_string_buffer_t *sb, int errnum,
00082     char *pathname)
00083 {
00084     char            ok;
00085     char            first;
00086     explain_explanation_t exp;
00087 
00088     /*
00089      * From mktemp(3) man page...
00090      *
00091      *     The mktemp() function always returns pathname.  If a unique name was
00092      *     created, the last six bytes of pathname will have been modified in
00093      *     such a way that the resulting name is unique (i.e., does not exist
00094      *     already) If a unique name could not be created, pathname is made an
00095      *     empty string.
00096      *
00097      * Morons.  Returning NULL would have been far more uesful, not to mention
00098      * far more consistent.
00099      */
00100     ok = !explain_is_efault_string(pathname);
00101     first = '\0';
00102     if (ok)
00103     {
00104         /*
00105          * If we reached here via explain_mktemp_on_error, the original first
00106          * character has been restored.  We only need the following hack if
00107          * the user called mktemp directly.
00108          */
00109         first = pathname[0];
00110         if (first == '\0')
00111         {
00112             /*
00113              * Try to undo the brain-damage of the way mktemp(3) reports
00114              * errors.  Assume the programmer was at least half-way sane.  Our
00115              * first guess is that the NUL replaces a slash (assume /tmp or
00116              * similar), except in the limiting case, when it had to be an 'X'.
00117              */
00118             pathname[0] = '/';
00119             if (strlen(pathname) <= 6)
00120                 pathname[0] = 'X';
00121         }
00122     }
00123 
00124     explain_explanation_init(&exp, errnum);
00125     explain_buffer_errno_mktemp_system_call(&exp.system_call_sb, errnum,
00126         pathname);
00127     explain_buffer_errno_mktemp_explanation(&exp.explanation_sb, errnum,
00128         "mktemp", pathname);
00129     explain_explanation_assemble(&exp, sb);
00130 
00131     /*
00132      * re-inflict the brain damage
00133      */
00134     if (ok)
00135         pathname[0] = first;
00136 }
00137 
00138 
00139 /* vim: set ts=8 sw=4 et : */