getgroups.c 787 B

123456789101112131415161718192021222324252627282930313233343536
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * getgroups() for uClibc
  4. *
  5. * Copyright (C) 2000-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * GNU Library General Public License (LGPL) version 2 or later.
  8. */
  9. #include "syscalls.h"
  10. #include <unistd.h>
  11. #define MIN(a,b) (((a)<(b))?(a):(b))
  12. #define __NR___syscall_getgroups __NR_getgroups
  13. static inline _syscall2(int, __syscall_getgroups,
  14. int, size, __kernel_gid_t *, list);
  15. int getgroups(int n, gid_t * groups)
  16. {
  17. if (unlikely(n < 0)) {
  18. __set_errno(EINVAL);
  19. return -1;
  20. } else {
  21. int i, ngids;
  22. __kernel_gid_t kernel_groups[n = MIN(n, sysconf(_SC_NGROUPS_MAX))];
  23. ngids = __syscall_getgroups(n, kernel_groups);
  24. if (n != 0 && ngids > 0) {
  25. for (i = 0; i < ngids; i++) {
  26. groups[i] = kernel_groups[i];
  27. }
  28. }
  29. return ngids;
  30. }
  31. }