ualarm
[unix-history] / usr / src / lib / libc / gen / getpwent.c
CommitLineData
b8f253e8
KM
1/*
2 * Copyright (c) 1984 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
2ce81398
DS
7#if defined(LIBC_SCCS) && !defined(lint)
8static char sccsid[] = "@(#)getpwent.c 5.2 (Berkeley) %G%";
9#endif LIBC_SCCS and not lint
b8f253e8 10
a4b07b23
BJ
11#include <stdio.h>
12#include <pwd.h>
499a4b51 13#include <ndbm.h>
a4b07b23 14
a4b07b23
BJ
15static char EMPTY[] = "";
16static FILE *pwf = NULL;
17static char line[BUFSIZ+1];
18static struct passwd passwd;
cc8dd839
S
19
20/*
21 * The following are shared with getpwnamuid.c
22 */
23char *_pw_file = "/etc/passwd";
24DBM *_pw_db;
25int _pw_stayopen;
a4b07b23
BJ
26
27setpwent()
28{
499a4b51 29 if (pwf == NULL)
cc8dd839 30 pwf = fopen(_pw_file, "r");
a4b07b23 31 else
499a4b51 32 rewind(pwf);
a4b07b23
BJ
33}
34
35endpwent()
36{
499a4b51
RC
37 if (pwf != NULL) {
38 fclose(pwf);
a4b07b23
BJ
39 pwf = NULL;
40 }
499a4b51 41 if (_pw_db != (DBM *)0) {
9aabfbe8 42 dbm_close(_pw_db);
499a4b51
RC
43 _pw_db = (DBM *)0;
44 _pw_stayopen = 0;
45 }
a4b07b23
BJ
46}
47
48static char *
49pwskip(p)
50register char *p;
51{
0d5dcbbf 52 while (*p && *p != ':' && *p != '\n')
a4b07b23 53 ++p;
499a4b51
RC
54 if (*p)
55 *p++ = 0;
a4b07b23
BJ
56 return(p);
57}
58
59struct passwd *
60getpwent()
61{
62 register char *p;
63
64 if (pwf == NULL) {
cc8dd839 65 if ((pwf = fopen( _pw_file, "r" )) == NULL)
a4b07b23
BJ
66 return(0);
67 }
68 p = fgets(line, BUFSIZ, pwf);
499a4b51 69 if (p == NULL)
a4b07b23
BJ
70 return(0);
71 passwd.pw_name = p;
72 p = pwskip(p);
73 passwd.pw_passwd = p;
74 p = pwskip(p);
75 passwd.pw_uid = atoi(p);
76 p = pwskip(p);
77 passwd.pw_gid = atoi(p);
78 passwd.pw_quota = 0;
79 passwd.pw_comment = EMPTY;
80 p = pwskip(p);
81 passwd.pw_gecos = p;
82 p = pwskip(p);
83 passwd.pw_dir = p;
84 p = pwskip(p);
85 passwd.pw_shell = p;
499a4b51
RC
86 while (*p && *p != '\n')
87 p++;
a4b07b23
BJ
88 *p = '\0';
89 return(&passwd);
90}
02d7fd83
RC
91
92setpwfile(file)
93 char *file;
94{
cc8dd839 95 _pw_file = file;
02d7fd83 96}