k9core

Coreutils for *nix operating systems [fork]
git clone git://bvnf.space/k9core.git
Log | Files | Refs | LICENSE

groups.c (912B)


      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 <errno.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{
	struct passwd *pwd;
	errno = 0;
	if(argc == 2)
		pwd = getpwnam(argv[1]);
	else
		pwd = getpwuid(getuid());
	if(pwd == NULL) {
		fprintf(stderr,
			   "groups: %s: %s\n",
			   argv[1],
			   errno == 0 ? "user not found" : strerror(errno));
		return 1;
	}

	int ngroups = 10;
	gid_t *groups = malloc(ngroups * sizeof(*groups));
	if(getgrouplist(pwd->pw_name, pwd->pw_gid, groups, &ngroups) == -1) {
		/* the user is a member of more than ngroups groups */
		ngroups += 10;
		groups = malloc(ngroups * sizeof(*groups));
		getgrouplist(pwd->pw_name, pwd->pw_gid, groups, &ngroups);
	}
	struct group *gr;
	for(int i = 0; i < ngroups; i++) {
		gr = getgrgid(groups[i]);
		printf("%s ", gr->gr_name);
	}
	printf("\n");
	free(groups);
	return 0;
}