libexplain  1.4.D001
libexplain/buffer/errno/settimeofday.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 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 of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
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/sys/stat.h>
00021 #include <libexplain/ac/sys/time.h>
00022 
00023 #include <libexplain/buffer/efault.h>
00024 #include <libexplain/buffer/einval.h>
00025 #include <libexplain/buffer/eperm.h>
00026 #include <libexplain/buffer/errno/generic.h>
00027 #include <libexplain/buffer/errno/settimeofday.h>
00028 #include <libexplain/buffer/gettext.h>
00029 #include <libexplain/buffer/timeval.h>
00030 #include <libexplain/buffer/timezone.h>
00031 #include <libexplain/explanation.h>
00032 #include <libexplain/is_efault.h>
00033 
00034 
00035 static void
00036 explain_buffer_errno_settimeofday_system_call(explain_string_buffer_t *sb, int
00037     errnum, const struct timeval *tv, const struct timezone *tz)
00038 {
00039     (void)errnum;
00040     explain_string_buffer_puts(sb, "settimeofday(tv = ");
00041     explain_buffer_timeval(sb, tv);
00042     explain_string_buffer_puts(sb, ", tz = ");
00043     explain_buffer_timezone(sb, tz);
00044     explain_string_buffer_putc(sb, ')');
00045 }
00046 
00047 
00048 static int
00049 valid_microseconds(long x)
00050 {
00051     if (x == UTIME_NOW || x == UTIME_OMIT)
00052         return 1;
00053     return (0 <= x && x < 1000000);
00054 }
00055 
00056 
00057 void
00058 explain_buffer_errno_settimeofday_explanation(explain_string_buffer_t *sb,
00059     int errnum, const char *syscall_name, const struct timeval *tv,
00060     const struct timezone *tz)
00061 {
00062     if (tz)
00063     {
00064         explain_string_buffer_puts(sb->footnotes, "; ");
00065         explain_buffer_gettext
00066         (
00067             sb->footnotes,
00068             /* FIXME: i18n */
00069             "the use of the timezone structure is obsolete; the tz "
00070             "argument should normally be specified as NULL"
00071         );
00072     }
00073 
00074     switch (errnum)
00075     {
00076     case EFAULT:
00077         if (explain_is_efault_pointer(tv, sizeof(*tv)))
00078         {
00079             explain_buffer_efault(sb, "tv");
00080             return;
00081         }
00082         if (explain_is_efault_pointer(tz, sizeof(*tz)))
00083         {
00084             explain_buffer_efault(sb, "tz");
00085             return;
00086         }
00087         break;
00088 
00089     case EINVAL:
00090         if (tv && !valid_microseconds(tv->tv_usec))
00091         {
00092             explain_buffer_einval_out_of_range(sb, "tv->tv_usec", 0, 1000000);
00093             return;
00094         }
00095         break;
00096 
00097     case EPERM:
00098         explain_buffer_eperm_sys_time(sb, syscall_name);
00099         return;
00100 
00101     default:
00102         break;
00103     }
00104     explain_buffer_errno_generic(sb, errnum, syscall_name);
00105 }
00106 
00107 
00108 void
00109 explain_buffer_errno_settimeofday(explain_string_buffer_t *sb, int errnum,
00110     const struct timeval *tv, const struct timezone *tz)
00111 {
00112     explain_explanation_t exp;
00113 
00114     explain_explanation_init(&exp, errnum);
00115     explain_buffer_errno_settimeofday_system_call
00116     (
00117         &exp.system_call_sb,
00118         errnum,
00119         tv,
00120         tz
00121     );
00122     explain_buffer_errno_settimeofday_explanation
00123     (
00124         &exp.explanation_sb,
00125         errnum,
00126         "settimeofday",
00127         tv,
00128         tz
00129     );
00130     explain_explanation_assemble(&exp, sb);
00131 }
00132 
00133 
00134 /* vim: set ts=8 sw=4 et : */