![]() Chapter Contents  | 
![]() Previous  | 
![]() Next  | 
| getgroups | 
| Portability: | POSIX.1 conforming | 
| SYNOPSIS | |
| DESCRIPTION | |
| RETURN VALUE | |
| EXAMPLE | |
| RELATED FUNCTIONS | 
| SYNOPSIS | 
#include <sys/types.h> #include <unistd.h> int getgroups(int listSize, gid_t *groupList);
| DESCRIPTION | 
getgroups
 stores the supplementary group IDs of the calling process
in an array.
listSize
groupList
Note:   
Note
that 
groupList
 is declared
as 
gid_t *groupList
 so
that NULL may be passed to 
groupList
 when 
listSize
 is 
0
. This could not be done if 
groupList
 was declared as an
array.  ![[cautionend]](../common/images/cautend.gif)
| RETURN VALUE | 
getgroups
 returns the number of supplementary group IDs in 
groupList
.  This value is always less than or
equal to the value of 
NGROUPS_MAX
 defined in 
<limits.h>
.  If 
listSize
 is 
0
, 
getgroups
 returns the total number of supplementary group IDs and does
not store the group IDs in an array.  
getgroups
 returns a 
-1
 if unsuccessful.
| EXAMPLE | 
The following example illustrates the
use of 
getgroups
 to determine
supplementary group IDs.  
Note:   
You must specify the 
posix
 option when compiling this
example.  ![[cautionend]](../common/images/cautend.gif)
 
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <limits.h>
main()
{
   gid_t groupIDs[NGROUPS_MAX];
   int i, count;
   if ((count = getgroups(NGROUPS_MAX, groupIDs)) == -1)
      perror("getgroups error");
   else
      for (i = 0; i < count; i++)
         printf("Group ID %d = %d\n", i + 1, (int) groupIDs[i]);
}
| RELATED FUNCTIONS | 
![]() Chapter Contents  | 
![]() Previous  | 
![]() Next  | 
![]() Top of Page  | 
Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.