home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / glibc-1.06 / sysdeps / unix / common / __getgrps.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-15  |  1.9 KB  |  66 lines

  1. /* Copyright (C) 1991, 1993 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with the GNU C Library; see the file COPYING.  If not, write to
  16. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <ansidecl.h>
  19. #include <sysdep.h>
  20. #include <errno.h>
  21. #include <stddef.h>
  22. #include <unistd.h>
  23. #include <sys/types.h>
  24. #include <limits.h>
  25. #include <stdlib.h>
  26.  
  27.  
  28. extern int EXFUN(__bsd_getgroups, (int n, int list[]));
  29.  
  30. /* If SIZE is zero, return the number of supplementary groups
  31.    the calling process is in.  Otherwise, fill in the group IDs
  32.    of its supplementary groups in LIST and return the number written.  */
  33. int
  34. DEFUN(__getgroups, (size, list), int size AND gid_t list[])
  35. {
  36.   int *groups;
  37.   int n;
  38.   register int i;
  39.  
  40.   if (sizeof (gid_t) == sizeof (int) || size == 0)
  41.     {
  42.       int save = errno;
  43.       n = __bsd_getgroups (size, (int *) list);
  44.       if (n < 0 && size == 0)
  45.     {
  46.       /* Dynix erroneously reports `getgroups (0, 0)' as an error.  */
  47.       errno = save;
  48.       groups = (int *) __alloca (NGROUPS_MAX * sizeof (int));
  49.       return __bsd_getgroups (NGROUPS_MAX, groups);
  50.     }
  51.       return n;
  52.     }
  53.  
  54.   if (size < 0 || list == NULL)
  55.     {
  56.       errno = EINVAL;
  57.       return -1;
  58.     }
  59.  
  60.   groups = (int *) __alloca (size * sizeof (int));
  61.   n = __bsd_getgroups (size, groups);
  62.   for (i = 0; i < n; ++i)
  63.     list[i] = (gid_t) groups[i];
  64.   return n;
  65. }
  66.