libexplain  1.4.D001
libexplain/getgrent_or_die.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/grp.h>
00021 
00022 #include <libexplain/getgrent.h>
00023 #include <libexplain/output.h>
00024 
00025 
00026 struct group *
00027 explain_getgrent_or_die(void)
00028 {
00029     struct group    *result;
00030     int             hold_errno;
00031 
00032     hold_errno = errno;
00033     errno = 0;
00034     result = explain_getgrent_on_error();
00035     if (!result)
00036     {
00037         /*
00038          * The getgrent() function returns a pointer to a group
00039          * structure, or NULL if there are no more entries or an error
00040          * occurs.
00041          *
00042          * Upon error, errno may be set.  If one wants to check errno
00043          * after the call, it should be set to zero before the call.
00044          */
00045         if (errno != 0)
00046             explain_output_exit_failure();
00047     }
00048     errno = hold_errno;
00049     return result;
00050 }
00051 
00052 
00053 struct group *
00054 explain_getgrent_on_error(void)
00055 {
00056     struct group    *result;
00057     int             hold_errno;
00058 
00059     hold_errno = errno;
00060     errno = 0;
00061 #ifdef HAVE_GETGRENT
00062     result = getgrent();
00063 #else
00064     errno = ENOSYS;
00065     result = 0;
00066 #endif
00067     if (!result)
00068     {
00069         /*
00070          * The getgrent() function returns a pointer to a group
00071          * structure, or NULL if there are no more entries or an error
00072          * occurs.
00073          *
00074          * Upon error, errno may be set.  If one wants to check errno
00075          * after the call, it should be set to zero before the call.
00076          */
00077         if (errno != 0)
00078         {
00079             hold_errno = errno;
00080             explain_output_error("%s", explain_errno_getgrent(hold_errno));
00081         }
00082     }
00083     errno = hold_errno;
00084     return result;
00085 }
00086 
00087 
00088 /* vim: set ts=8 sw=4 et : */