Import upstream version 1.28
[debian/tar] / gnu / getgroups.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* provide consistent interface to getgroups for systems that don't allow N==0
4
5    Copyright (C) 1996, 1999, 2003, 2006-2014 Free Software Foundation, Inc.
6
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 /* written by Jim Meyering */
21
22 #include <config.h>
23
24 #include <unistd.h>
25
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29
30 #if !HAVE_GETGROUPS
31
32 /* Provide a stub that fails with ENOSYS, since there is no group
33    information available on mingw.  */
34 int
35 getgroups (int n _GL_UNUSED, GETGROUPS_T *groups _GL_UNUSED)
36 {
37   errno = ENOSYS;
38   return -1;
39 }
40
41 #else /* HAVE_GETGROUPS */
42
43 # undef getgroups
44 # ifndef GETGROUPS_ZERO_BUG
45 #  define GETGROUPS_ZERO_BUG 0
46 # endif
47
48 /* On OS X 10.6 and later, use the usual getgroups, not the one
49    supplied when _DARWIN_C_SOURCE is defined.  _DARWIN_C_SOURCE is
50    normally defined, since it means "conform to POSIX, but add
51    non-POSIX extensions even if that violates the POSIX namespace
52    rules", which is what we normally want.  But with getgroups there
53    is an inconsistency, and _DARWIN_C_SOURCE means "change getgroups()
54    so that it no longer works right".  The BUGS section of compat(5)
55    says that the behavior is dubious if you compile different sections
56    of a program with different _DARWIN_C_SOURCE settings, so fix only
57    the offending symbol.  */
58 # ifdef __APPLE__
59 int posix_getgroups (int, gid_t []) __asm ("_getgroups");
60 #  define getgroups posix_getgroups
61 # endif
62
63 /* On at least Ultrix 4.3 and NextStep 3.2, getgroups (0, NULL) always
64    fails.  On other systems, it returns the number of supplemental
65    groups for the process.  This function handles that special case
66    and lets the system-provided function handle all others.  However,
67    it can fail with ENOMEM if memory is tight.  It is unspecified
68    whether the effective group id is included in the list.  */
69
70 int
71 rpl_getgroups (int n, gid_t *group)
72 {
73   int n_groups;
74   GETGROUPS_T *gbuf;
75   int saved_errno;
76
77   if (n < 0)
78     {
79       errno = EINVAL;
80       return -1;
81     }
82
83   if (n != 0 || !GETGROUPS_ZERO_BUG)
84     {
85       int result;
86       if (sizeof *group == sizeof *gbuf)
87         return getgroups (n, (GETGROUPS_T *) group);
88
89       if (SIZE_MAX / sizeof *gbuf <= n)
90         {
91           errno = ENOMEM;
92           return -1;
93         }
94       gbuf = malloc (n * sizeof *gbuf);
95       if (!gbuf)
96         return -1;
97       result = getgroups (n, gbuf);
98       if (0 <= result)
99         {
100           n = result;
101           while (n--)
102             group[n] = gbuf[n];
103         }
104       saved_errno = errno;
105       free (gbuf);
106       errno = saved_errno;
107       return result;
108     }
109
110   n = 20;
111   while (1)
112     {
113       /* No need to worry about address arithmetic overflow here,
114          since the ancient systems that we're running on have low
115          limits on the number of secondary groups.  */
116       gbuf = malloc (n * sizeof *gbuf);
117       if (!gbuf)
118         return -1;
119       n_groups = getgroups (n, gbuf);
120       if (n_groups == -1 ? errno != EINVAL : n_groups < n)
121         break;
122       free (gbuf);
123       n *= 2;
124     }
125
126   saved_errno = errno;
127   free (gbuf);
128   errno = saved_errno;
129
130   return n_groups;
131 }
132
133 #endif /* HAVE_GETGROUPS */