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