#include <grp.h>
int getgrouplist(const char *user, gid_t group,
gid_t *groups, int *ngroups);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
getgrouplist():
glibc 2.19 以降:
_DEFAULT_SOURCE
glibc 2.19 以前:
_BSD_SOURCE
引数 group がグループデータベースに user が所属するグループがなかった場合、 getgrouplist() が返すグループのリストに引数 group も追加される。 通常は、この引数にはユーザー user のパスワードレコードに書かれているグループ ID を指定する。
引数 ngroups は、値渡しと結果の両方に使用される引数 (value-result argument) であり、 リターン時には、常に group も含めた user が所属するグループ数が格納される。 この値は groups に格納されたグループ数より大きくなる可能性がある。
If the user is a member of more than *ngroups groups, then getgrouplist() returns -1. In this case, the value returned in *ngroups can be used to resize the buffer passed to a further call to getgrouplist().
| インターフェース | 属性 | 値 |
| getgrouplist() | Thread safety | MT-Safe locale |
$ ./a.out cecilia 0 getgrouplist() returned -1 ngroups = 3 $ ./a.out cecilia 3 ngroups = 3 16 (dialout) 33 (video) 100 (users)
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[])
{
int ngroups;
gid_t *groups;
struct group *gr;
struct passwd *pw;
if (argc != 3) {
fprintf(stderr, "Usage: %s <user> <ngroups>\n", argv[0]);
exit(EXIT_FAILURE);
}
ngroups = atoi(argv[2]);
groups = malloc(sizeof(*groups) * ngroups);
if (groups == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
/* Fetch passwd structure (contains first group ID for user). */
errno = 0;
pw = getpwnam(argv[1]);
if (pw == NULL) {
if (errno)
perror("getpwnam");
else
fprintf(stderr, "no such user\n");
exit(EXIT_FAILURE);
}
/* Retrieve group list. */
if (getgrouplist(argv[1], pw->pw_gid, groups, &ngroups) == -1) {
fprintf(stderr, "getgrouplist() returned -1\n");
fprintf(stderr, "ngroups = %d\n", ngroups);
exit(EXIT_FAILURE);
}
/* Display list of retrieved groups, along with group names. */
fprintf(stderr, "ngroups = %d\n", ngroups);
for (int j = 0; j < ngroups; j++) {
printf("%d", groups[j]);
gr = getgrgid(groups[j]);
if (gr != NULL)
printf(" (%s)", gr->gr_name);
printf("\n");
}
exit(EXIT_SUCCESS);
}