aboutsummaryrefslogtreecommitdiff
blob: 81389677b784ad00793678b395be89abe639de96 (plain)
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
43
#include "tests.h"

static int usage(int status)
{
	fputs(
		"Usage: get-group [args]\n"
		" `get-group`         return current gid\n"
		" `get-group file`    return gid of file\n"
		" `get-group -group`  return gid of group name\n"
		, status ? stderr : stdout
	);
	return status;
}

int main(int argc, char *argv[])
{
	switch (argc) {
		case 1:
			printf("%i\n", getgid());
			return 0;

		case 2:
			if (!strcmp(argv[1], "-h"))
				return usage(0);

			if (*argv[1] == '-') {
				const char *name = argv[1] + 1;
				struct group *grp = getgrnam(name);
				if (!grp)
					errp("getgrnam(%s) failed", name);
				printf("%i\n", grp->gr_gid);
			} else {
				const char *file = argv[1];
				struct stat st;
				if (lstat(file, &st))
					errp("lstat(%s) failed", file);
				printf("%i\n", st.st_gid);
			}
			return 0;
	}

	return usage(1);
}