File: initgroups.c

package info (click to toggle)
samba 3.0.24-6etch10
  • links: PTS
  • area: main
  • in suites: etch
  • size: 49,836 kB
  • ctags: 44,390
  • sloc: ansic: 335,711; sh: 8,133; perl: 7,045; makefile: 3,107; python: 2,370; exp: 1,147; yacc: 881; awk: 486; csh: 58; sed: 45
file content (42 lines) | stat: -rw-r--r-- 750 bytes parent folder | download | duplicates (26)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <unistd.h>
#include <grp.h>
#include <pwd.h>
#include <sys/types.h>

int main(int argc, char **argv)
{
	int result, ngroups, i;
	gid_t *groups;
	struct passwd *pw;

	if (!(pw = getpwnam(argv[1]))) {
		printf("FAIL: no passwd entry for %s\n", argv[1]);
		return 1;
	}

	result = initgroups(argv[1], pw->pw_gid);

	if (result == -1) {
		printf("FAIL");
		return 1;
	}

	ngroups = getgroups(0, NULL);

	groups = (gid_t *)malloc(sizeof(gid_t) * ngroups);
	ngroups = getgroups(ngroups, groups);

	printf("%s is a member of groups:\n", argv[1]);

	for (i = 0; i < ngroups; i++) {
		struct group *grp;

		grp = getgrgid(groups[i]);

		printf("%d (%s)\n", groups[i], grp ? grp->gr_name : "?");
	}

	printf("PASS\n");
	return 0;
}