4.3BSD beta release manual page
[unix-history] / usr / src / lib / libc / gen / getpwent.c
CommitLineData
cc8dd839 1/* @(#)getpwent.c 4.6 (Berkeley) 85/01/21 */
a4b07b23
BJ
2#include <stdio.h>
3#include <pwd.h>
499a4b51 4#include <ndbm.h>
a4b07b23 5
a4b07b23
BJ
6static char EMPTY[] = "";
7static FILE *pwf = NULL;
8static char line[BUFSIZ+1];
9static struct passwd passwd;
cc8dd839
S
10
11/*
12 * The following are shared with getpwnamuid.c
13 */
14char *_pw_file = "/etc/passwd";
15DBM *_pw_db;
16int _pw_stayopen;
a4b07b23
BJ
17
18setpwent()
19{
499a4b51 20 if (pwf == NULL)
cc8dd839 21 pwf = fopen(_pw_file, "r");
a4b07b23 22 else
499a4b51 23 rewind(pwf);
a4b07b23
BJ
24}
25
26endpwent()
27{
499a4b51
RC
28 if (pwf != NULL) {
29 fclose(pwf);
a4b07b23
BJ
30 pwf = NULL;
31 }
499a4b51 32 if (_pw_db != (DBM *)0) {
9aabfbe8 33 dbm_close(_pw_db);
499a4b51
RC
34 _pw_db = (DBM *)0;
35 _pw_stayopen = 0;
36 }
a4b07b23
BJ
37}
38
39static char *
40pwskip(p)
41register char *p;
42{
0d5dcbbf 43 while (*p && *p != ':' && *p != '\n')
a4b07b23 44 ++p;
499a4b51
RC
45 if (*p)
46 *p++ = 0;
a4b07b23
BJ
47 return(p);
48}
49
50struct passwd *
51getpwent()
52{
53 register char *p;
54
55 if (pwf == NULL) {
cc8dd839 56 if ((pwf = fopen( _pw_file, "r" )) == NULL)
a4b07b23
BJ
57 return(0);
58 }
59 p = fgets(line, BUFSIZ, pwf);
499a4b51 60 if (p == NULL)
a4b07b23
BJ
61 return(0);
62 passwd.pw_name = p;
63 p = pwskip(p);
64 passwd.pw_passwd = p;
65 p = pwskip(p);
66 passwd.pw_uid = atoi(p);
67 p = pwskip(p);
68 passwd.pw_gid = atoi(p);
69 passwd.pw_quota = 0;
70 passwd.pw_comment = EMPTY;
71 p = pwskip(p);
72 passwd.pw_gecos = p;
73 p = pwskip(p);
74 passwd.pw_dir = p;
75 p = pwskip(p);
76 passwd.pw_shell = p;
499a4b51
RC
77 while (*p && *p != '\n')
78 p++;
a4b07b23
BJ
79 *p = '\0';
80 return(&passwd);
81}
02d7fd83
RC
82
83setpwfile(file)
84 char *file;
85{
cc8dd839 86 _pw_file = file;
02d7fd83 87}