2004-11-03 13:51:07 +00:00
|
|
|
.\" Copyright 2002 Walter Harms (walter.harms@informatik.uni-oldenburg.de)
|
|
|
|
.\" Distributed under GPL
|
|
|
|
.\" Thanks to glibc info pages
|
|
|
|
.\"
|
|
|
|
.\" Modified 2003-11-18, aeb: glibc is broken
|
2004-12-17 10:17:35 +00:00
|
|
|
.TH GETGROUPLIST 3 2003-11-18 "GNU" "Linux Programmer's Manual"
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH NAME
|
|
|
|
getgrouplist \- list of groups a user belongs to
|
|
|
|
.SH SYNOPSIS
|
2007-04-12 22:42:49 +00:00
|
|
|
.sp
|
2004-11-03 13:51:07 +00:00
|
|
|
.B #include <grp.h>
|
|
|
|
.sp
|
2007-04-03 15:32:52 +00:00
|
|
|
.BI "int getgrouplist(const char *" user ", gid_t " group ,
|
|
|
|
.in 24
|
2004-11-03 13:51:07 +00:00
|
|
|
.BI "gid_t *" groups ", int *" ngroups );
|
|
|
|
.SH DESCRIPTION
|
2007-04-12 22:42:49 +00:00
|
|
|
The
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR getgrouplist ()
|
2004-11-03 13:51:07 +00:00
|
|
|
function scans the group database for all the groups
|
|
|
|
.I user
|
2007-04-12 22:42:49 +00:00
|
|
|
belongs to.
|
|
|
|
Up to
|
2004-11-03 13:51:07 +00:00
|
|
|
.RI * ngroups
|
|
|
|
group IDs corresponding to these groups are stored in the array
|
|
|
|
.IR groups ;
|
|
|
|
the return value from the function is the number of group IDs
|
2007-04-12 22:42:49 +00:00
|
|
|
actually stored.
|
|
|
|
The group
|
2004-11-03 13:51:07 +00:00
|
|
|
.I group
|
2007-04-12 22:42:49 +00:00
|
|
|
is automatically included in the list of groups returned by
|
2005-10-19 14:54:31 +00:00
|
|
|
.BR getgrouplist ().
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "RETURN VALUE"
|
|
|
|
If
|
2007-04-12 22:42:49 +00:00
|
|
|
.RI * ngroups
|
2004-11-03 13:51:07 +00:00
|
|
|
is smaller than the total number of groups found, then
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR getgrouplist ()
|
2005-07-07 08:27:03 +00:00
|
|
|
returns a value of `\-1'.
|
2004-11-03 13:51:07 +00:00
|
|
|
In all cases the actual number of groups is stored in
|
|
|
|
.RI * ngroups .
|
2007-05-19 04:30:20 +00:00
|
|
|
.SH "VERSIONS"
|
|
|
|
This function is present since glibc 2.2.4.
|
|
|
|
.SH "CONFORMING TO"
|
|
|
|
This function is non-standard; it appears on most BSDs.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH BUGS
|
|
|
|
The glibc 2.3.2 implementation of this function is broken:
|
|
|
|
it overwrites memory when the actual number of groups is larger than
|
|
|
|
.RI * ngroups .
|
|
|
|
.SH EXAMPLE
|
|
|
|
.nf
|
|
|
|
/* This crashes with glibc 2.3.2 */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
|
2007-04-12 22:42:49 +00:00
|
|
|
int
|
|
|
|
main(void)
|
2007-04-05 12:36:57 +00:00
|
|
|
{
|
|
|
|
int i, ng = 0;
|
|
|
|
char *user = "who"; /* username here */
|
|
|
|
gid_t *groups = NULL;
|
|
|
|
struct passwd *pw = getpwnam(user);
|
2004-11-03 13:51:07 +00:00
|
|
|
|
2007-04-05 12:36:57 +00:00
|
|
|
if (pw == NULL)
|
2007-05-16 02:09:09 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
2004-11-03 13:51:07 +00:00
|
|
|
|
2007-04-05 12:36:57 +00:00
|
|
|
if (getgrouplist(user, pw->pw_gid, NULL, &ng) < 0) {
|
|
|
|
groups = (gid_t *) malloc(ng * sizeof (gid_t));
|
|
|
|
getgrouplist(user, pw->pw_gid, groups, &ng);
|
|
|
|
}
|
2004-11-03 13:51:07 +00:00
|
|
|
|
2007-04-05 12:36:57 +00:00
|
|
|
for(i = 0; i < ng; i++)
|
|
|
|
printf("%d\en", groups[i]);
|
|
|
|
|
2007-05-16 02:09:09 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
2004-11-03 13:51:07 +00:00
|
|
|
}
|
|
|
|
.fi
|
|
|
|
.SH "SEE ALSO"
|
|
|
|
.BR getgroups (3),
|
|
|
|
.BR setgroups (3)
|