getgroups.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. #define sysconf __sysconf
  10. #include "syscalls.h"
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <grp.h>
  14. #define MIN(a,b) (((a)<(b))?(a):(b))
  15. #define __NR___syscall_getgroups __NR_getgroups
  16. static inline _syscall2(int, __syscall_getgroups,
  17. int, size, __kernel_gid_t *, list);
  18. int attribute_hidden __getgroups(int size, gid_t groups[])
  19. {
  20. if (unlikely(size < 0)) {
  21. ret_error:
  22. __set_errno(EINVAL);
  23. return -1;
  24. } else {
  25. int i, ngids;
  26. __kernel_gid_t *kernel_groups;
  27. size = MIN(size, sysconf(_SC_NGROUPS_MAX));
  28. kernel_groups = (__kernel_gid_t *)malloc(sizeof(*kernel_groups) * size);
  29. if (size && kernel_groups == NULL)
  30. goto ret_error;
  31. ngids = __syscall_getgroups(size, kernel_groups);
  32. if (size != 0 && ngids > 0) {
  33. for (i = 0; i < ngids; i++) {
  34. groups[i] = kernel_groups[i];
  35. }
  36. }
  37. if (kernel_groups)
  38. free(kernel_groups);
  39. return ngids;
  40. }
  41. }
  42. strong_alias(__getgroups,getgroups)