gnulib-common: prefer _GL_UNUSED over _UNUSED_PARAMETER_
[gnulib.git] / lib / getgroups.c
1 /* provide consistent interface to getgroups for systems that don't allow N==0
2
3    Copyright (C) 1996, 1999, 2003, 2006, 2007, 2008, 2009 Free
4    Software Foundation, Inc.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 /* written by Jim Meyering */
20
21 #include <config.h>
22
23 #include <unistd.h>
24
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <stdint.h>
28
29 #if !HAVE_GETGROUPS
30
31 /* Provide a stub that fails with ENOSYS, since there is no group
32    information available on mingw.  */
33 int
34 getgroups (int n _GL_UNUSED, GETGROUPS_T *groups _GL_UNUSED)
35 {
36   errno = ENOSYS;
37   return -1;
38 }
39
40 #else /* HAVE_GETGROUPS */
41
42 # undef getgroups
43 # ifndef GETGROUPS_ZERO_BUG
44 #  define GETGROUPS_ZERO_BUG 0
45 # endif
46
47 /* On at least Ultrix 4.3 and NextStep 3.2, getgroups (0, NULL) always
48    fails.  On other systems, it returns the number of supplemental
49    groups for the process.  This function handles that special case
50    and lets the system-provided function handle all others.  However,
51    it can fail with ENOMEM if memory is tight.  It is unspecified
52    whether the effective group id is included in the list.  */
53
54 int
55 rpl_getgroups (int n, gid_t *group)
56 {
57   int n_groups;
58   GETGROUPS_T *gbuf;
59   int saved_errno;
60
61   if (n < 0)
62     {
63       errno = EINVAL;
64       return -1;
65     }
66
67   if (n != 0 || !GETGROUPS_ZERO_BUG)
68     {
69       int result;
70       if (sizeof *group == sizeof *gbuf)
71         return getgroups (n, (GETGROUPS_T *) group);
72
73       if (SIZE_MAX / sizeof *gbuf <= n)
74         {
75           errno = ENOMEM;
76           return -1;
77         }
78       gbuf = malloc (n * sizeof *gbuf);
79       if (!gbuf)
80         return -1;
81       result = getgroups (n, gbuf);
82       if (0 <= result)
83         {
84           n = result;
85           while (n--)
86             group[n] = gbuf[n];
87         }
88       saved_errno = errno;
89       free (gbuf);
90       errno == saved_errno;
91       return result;
92     }
93
94   n = 20;
95   while (1)
96     {
97       /* No need to worry about address arithmetic overflow here,
98          since the ancient systems that we're running on have low
99          limits on the number of secondary groups.  */
100       gbuf = malloc (n * sizeof *gbuf);
101       if (!gbuf)
102         return -1;
103       n_groups = getgroups (n, gbuf);
104       if (n_groups == -1 ? errno != EINVAL : n_groups < n)
105         break;
106       free (gbuf);
107       n *= 2;
108     }
109
110   saved_errno = errno;
111   free (gbuf);
112   errno = saved_errno;
113
114   return n_groups;
115 }
116
117 #endif /* HAVE_GETGROUPS */