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