aboutsummaryrefslogtreecommitdiff
path: root/emu/Android/compat/getgrent.c
blob: 8b8ae1f6ee64b36987f9a8bc6f6c7652dd8dde27 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * getgrent.c -- simulate getgrent(), setgrent(), endgrent()
 */

#define __USE_XOPEN

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <grp.h>

static struct group entries[] = {
	{ .gr_name = (char *) "root", .gr_passwd = (char *) "x",
	  .gr_gid = 0, .gr_mem = NULL },
	{ .gr_name = (char *) "fsgqa", .gr_passwd = (char *) "x",
	  .gr_gid = 31415, .gr_mem = NULL },
	{ .gr_name = NULL, .gr_passwd = NULL, .gr_gid = 0, .gr_mem = NULL },
};

static struct group *current_grp;

struct group *getgrent(void)
{
	if (current_grp == NULL)
		current_grp = entries;
	if (current_grp->gr_name == NULL) {
		return NULL;
	}
	return current_grp++;
}

void setgrent(void)
{
	current_grp = NULL;
}

void endgrent(void)
{
	current_grp = NULL;
}

#ifdef DEBUG
struct group *getgrnam(const char *name)
{
	struct group *cur;
	
	setgrent();
	while ((cur = getgrent()) != NULL) {
		if (strcmp(name, cur->gr_name) == 0)
			return cur;
	}
	return NULL;
}

struct group *getgrgid(gid_t gid)
{
	struct group *cur;

	setgrent();
	while ((cur = getgrent()) != NULL) {
		if (gid == cur->gr_gid)
			return cur;
	}
	return NULL;
}

static void print_group(struct group *grp)
{
	if (grp == NULL)
		printf("NULL entry\n");
	else
		printf("%s:%s:%d\n", grp->gr_name,
		       grp->gr_passwd, grp->gr_gid);
}

int main(int argc, char **argv)
{
	struct group *grp;
	int err;

	grp = getgrnam("root");
	print_group(grp);
	if (!grp)
		err++;
	grp = getgrnam("fsgqa");
	print_group(grp);
	if (!grp)
		err++;
	grp = getgrnam("foo");
	print_group(grp);
	if (grp)
		err++;
	grp = getgrgid(0);
	print_group(grp);
	if (!grp)
		err++;
	grp = getgrgid(31415);
	print_group(grp);
	if (!grp)
		err++;
	grp = getgrgid(42);
	print_group(grp);
	if (grp)
		err++;
	if (err)
		printf("Failed!\n");
	else
		printf("Succeeded!\n");
	return err;
}
#endif