]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getgrouplist.3
Added/updated glibc feature test macro requirements
[thirdparty/man-pages.git] / man3 / getgrouplist.3
CommitLineData
fea681da
MK
1.\" Copyright 2002 Walter Harms (walter.harms@informatik.uni-oldenburg.de)
2.\" Distributed under GPL
3.\" Thanks to glibc info pages
4.\"
5.\" Modified 2003-11-18, aeb: glibc is broken
cc4615cc 6.TH GETGROUPLIST 3 2007-07-26 "GNU" "Linux Programmer's Manual"
fea681da
MK
7.SH NAME
8getgrouplist \- list of groups a user belongs to
9.SH SYNOPSIS
c13182ef 10.sp
fea681da
MK
11.B #include <grp.h>
12.sp
b9f02710 13.BI "int getgrouplist(const char *" user ", gid_t " group ,
cc4615cc
MK
14.br
15.BI " gid_t *" groups ", int *" ngroups );
16.sp
17.in -4n
18Feature Test Macro Requirements for glibc (see
19.BR feature_test_macros (7)):
20.in
21.sp
22.BR getgrouplist ():
23_BSD_SOURCE
fea681da 24.SH DESCRIPTION
c13182ef 25The
63aa9df0 26.BR getgrouplist ()
fea681da
MK
27function scans the group database for all the groups
28.I user
c13182ef
MK
29belongs to.
30Up to
fea681da
MK
31.RI * ngroups
32group IDs corresponding to these groups are stored in the array
33.IR groups ;
34the return value from the function is the number of group IDs
c13182ef
MK
35actually stored.
36The group
fea681da 37.I group
c13182ef 38is automatically included in the list of groups returned by
4d52e8f8 39.BR getgrouplist ().
fea681da
MK
40.SH "RETURN VALUE"
41If
c13182ef 42.RI * ngroups
fea681da 43is smaller than the total number of groups found, then
63aa9df0 44.BR getgrouplist ()
2b0fa182 45returns \-1.
fea681da
MK
46In all cases the actual number of groups is stored in
47.RI * ngroups .
2b2581ee
MK
48.SH "VERSIONS"
49This function is present since glibc 2.2.4.
50.SH "CONFORMING TO"
51This function is non-standard; it appears on most BSDs.
fea681da
MK
52.SH BUGS
53The glibc 2.3.2 implementation of this function is broken:
54it overwrites memory when the actual number of groups is larger than
55.RI * ngroups .
fea681da
MK
56.SH EXAMPLE
57.nf
58/* This crashes with glibc 2.3.2 */
59#include <stdio.h>
60#include <stdlib.h>
61#include <grp.h>
62#include <pwd.h>
63
c13182ef
MK
64int
65main(void)
cf0a9ace
MK
66{
67 int i, ng = 0;
68 char *user = "who"; /* username here */
69 gid_t *groups = NULL;
70 struct passwd *pw = getpwnam(user);
fea681da 71
cf0a9ace 72 if (pw == NULL)
5bc8c34c 73 exit(EXIT_SUCCESS);
fea681da 74
29059a65 75 if (getgrouplist(user, pw\->pw_gid, NULL, &ng) < 0) {
cf0a9ace 76 groups = (gid_t *) malloc(ng * sizeof (gid_t));
29059a65 77 getgrouplist(user, pw\->pw_gid, groups, &ng);
cf0a9ace 78 }
fea681da 79
d4949190 80 for (i = 0; i < ng; i++)
cf0a9ace
MK
81 printf("%d\en", groups[i]);
82
5bc8c34c 83 exit(EXIT_SUCCESS);
fea681da
MK
84}
85.fi
86.SH "SEE ALSO"
f56f5b89
MK
87.BR getgroups (2),
88.BR setgroups (2)