Commit | Line | Data |
---|---|---|
a4b07b23 BJ |
1 | /* @(#)getpwent.c 4.1 (Berkeley) %G% */ |
2 | #include <stdio.h> | |
3 | #include <pwd.h> | |
4 | ||
5 | static char PASSWD[] = "/etc/passwd"; | |
6 | static char EMPTY[] = ""; | |
7 | static FILE *pwf = NULL; | |
8 | static char line[BUFSIZ+1]; | |
9 | static struct passwd passwd; | |
10 | ||
11 | setpwent() | |
12 | { | |
13 | if( pwf == NULL ) | |
14 | pwf = fopen( PASSWD, "r" ); | |
15 | else | |
16 | rewind( pwf ); | |
17 | } | |
18 | ||
19 | endpwent() | |
20 | { | |
21 | if( pwf != NULL ){ | |
22 | fclose( pwf ); | |
23 | pwf = NULL; | |
24 | } | |
25 | } | |
26 | ||
27 | static char * | |
28 | pwskip(p) | |
29 | register char *p; | |
30 | { | |
31 | while( *p && *p != ':' ) | |
32 | ++p; | |
33 | if( *p ) *p++ = 0; | |
34 | return(p); | |
35 | } | |
36 | ||
37 | struct passwd * | |
38 | getpwent() | |
39 | { | |
40 | register char *p; | |
41 | ||
42 | if (pwf == NULL) { | |
43 | if( (pwf = fopen( PASSWD, "r" )) == NULL ) | |
44 | return(0); | |
45 | } | |
46 | p = fgets(line, BUFSIZ, pwf); | |
47 | if (p==NULL) | |
48 | return(0); | |
49 | passwd.pw_name = p; | |
50 | p = pwskip(p); | |
51 | passwd.pw_passwd = p; | |
52 | p = pwskip(p); | |
53 | passwd.pw_uid = atoi(p); | |
54 | p = pwskip(p); | |
55 | passwd.pw_gid = atoi(p); | |
56 | passwd.pw_quota = 0; | |
57 | passwd.pw_comment = EMPTY; | |
58 | p = pwskip(p); | |
59 | passwd.pw_gecos = p; | |
60 | p = pwskip(p); | |
61 | passwd.pw_dir = p; | |
62 | p = pwskip(p); | |
63 | passwd.pw_shell = p; | |
64 | while(*p && *p != '\n') p++; | |
65 | *p = '\0'; | |
66 | return(&passwd); | |
67 | } |