Update.
[glibc.git] / sysdeps / unix / sysv / linux / i386 / getgroups.c
blob071be4f4b7b985d3f2835f1bf621e827dd2ae67b
1 /* Copyright (C) 1997, 1998, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include <errno.h>
20 #include <unistd.h>
21 #include <sys/param.h>
22 #include <sys/types.h>
24 #include <sysdep.h>
25 #include <sys/syscall.h>
26 #include <linux/posix_types.h>
27 #include <kernel-features.h>
30 extern int __syscall_getgroups(int, __kernel_gid_t *);
32 #ifdef __NR_getgroups32
33 extern int __syscall_getgroups32 (int, __kernel_gid32_t *);
34 # if __ASSUME_32BITUIDS == 0
35 /* This variable is shared with all files that need to check for 32bit
36 uids. */
37 extern int __libc_missing_32bit_uids;
38 # endif
39 #endif /* __NR_getgroups32 */
41 /* For Linux we must convert the array of groups from the format that the
42 kernel returns. */
43 int
44 __getgroups (int n, gid_t *groups)
46 if (n < 0)
48 __set_errno (EINVAL);
49 return -1;
51 else
53 #if __ASSUME_32BITUIDS > 0
54 return INLINE_SYSCALL (getgroups32, 2, n, groups);
55 #else
56 int i, ngids;
57 __kernel_gid_t kernel_groups[n = MIN (n, __sysconf (_SC_NGROUPS_MAX))];
58 # ifdef __NR_getgroups32
59 if (__libc_missing_32bit_uids <= 0)
61 int result;
62 int saved_errno = errno;
64 result = INLINE_SYSCALL (getgroups32, 2, n, groups);
65 if (result == 0 || errno != ENOSYS)
66 return result;
68 __set_errno (saved_errno);
69 __libc_missing_32bit_uids = 1;
71 # endif /* __NR_getgroups32 */
73 ngids = INLINE_SYSCALL (getgroups, 2, n, kernel_groups);
74 if (n != 0 && ngids > 0)
75 for (i = 0; i < ngids; i++)
76 groups[i] = kernel_groups[i];
78 return ngids;
79 #endif
83 weak_alias (__getgroups, getgroups)