source: trunk/minix/lib/other/getpwent.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 3.2 KB
Line 
1/* getpwent(), getpwuid(), getpwnam() - password file routines
2 *
3 * Author: Kees J. Bot
4 * 31 Jan 1994
5 */
6#define nil 0
7#define open _open
8#define fcntl _fcntl
9#define read _read
10#define close _close
11#include <sys/types.h>
12#include <pwd.h>
13#include <string.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <fcntl.h>
17
18#define arraysize(a) (sizeof(a) / sizeof((a)[0]))
19#define arraylimit(a) ((a) + arraysize(a))
20
21static char PASSWD[]= "/etc/passwd"; /* The password file. */
22static const char *pwfile; /* Current password file. */
23
24static char buf[1024]; /* Read buffer. */
25static char pwline[256]; /* One line from the password file. */
26static struct passwd entry; /* Entry to fill and return. */
27static int pwfd= -1; /* Filedescriptor to the file. */
28static char *bufptr; /* Place in buf. */
29static ssize_t buflen= 0; /* Remaining characters in buf. */
30static char *lineptr; /* Place in the line. */
31
32void endpwent(void)
33/* Close the password file. */
34{
35 if (pwfd >= 0) {
36 (void) close(pwfd);
37 pwfd= -1;
38 buflen= 0;
39 }
40}
41
42int setpwent(void)
43/* Open the password file. */
44{
45 if (pwfd >= 0) endpwent();
46
47 if (pwfile == nil) pwfile= PASSWD;
48
49 if ((pwfd= open(pwfile, O_RDONLY)) < 0) return -1;
50 (void) fcntl(pwfd, F_SETFD, fcntl(pwfd, F_GETFD) | FD_CLOEXEC);
51 return 0;
52}
53
54void setpwfile(const char *file)
55/* Prepare for reading an alternate password file. */
56{
57 endpwent();
58 pwfile= file;
59}
60
61static int getline(void)
62/* Get one line from the password file, return 0 if bad or EOF. */
63{
64 lineptr= pwline;
65
66 do {
67 if (buflen == 0) {
68 if ((buflen= read(pwfd, buf, sizeof(buf))) <= 0)
69 return 0;
70 bufptr= buf;
71 }
72
73 if (lineptr == arraylimit(pwline)) return 0;
74 buflen--;
75 } while ((*lineptr++ = *bufptr++) != '\n');
76
77 lineptr= pwline;
78 return 1;
79}
80
81static char *scan_colon(void)
82/* Scan for a field separator in a line, return the start of the field. */
83{
84 char *field= lineptr;
85 char *last;
86
87 for (;;) {
88 last= lineptr;
89 if (*lineptr == 0) return nil;
90 if (*lineptr == '\n') break;
91 if (*lineptr++ == ':') break;
92 }
93 *last= 0;
94 return field;
95}
96
97struct passwd *getpwent(void)
98/* Read one entry from the password file. */
99{
100 char *p;
101
102 /* Open the file if not yet open. */
103 if (pwfd < 0 && setpwent() < 0) return nil;
104
105 /* Until a good line is read. */
106 for (;;) {
107 if (!getline()) return nil; /* EOF or corrupt. */
108
109 if ((entry.pw_name= scan_colon()) == nil) continue;
110 if ((entry.pw_passwd= scan_colon()) == nil) continue;
111 if ((p= scan_colon()) == nil) continue;
112 entry.pw_uid= strtol(p, nil, 0);
113 if ((p= scan_colon()) == nil) continue;
114 entry.pw_gid= strtol(p, nil, 0);
115 if ((entry.pw_gecos= scan_colon()) == nil) continue;
116 if ((entry.pw_dir= scan_colon()) == nil) continue;
117 if ((entry.pw_shell= scan_colon()) == nil) continue;
118
119 if (*lineptr == 0) return &entry;
120 }
121}
122
123struct passwd *getpwuid(_mnx_Uid_t uid)
124/* Return the password file entry belonging to the user-id. */
125{
126 struct passwd *pw;
127
128 endpwent();
129 while ((pw= getpwent()) != nil && pw->pw_uid != uid) {}
130 endpwent();
131 return pw;
132}
133
134struct passwd *getpwnam(const char *name)
135/* Return the password file entry belonging to the user name. */
136{
137 struct passwd *pw;
138
139 endpwent();
140 while ((pw= getpwent()) != nil && strcmp(pw->pw_name, name) != 0) {}
141 endpwent();
142 return pw;
143}
Note: See TracBrowser for help on using the repository browser.