OSDN Git Service

Last portion of libc_hidden_proto removal.
[uclinux-h8/uClibc.git] / libc / sysdeps / linux / common / getgroups.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * getgroups() for uClibc
4  *
5  * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
6  *
7  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
8  */
9
10 #include <sys/syscall.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <grp.h>
14
15 /* libc_hidden_proto(getgroups) */
16
17 #if defined(__NR_getgroups32)
18 # undef __NR_getgroups
19 # define __NR_getgroups __NR_getgroups32
20 _syscall2(int, getgroups, int, size, gid_t *, list)
21
22 #elif __WORDSIZE == 64
23 _syscall2(int, getgroups, int, size, gid_t *, list)
24
25 #else
26
27 /* libc_hidden_proto(sysconf) */
28 #define MIN(a,b) (((a)<(b))?(a):(b))
29
30 #define __NR___syscall_getgroups __NR_getgroups
31 static __inline__ _syscall2(int, __syscall_getgroups,
32                 int, size, __kernel_gid_t *, list)
33
34 int getgroups(int size, gid_t groups[])
35 {
36         if (unlikely(size < 0)) {
37 ret_error:
38                 __set_errno(EINVAL);
39                 return -1;
40         } else {
41                 int i, ngids;
42                 __kernel_gid_t *kernel_groups;
43
44                 size = MIN(size, sysconf(_SC_NGROUPS_MAX));
45                 kernel_groups = (__kernel_gid_t *)malloc(sizeof(*kernel_groups) * size);
46                 if (size && kernel_groups == NULL)
47                         goto ret_error;
48
49                 ngids = __syscall_getgroups(size, kernel_groups);
50                 if (size != 0 && ngids > 0) {
51                         for (i = 0; i < ngids; i++) {
52                                 groups[i] = kernel_groups[i];
53                         }
54                 }
55
56                 free(kernel_groups);
57                 return ngids;
58         }
59 }
60 #endif
61
62 libc_hidden_def(getgroups)