getpwent.c revision 1.4 1 1.4 tsutsui /* $NetBSD: getpwent.c,v 1.4 2001/06/15 17:26:51 tsutsui Exp $ */
2 1.1 gwr
3 1.1 gwr /*
4 1.1 gwr * Copyright (c) 1995 Gordon W. Ross
5 1.1 gwr * All rights reserved.
6 1.1 gwr *
7 1.1 gwr * Redistribution and use in source and binary forms, with or without
8 1.1 gwr * modification, are permitted provided that the following conditions
9 1.1 gwr * are met:
10 1.1 gwr * 1. Redistributions of source code must retain the above copyright
11 1.1 gwr * notice, this list of conditions and the following disclaimer.
12 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 gwr * notice, this list of conditions and the following disclaimer in the
14 1.1 gwr * documentation and/or other materials provided with the distribution.
15 1.1 gwr * 3. The name of the author may not be used to endorse or promote products
16 1.1 gwr * derived from this software without specific prior written permission.
17 1.1 gwr * 4. All advertising materials mentioning features or use of this software
18 1.1 gwr * must display the following acknowledgement:
19 1.1 gwr * This product includes software developed by Gordon W. Ross
20 1.1 gwr *
21 1.1 gwr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 gwr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 gwr * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 gwr * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 gwr * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 gwr * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 gwr * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 gwr * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 gwr * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 gwr * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 gwr */
32 1.1 gwr
33 1.1 gwr /*
34 1.1 gwr * Smaller replacement for: libc/gen/getpwent.c
35 1.1 gwr * Needed by programs like: rsh, rlogin
36 1.1 gwr */
37 1.1 gwr
38 1.4 tsutsui #include <sys/cdefs.h>
39 1.4 tsutsui
40 1.4 tsutsui #ifdef __weak_alias
41 1.4 tsutsui #define endpwent _endpwent
42 1.4 tsutsui #define getpwent _getpwent
43 1.4 tsutsui #define getpwnam _getpwnam
44 1.4 tsutsui #define getpwuid _getpwuid
45 1.4 tsutsui #define setpassent _setpassent
46 1.4 tsutsui #define setpwent _setpwent
47 1.4 tsutsui #endif
48 1.4 tsutsui
49 1.1 gwr #include <stdio.h>
50 1.3 sommerfe #include <stdlib.h>
51 1.1 gwr #include <string.h>
52 1.1 gwr #include <pwd.h>
53 1.4 tsutsui
54 1.4 tsutsui #ifdef __weak_alias
55 1.4 tsutsui __weak_alias(endpwent,_endpwent)
56 1.4 tsutsui __weak_alias(getpwent,_getpwent)
57 1.4 tsutsui __weak_alias(getpwnam,_getpwnam)
58 1.4 tsutsui __weak_alias(getpwuid,_getpwuid)
59 1.4 tsutsui __weak_alias(setpassent,_setpassent)
60 1.4 tsutsui __weak_alias(setpwent,_setpwent)
61 1.4 tsutsui #endif
62 1.1 gwr
63 1.1 gwr #define PWNULL (struct passwd *)0
64 1.1 gwr #define MAXFIELD 8
65 1.1 gwr
66 1.1 gwr static char *pw_file = "/etc/passwd";
67 1.1 gwr static FILE *pw_fp;
68 1.1 gwr static char pw_line[128];
69 1.1 gwr static struct passwd pw_ent;
70 1.1 gwr
71 1.1 gwr /*
72 1.1 gwr * Open passwd file if necessary, and
73 1.1 gwr * get the next entry.
74 1.1 gwr */
75 1.1 gwr struct passwd *
76 1.1 gwr getpwent()
77 1.1 gwr {
78 1.1 gwr char *fv[MAXFIELD];
79 1.2 gwr char *p;
80 1.1 gwr int fc;
81 1.1 gwr
82 1.1 gwr /* Open passwd file if not already. */
83 1.1 gwr if (pw_fp == NULL)
84 1.1 gwr pw_fp = fopen(pw_file, "r");
85 1.1 gwr /* Still NULL. No passwd file? */
86 1.1 gwr if (pw_fp == NULL)
87 1.1 gwr return PWNULL;
88 1.1 gwr
89 1.2 gwr readnext:
90 1.1 gwr /* Read the next line... */
91 1.1 gwr if (fgets(pw_line, sizeof(pw_line), pw_fp) == NULL)
92 1.1 gwr return PWNULL;
93 1.1 gwr
94 1.1 gwr /* ...and parse it. */
95 1.1 gwr p = pw_line;
96 1.1 gwr fc = 0;
97 1.1 gwr while (fc < MAXFIELD) {
98 1.2 gwr fv[fc] = strsep(&p, ":\n");
99 1.2 gwr if (fv[fc] == NULL)
100 1.1 gwr break;
101 1.2 gwr fc++;
102 1.1 gwr }
103 1.1 gwr
104 1.1 gwr /* Need at least 0..5 */
105 1.1 gwr if (fc < 6)
106 1.1 gwr goto readnext;
107 1.1 gwr while (fc < MAXFIELD)
108 1.1 gwr fv[fc++] = "";
109 1.1 gwr
110 1.2 gwr /* Build the pw entry... */
111 1.1 gwr pw_ent.pw_name = fv[0];
112 1.1 gwr pw_ent.pw_passwd = fv[1];
113 1.1 gwr pw_ent.pw_uid = atoi(fv[2]);
114 1.1 gwr pw_ent.pw_gid = atoi(fv[3]);
115 1.1 gwr pw_ent.pw_gecos = fv[4];
116 1.1 gwr pw_ent.pw_dir = fv[5];
117 1.1 gwr pw_ent.pw_shell = fv[6];
118 1.1 gwr
119 1.1 gwr return (&pw_ent);
120 1.1 gwr }
121 1.1 gwr
122 1.1 gwr /* internal for setpwent() */
123 1.1 gwr int
124 1.1 gwr setpassent(stayopen)
125 1.1 gwr int stayopen;
126 1.1 gwr {
127 1.1 gwr if (pw_fp)
128 1.1 gwr rewind(pw_fp);
129 1.3 sommerfe return 1;
130 1.1 gwr }
131 1.1 gwr
132 1.1 gwr /* rewind to the beginning. */
133 1.1 gwr void
134 1.1 gwr setpwent()
135 1.1 gwr {
136 1.1 gwr (void) setpassent(0);
137 1.1 gwr }
138 1.1 gwr
139 1.1 gwr /* done with the passwd file */
140 1.1 gwr void
141 1.1 gwr endpwent()
142 1.1 gwr {
143 1.2 gwr if (pw_fp) {
144 1.1 gwr fclose(pw_fp);
145 1.2 gwr pw_fp = NULL;
146 1.2 gwr }
147 1.1 gwr }
148 1.1 gwr
149 1.1 gwr struct passwd *
150 1.1 gwr getpwnam(name)
151 1.1 gwr const char *name;
152 1.1 gwr {
153 1.1 gwr struct passwd *pw;
154 1.1 gwr
155 1.1 gwr setpwent();
156 1.1 gwr while ((pw = getpwent()) != PWNULL)
157 1.1 gwr if (!strcmp(pw->pw_name, name))
158 1.1 gwr break;
159 1.1 gwr
160 1.1 gwr endpwent();
161 1.1 gwr return(pw);
162 1.1 gwr }
163 1.1 gwr
164 1.1 gwr struct passwd *
165 1.1 gwr getpwuid(uid)
166 1.1 gwr uid_t uid;
167 1.1 gwr {
168 1.1 gwr struct passwd *pw;
169 1.1 gwr
170 1.1 gwr setpwent();
171 1.1 gwr while ((pw = getpwent()) != PWNULL)
172 1.1 gwr if (pw->pw_uid == uid)
173 1.1 gwr break;
174 1.1 gwr
175 1.1 gwr endpwent();
176 1.1 gwr return(pw);
177 1.1 gwr }
178 1.1 gwr
179 1.1 gwr #ifdef TEST_MAIN
180 1.1 gwr main() {
181 1.1 gwr struct passwd *pw;
182 1.1 gwr
183 1.2 gwr printf("#name, password, uid, gid, comment, dir, shell\n");
184 1.1 gwr
185 1.1 gwr while ((pw = getpwent()) != NULL) {
186 1.1 gwr printf("%s:%s:", pw->pw_name, pw->pw_passwd);
187 1.1 gwr printf("%d:%d:", pw->pw_uid, pw->pw_gid);
188 1.1 gwr printf("%s:", pw->pw_gecos);
189 1.1 gwr printf("%s:", pw->pw_dir);
190 1.1 gwr printf("%s\n", pw->pw_shell);
191 1.1 gwr }
192 1.1 gwr }
193 1.1 gwr #endif
194