getgroups.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. #include <sys/syscall.h>
  10. #include <unistd.h>
  11. #if defined(__NR_getgroups32)
  12. # undef __NR_getgroups
  13. # define __NR_getgroups __NR_getgroups32
  14. _syscall2(int, getgroups, int, size, gid_t *, list)
  15. #elif __WORDSIZE == 64
  16. _syscall2(int, getgroups, int, size, gid_t *, list)
  17. #else
  18. # include <errno.h>
  19. # include <stdlib.h>
  20. # include <sys/types.h>
  21. # include <sys/param.h>
  22. # define __NR___syscall_getgroups __NR_getgroups
  23. static __always_inline
  24. _syscall2(int, __syscall_getgroups, int, size, __kernel_gid_t *, list)
  25. int getgroups(int size, gid_t groups[])
  26. {
  27. if (unlikely(size < 0)) {
  28. ret_error:
  29. __set_errno(EINVAL);
  30. return -1;
  31. } else {
  32. int i, ngids;
  33. __kernel_gid_t *kernel_groups;
  34. size = MIN(size, sysconf(_SC_NGROUPS_MAX));
  35. kernel_groups = (__kernel_gid_t *)malloc(sizeof(*kernel_groups) * size);
  36. if (size && kernel_groups == NULL)
  37. goto ret_error;
  38. ngids = __syscall_getgroups(size, kernel_groups);
  39. if (size != 0 && ngids > 0) {
  40. for (i = 0; i < ngids; i++) {
  41. groups[i] = kernel_groups[i];
  42. }
  43. }
  44. free(kernel_groups);
  45. return ngids;
  46. }
  47. }
  48. #endif
  49. libc_hidden_def(getgroups)