getgroups.c 1.3 KB

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