getgroups.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 <stdlib.h>
  11. #include <unistd.h>
  12. #include <grp.h>
  13. #if defined(__NR_getgroups32)
  14. # undef __NR_getgroups
  15. # define __NR_getgroups __NR_getgroups32
  16. _syscall2(int, getgroups, int, size, gid_t *, list)
  17. #elif __WORDSIZE == 64
  18. _syscall2(int, getgroups, int, size, gid_t *, list)
  19. #else
  20. #define MIN(a,b) (((a)<(b))?(a):(b))
  21. #define __NR___syscall_getgroups __NR_getgroups
  22. static __inline__ _syscall2(int, __syscall_getgroups,
  23. 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)