|
|
1.1 root 1: /*
2: * libc/gen/getpwent.c
3: * Coherent I/O Library.
4: * Get password file entry.
5: * Searches by next entry, name or numerical id.
6: */
7:
8: #include <stdio.h>
9: #include <pwd.h>
10:
11: #define field(x) { x=cp; while (*cp++); }
12: #define NPWLINE 120
13: #define PWFILE "/etc/passwd"
14:
15: static char pwline[NPWLINE];
16: static struct passwd pw;
17: static FILE *pwfile = { NULL };
18:
19: struct passwd *
20: getpwnam(name)
21: char *name;
22: {
23: register struct passwd *pwp;
24:
25: setpwent();
26: while ((pwp = getpwent()) != NULL)
27: if (streq(name, pwp->pw_name))
28: return pwp;
29: return NULL;
30: }
31:
32: struct passwd *
33: getpwuid(uid)
34: {
35: register struct passwd *pwp;
36:
37: setpwent();
38: while ((pwp = getpwent()) != NULL)
39: if (uid == pwp->pw_uid)
40: return pwp;
41: return NULL;
42: }
43:
44: struct passwd *
45: getpwent()
46: {
47: register char *cp, *xp;
48: register int c, nseps;
49:
50: if (pwfile == NULL)
51: if ((pwfile = fopen(PWFILE, "r")) == NULL)
52: return NULL;
53: again:
54: cp = pwline;
55: for (nseps = 0; (c = getc(pwfile))!=EOF && c!='\n'; ) {
56: if (c == ':') {
57: c = '\0';
58: ++nseps;
59: }
60: if (cp < &pwline[NPWLINE-1])
61: *cp++ = c;
62: }
63: if (c == EOF)
64: return NULL;
65: if (nseps != 6)
66: goto again; /* accept only lines with six ':'s */
67: *cp = '\0';
68: cp = pwline;
69: field(pw.pw_name);
70: field(pw.pw_passwd);
71: field(xp);
72: pw.pw_uid = atoi(xp);
73: field(xp);
74: pw.pw_gid = atoi(xp);
75: field(pw.pw_comment);
76: pw.pw_gecos = pw.pw_comment;
77: field(pw.pw_dir);
78: field(pw.pw_shell);
79: return &pw;
80: }
81:
82: setpwent()
83: {
84: if (pwfile != NULL)
85: rewind(pwfile);
86: }
87:
88: endpwent()
89: {
90: if (pwfile != NULL) {
91: fclose(pwfile);
92: pwfile = NULL;
93: }
94: }
95:
96: /* end of getpwent.c */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.