getpwent.c revision 1.1.1.1.2.1 1 1.1.1.1.2.1 gwr /* $NetBSD: getpwent.c,v 1.1.1.1.2.1 1995/10/11 23:52:08 gwr 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.1 gwr #include <stdio.h>
39 1.1 gwr #include <string.h>
40 1.1 gwr #include <pwd.h>
41 1.1 gwr
42 1.1 gwr #define PWNULL (struct passwd *)0
43 1.1 gwr #define MAXFIELD 8
44 1.1 gwr
45 1.1 gwr static char *pw_file = "/etc/passwd";
46 1.1 gwr static FILE *pw_fp;
47 1.1 gwr static char pw_line[128];
48 1.1 gwr static struct passwd pw_ent;
49 1.1 gwr
50 1.1 gwr /*
51 1.1 gwr * Open passwd file if necessary, and
52 1.1 gwr * get the next entry.
53 1.1 gwr */
54 1.1 gwr struct passwd *
55 1.1 gwr getpwent()
56 1.1 gwr {
57 1.1 gwr char *fv[MAXFIELD];
58 1.1.1.1.2.1 gwr char *p;
59 1.1 gwr int fc;
60 1.1 gwr
61 1.1 gwr /* Open passwd file if not already. */
62 1.1 gwr if (pw_fp == NULL)
63 1.1 gwr pw_fp = fopen(pw_file, "r");
64 1.1 gwr /* Still NULL. No passwd file? */
65 1.1 gwr if (pw_fp == NULL)
66 1.1 gwr return PWNULL;
67 1.1 gwr
68 1.1 gwr readnext:
69 1.1.1.1.2.1 gwr /* Read the next line... */
70 1.1 gwr if (fgets(pw_line, sizeof(pw_line), pw_fp) == NULL)
71 1.1 gwr return PWNULL;
72 1.1 gwr
73 1.1 gwr /* ...and parse it. */
74 1.1 gwr p = pw_line;
75 1.1 gwr fc = 0;
76 1.1 gwr while (fc < MAXFIELD) {
77 1.1.1.1.2.1 gwr fv[fc] = strsep(&p, ":\n");
78 1.1.1.1.2.1 gwr if (fv[fc] == NULL)
79 1.1 gwr break;
80 1.1.1.1.2.1 gwr fc++;
81 1.1 gwr }
82 1.1 gwr
83 1.1 gwr /* Need at least 0..5 */
84 1.1 gwr if (fc < 6)
85 1.1 gwr goto readnext;
86 1.1 gwr while (fc < MAXFIELD)
87 1.1 gwr fv[fc++] = "";
88 1.1 gwr
89 1.1.1.1.2.1 gwr /* Build the pw entry... */
90 1.1 gwr pw_ent.pw_name = fv[0];
91 1.1 gwr pw_ent.pw_passwd = fv[1];
92 1.1 gwr pw_ent.pw_uid = atoi(fv[2]);
93 1.1 gwr pw_ent.pw_gid = atoi(fv[3]);
94 1.1 gwr pw_ent.pw_gecos = fv[4];
95 1.1 gwr pw_ent.pw_dir = fv[5];
96 1.1 gwr pw_ent.pw_shell = fv[6];
97 1.1 gwr
98 1.1 gwr return (&pw_ent);
99 1.1 gwr }
100 1.1 gwr
101 1.1 gwr /* internal for setpwent() */
102 1.1 gwr int
103 1.1 gwr setpassent(stayopen)
104 1.1 gwr int stayopen;
105 1.1 gwr {
106 1.1 gwr if (pw_fp)
107 1.1 gwr rewind(pw_fp);
108 1.1 gwr }
109 1.1 gwr
110 1.1 gwr /* rewind to the beginning. */
111 1.1 gwr void
112 1.1 gwr setpwent()
113 1.1 gwr {
114 1.1 gwr (void) setpassent(0);
115 1.1 gwr }
116 1.1 gwr
117 1.1 gwr /* done with the passwd file */
118 1.1 gwr void
119 1.1 gwr endpwent()
120 1.1 gwr {
121 1.1.1.1.2.1 gwr if (pw_fp) {
122 1.1 gwr fclose(pw_fp);
123 1.1.1.1.2.1 gwr pw_fp = NULL;
124 1.1.1.1.2.1 gwr }
125 1.1 gwr }
126 1.1 gwr
127 1.1 gwr struct passwd *
128 1.1 gwr getpwnam(name)
129 1.1 gwr const char *name;
130 1.1 gwr {
131 1.1 gwr struct passwd *pw;
132 1.1 gwr
133 1.1 gwr setpwent();
134 1.1 gwr while ((pw = getpwent()) != PWNULL)
135 1.1 gwr if (!strcmp(pw->pw_name, name))
136 1.1 gwr break;
137 1.1 gwr
138 1.1 gwr endpwent();
139 1.1 gwr return(pw);
140 1.1 gwr }
141 1.1 gwr
142 1.1 gwr struct passwd *
143 1.1 gwr getpwuid(uid)
144 1.1 gwr uid_t uid;
145 1.1 gwr {
146 1.1 gwr struct passwd *pw;
147 1.1 gwr
148 1.1 gwr setpwent();
149 1.1 gwr while ((pw = getpwent()) != PWNULL)
150 1.1 gwr if (pw->pw_uid == uid)
151 1.1 gwr break;
152 1.1 gwr
153 1.1 gwr endpwent();
154 1.1 gwr return(pw);
155 1.1 gwr }
156 1.1 gwr
157 1.1 gwr #ifdef TEST_MAIN
158 1.1 gwr main() {
159 1.1 gwr struct passwd *pw;
160 1.1 gwr
161 1.1.1.1.2.1 gwr printf("#name, password, uid, gid, comment, dir, shell\n");
162 1.1 gwr
163 1.1 gwr while ((pw = getpwent()) != NULL) {
164 1.1 gwr printf("%s:%s:", pw->pw_name, pw->pw_passwd);
165 1.1 gwr printf("%d:%d:", pw->pw_uid, pw->pw_gid);
166 1.1 gwr printf("%s:", pw->pw_gecos);
167 1.1 gwr printf("%s:", pw->pw_dir);
168 1.1 gwr printf("%s\n", pw->pw_shell);
169 1.1 gwr }
170 1.1 gwr }
171 1.1 gwr #endif
172