src/dispextern.h (FACE_FOR_CHAR): Fix the commentary.
[emacs.git] / lib / getgroups.c
bloba7f0f9e2a29347d70db908c28eec5115658dc7da
1 /* provide consistent interface to getgroups for systems that don't allow N==0
3 Copyright (C) 1996, 1999, 2003, 2006-2015 Free Software Foundation,
4 Inc.
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.
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.
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/>. */
19 /* written by Jim Meyering */
21 #include <config.h>
23 #include <unistd.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <stdint.h>
29 #if !HAVE_GETGROUPS
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)
36 errno = ENOSYS;
37 return -1;
40 #else /* HAVE_GETGROUPS */
42 # undef getgroups
43 # ifndef GETGROUPS_ZERO_BUG
44 # define GETGROUPS_ZERO_BUG 0
45 # endif
47 /* On OS X 10.6 and later, use the usual getgroups, not the one
48 supplied when _DARWIN_C_SOURCE is defined. _DARWIN_C_SOURCE is
49 normally defined, since it means "conform to POSIX, but add
50 non-POSIX extensions even if that violates the POSIX namespace
51 rules", which is what we normally want. But with getgroups there
52 is an inconsistency, and _DARWIN_C_SOURCE means "change getgroups()
53 so that it no longer works right". The BUGS section of compat(5)
54 says that the behavior is dubious if you compile different sections
55 of a program with different _DARWIN_C_SOURCE settings, so fix only
56 the offending symbol. */
57 # ifdef __APPLE__
58 int posix_getgroups (int, gid_t []) __asm ("_getgroups");
59 # define getgroups posix_getgroups
60 # endif
62 /* On at least Ultrix 4.3 and NextStep 3.2, getgroups (0, NULL) always
63 fails. On other systems, it returns the number of supplemental
64 groups for the process. This function handles that special case
65 and lets the system-provided function handle all others. However,
66 it can fail with ENOMEM if memory is tight. It is unspecified
67 whether the effective group id is included in the list. */
69 int
70 rpl_getgroups (int n, gid_t *group)
72 int n_groups;
73 GETGROUPS_T *gbuf;
74 int saved_errno;
76 if (n < 0)
78 errno = EINVAL;
79 return -1;
82 if (n != 0 || !GETGROUPS_ZERO_BUG)
84 int result;
85 if (sizeof *group == sizeof *gbuf)
86 return getgroups (n, (GETGROUPS_T *) group);
88 if (SIZE_MAX / sizeof *gbuf <= n)
90 errno = ENOMEM;
91 return -1;
93 gbuf = malloc (n * sizeof *gbuf);
94 if (!gbuf)
95 return -1;
96 result = getgroups (n, gbuf);
97 if (0 <= result)
99 n = result;
100 while (n--)
101 group[n] = gbuf[n];
103 saved_errno = errno;
104 free (gbuf);
105 errno = saved_errno;
106 return result;
109 n = 20;
110 while (1)
112 /* No need to worry about address arithmetic overflow here,
113 since the ancient systems that we're running on have low
114 limits on the number of secondary groups. */
115 gbuf = malloc (n * sizeof *gbuf);
116 if (!gbuf)
117 return -1;
118 n_groups = getgroups (n, gbuf);
119 if (n_groups == -1 ? errno != EINVAL : n_groups < n)
120 break;
121 free (gbuf);
122 n *= 2;
125 saved_errno = errno;
126 free (gbuf);
127 errno = saved_errno;
129 return n_groups;
132 #endif /* HAVE_GETGROUPS */