1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
#ifndef _NO_GETPWENT
#include <stdio.h>
#include <sys/types.h>
#include <pwd.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
static struct passwd pw_passwd; /* password structure */
static FILE *passwd_fp;
static char logname[8];
static char password[1024];
static char gecos[1024];
static char dir[1024];
static char shell[1024];
struct passwd *
getpwnam (name)
const char *name;
{
FILE *fp;
char buf[1024];
if ((fp = fopen ("/etc/passwd", "r")) == NULL)
{
return NULL;
}
while (fgets (buf, sizeof (buf), fp))
{
sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n",
logname, password, &pw_passwd.pw_uid,
&pw_passwd.pw_gid, gecos,
dir, shell);
pw_passwd.pw_name = logname;
pw_passwd.pw_passwd = password;
pw_passwd.pw_comment = "";
pw_passwd.pw_gecos = gecos;
pw_passwd.pw_dir = dir;
pw_passwd.pw_shell = shell;
if (!strcmp (logname, name))
{
fclose (fp);
return &pw_passwd;
}
}
fclose (fp);
return NULL;
}
struct passwd *
getpwuid (uid_t uid)
{
FILE *fp;
char buf[1024];
if ((fp = fopen ("/etc/passwd", "r")) == NULL)
{
return NULL;
}
while (fgets (buf, sizeof (buf), fp))
{
sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n",
logname, password, &pw_passwd.pw_uid,
&pw_passwd.pw_gid, gecos,
dir, shell);
pw_passwd.pw_name = logname;
pw_passwd.pw_passwd = password;
pw_passwd.pw_comment = "";
pw_passwd.pw_gecos = gecos;
pw_passwd.pw_dir = dir;
pw_passwd.pw_shell = shell;
if (uid == pw_passwd.pw_uid)
{
fclose (fp);
return &pw_passwd;
}
}
fclose (fp);
return NULL;
}
struct passwd *
getpwent ()
{
char buf[1024];
if (passwd_fp == NULL)
return NULL;
if (fgets (buf, sizeof (buf), passwd_fp) == NULL)
return NULL;
sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n",
logname, password, &pw_passwd.pw_uid,
&pw_passwd.pw_gid, gecos,
dir, shell);
pw_passwd.pw_name = logname;
pw_passwd.pw_passwd = password;
pw_passwd.pw_comment = "";
pw_passwd.pw_gecos = gecos;
pw_passwd.pw_dir = dir;
pw_passwd.pw_shell = shell;
return &pw_passwd;
}
void
setpwent ()
{
if (passwd_fp != NULL)
fclose (passwd_fp);
passwd_fp = fopen ("/etc/passwd", "r");
}
void
endpwent ()
{
if (passwd_fp != NULL)
fclose (passwd_fp);
}
#endif /* !_NO_GETPWENT */
|