getgroups.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * getgroups() for uClibc
  3. *
  4. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  5. *
  6. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  7. */
  8. #include <sys/syscall.h>
  9. #include <unistd.h>
  10. #if defined(__NR_getgroups32)
  11. # undef __NR_getgroups
  12. # define __NR_getgroups __NR_getgroups32
  13. _syscall2(int, getgroups, int, size, gid_t *, list)
  14. #elif __WORDSIZE == 64
  15. _syscall2(int, getgroups, int, size, gid_t *, list)
  16. #else
  17. # include <errno.h>
  18. # include <stdlib.h>
  19. # include <sys/types.h>
  20. # include <sys/param.h>
  21. # define __NR___syscall_getgroups __NR_getgroups
  22. static __always_inline
  23. _syscall2(int, __syscall_getgroups, int, size, __kernel_gid_t *, list)
  24. int getgroups(int size, gid_t groups[])
  25. {
  26. if (unlikely(size < 0)) {
  27. ret_error:
  28. __set_errno(EINVAL);
  29. return -1;
  30. } else {
  31. int i, ngids;
  32. __kernel_gid_t *kernel_groups;
  33. size = MIN(size, sysconf(_SC_NGROUPS_MAX));
  34. kernel_groups = (__kernel_gid_t *)malloc(sizeof(*kernel_groups) * size);
  35. if (size && kernel_groups == NULL)
  36. goto ret_error;
  37. ngids = __syscall_getgroups(size, kernel_groups);
  38. if (size != 0 && ngids > 0) {
  39. for (i = 0; i < ngids; i++) {
  40. groups[i] = kernel_groups[i];
  41. }
  42. }
  43. free(kernel_groups);
  44. return ngids;
  45. }
  46. }
  47. #endif
  48. libc_hidden_def(getgroups)