libexplain  1.4.D001
libexplain/buffer/errno/getgroups.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,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser 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/limits.h> /* for NGROUPS_MAX on Solaris */
00021 #include <libexplain/ac/unistd.h>
00022 
00023 #include <libexplain/buffer/efault.h>
00024 #include <libexplain/buffer/einval.h>
00025 #include <libexplain/buffer/errno/generic.h>
00026 #include <libexplain/buffer/errno/getgroups.h>
00027 #include <libexplain/buffer/pointer.h>
00028 #include <libexplain/explanation.h>
00029 #include <libexplain/option.h>
00030 
00031 
00032 static void
00033 explain_buffer_errno_getgroups_system_call(explain_string_buffer_t *sb, int
00034     errnum, int data_size, gid_t *data)
00035 {
00036     (void)errnum;
00037     explain_string_buffer_puts(sb, "getgroups(data_size = ");
00038     explain_string_buffer_printf(sb, "%d", data_size);
00039     explain_string_buffer_puts(sb, ", data = ");
00040     explain_buffer_pointer(sb, data);
00041     explain_string_buffer_putc(sb, ')');
00042 }
00043 
00044 
00045 static void
00046 explain_buffer_errno_getgroups_explanation(explain_string_buffer_t *sb,
00047     int errnum, int data_size, gid_t *data)
00048 {
00049     /*
00050      * http://www.opengroup.org/onlinepubs/009695399/functions/getgroups.html
00051      */
00052     (void)data;
00053     switch (errnum)
00054     {
00055     case EFAULT:
00056         explain_buffer_efault(sb, "data");
00057         break;
00058 
00059     case EINVAL:
00060         explain_buffer_einval_too_small(sb, "data_size", data_size);
00061         if (data_size >= 0 && explain_option_dialect_specific())
00062         {
00063             gid_t groups[NGROUPS_MAX];
00064             int ngroups;
00065 
00066             ngroups = getgroups(NGROUPS_MAX, groups);
00067             if (ngroups >= 0 && data_size < ngroups)
00068             {
00069                 explain_string_buffer_printf
00070                 (
00071                     sb,
00072                     " (%d < %d)",
00073                     data_size,
00074                     ngroups
00075                 );
00076             }
00077         }
00078         break;
00079 
00080     default:
00081         explain_buffer_errno_generic(sb, errnum, "getgroups");
00082         break;
00083     }
00084 }
00085 
00086 
00087 void
00088 explain_buffer_errno_getgroups(explain_string_buffer_t *sb, int errnum, int
00089     data_size, gid_t *data)
00090 {
00091     explain_explanation_t exp;
00092 
00093     explain_explanation_init(&exp, errnum);
00094     explain_buffer_errno_getgroups_system_call(&exp.system_call_sb, errnum,
00095         data_size, data);
00096     explain_buffer_errno_getgroups_explanation(&exp.explanation_sb, errnum,
00097         data_size, data);
00098     explain_explanation_assemble(&exp, sb);
00099 }
00100 
00101 
00102 /* vim: set ts=8 sw=4 et : */