aboutsummaryrefslogtreecommitdiff
path: root/emu/Android/compat/getpwent.c
blob: a3f10bc399599a780d96956236623a5d2df36b0a (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
114
115
116
117
/*
 * getpwent.c -- simulate getpwent() and setpwent()
 */

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

static struct passwd entries[] = {
	{ .pw_name = (char *) "root", .pw_passwd = (char *) "x",
	  .pw_uid = 0, .pw_gid = 0, .pw_dir = (char *) "/",
	  .pw_shell = (char *) "/bin/sh" },
	{ .pw_name = (char *) "fsgqa", .pw_passwd = (char *) "x",
	  .pw_uid = 31415, .pw_gid = 31415, .pw_dir = (char *) "/home/fsgqa",
	  .pw_shell = (char *) "/bin/sh" },
	{ .pw_name = NULL, .pw_passwd = NULL, .pw_uid = 0, .pw_gid = 0,
	  .pw_dir = NULL, .pw_shell = NULL },
};

static struct passwd *current_pw;

struct passwd *getpwent(void)
{
	if (current_pw == NULL)
		current_pw = entries;
	if (current_pw->pw_name == NULL) {
		return NULL;
	}
	return current_pw++;
}

int setpwent(void)
{
	current_pw = NULL;
}

#if 0
void endpwent(void)
{
	current_pw = NULL;
}
#endif

#ifdef DEBUG
struct passwd *getpwnam(const char *name)
{
	struct passwd *cur;
	
	setpwent();
	while ((cur = getpwent()) != NULL) {
		if (strcmp(name, cur->pw_name) == 0)
			return cur;
	}
	return NULL;
}

struct passwd *getpwuid(uid_t uid)
{
	struct passwd *cur;

	setpwent();
	while ((cur = getpwent()) != NULL) {
		if (uid == cur->pw_uid)
			return cur;
	}
	return NULL;
}

static void print_passwd(struct passwd *pw)
{
	if (pw == NULL)
		printf("NULL entry\n");
	else
		printf("%s:%s:%d:%d:%s:%s:%s\n", pw->pw_name, pw->pw_passwd,
		       pw->pw_uid, pw->pw_gid, pw->pw_name, pw->pw_dir,
		       pw->pw_shell);
}

int main(int argc, char **argv)
{
	struct passwd *pw;
	int err;

	pw = getpwnam("root");
	print_passwd(pw);
	if (!pw)
		err++;
	pw = getpwnam("fsgqa");
	print_passwd(pw);
	if (!pw)
		err++;
	pw = getpwnam("foo");
	print_passwd(pw);
	if (pw)
		err++;
	pw = getpwuid(0);
	print_passwd(pw);
	if (!pw)
		err++;
	pw = getpwuid(31415);
	print_passwd(pw);
	if (!pw)
		err++;
	pw = getpwuid(42);
	print_passwd(pw);
	if (pw)
		err++;
	if (err)
		printf("Failed!\n");
	else
		printf("Succeeded!\n");
	return err;
}
#endif