Drop main() prototype. Syncs with NetBSD-8
[minix.git] / tests / usr.bin / id / pwgr.c
blob364f64226fbb70cff0675e7d60370839ef8a168d
1 /* $NetBSD: pwgr.c,v 1.1 2012/03/17 16:33:14 jruoho Exp $ */
2 /*
3 * Copyright (c) 2007 The NetBSD Foundation, Inc.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
29 * This file implements replacements for all user/group-related functions
30 * called by id(1). It provides fake but deterministic user and group
31 * information. The details are as such:
32 * User root, uid 0, primary group 0 (wheel).
33 * User test, uid 100, primary group 100 (users), secondary group 0 (wheel).
36 #include <sys/types.h>
38 #include <errno.h>
39 #include <grp.h>
40 #include <pwd.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <string.h>
45 char Login[16];
46 struct group GrEntry;
47 struct passwd PwEntry;
49 gid_t
50 getgid(void)
52 return 100;
55 gid_t
56 getegid(void)
58 if (getenv("LIBFAKE_EGID_ROOT") != NULL)
59 return 0;
60 else
61 return 100;
64 uid_t
65 getuid(void)
67 return 100;
70 uid_t
71 geteuid(void)
73 if (getenv("LIBFAKE_EUID_ROOT") != NULL)
74 return 0;
75 else
76 return 100;
79 char *
80 getlogin(void)
82 strcpy(Login, "test");
83 return Login;
86 struct group *
87 getgrgid(gid_t gid)
89 struct group *g = &GrEntry;
91 memset(g, 0, sizeof(*g));
92 if (gid == 0) {
93 g->gr_name = __UNCONST("wheel");
94 g->gr_gid = 0;
95 } else if (gid == 100) {
96 g->gr_name = __UNCONST("users");
97 g->gr_gid = 100;
98 } else
99 g = NULL;
101 return g;
105 getgrouplist(const char *name, gid_t basegid, gid_t *groups, int *ngroups)
107 int cnt, ret;
109 if (strcmp(name, "root") == 0) {
110 if (*ngroups >= 1) {
111 groups[0] = basegid;
112 cnt = 1;
115 ret = (*ngroups >= cnt) ? 0 : -1;
116 *ngroups = cnt;
117 } else if (strcmp(name, "test") == 0) {
118 if (*ngroups >= 1) {
119 groups[0] = basegid;
120 cnt = 1;
123 if (*ngroups >= 2) {
124 groups[1] = 0;
125 cnt = 2;
128 ret = (*ngroups >= cnt) ? 0 : -1;
129 *ngroups = cnt;
130 } else
131 ret = -1;
133 return ret;
137 getgroups(int gidsetlen, gid_t *gidset)
139 if (gidsetlen < 2) {
140 errno = EINVAL;
141 return -1;
144 gidset[0] = 100;
145 gidset[1] = 0;
146 return 2;
149 struct passwd *
150 getpwnam(const char *login)
152 struct passwd *p = &PwEntry;
154 memset(p, 0, sizeof(*p));
155 if (strcmp(login, "root") == 0) {
156 p->pw_name = __UNCONST("root");
157 p->pw_uid = 0;
158 p->pw_gid = 0;
159 } else if (strcmp(login, "test") == 0) {
160 p->pw_name = __UNCONST("test");
161 p->pw_uid = 100;
162 p->pw_gid = 100;
163 } else
164 p = NULL;
166 return p;
169 struct passwd *
170 getpwuid(uid_t uid)
172 struct passwd *p = &PwEntry;
174 memset(p, 0, sizeof(*p));
175 if (uid == 0) {
176 p->pw_name = __UNCONST("root");
177 p->pw_uid = 0;
178 p->pw_gid = 0;
179 } else if (uid == 100) {
180 p->pw_name = __UNCONST("test");
181 p->pw_uid = 100;
182 p->pw_gid = 100;
183 } else
184 p = NULL;
186 return p;