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