|
|
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 c;
49:
50: if (pwfile == NULL)
51: if ((pwfile = fopen(PWFILE, "r")) == NULL)
52: return NULL;
53: cp = pwline;
54: while ((c = getc(pwfile))!=EOF && c!='\n') {
55: if (c == ':')
56: c = '\0';
57: if (cp < &pwline[NPWLINE-1])
58: *cp++ = c;
59: }
60: if (c == EOF)
61: return NULL;
62: *cp = '\0';
63: cp = pwline;
64: field(pw.pw_name);
65: field(pw.pw_passwd);
66: field(xp);
67: pw.pw_uid = atoi(xp);
68: field(xp);
69: pw.pw_gid = atoi(xp);
70: field(pw.pw_comment);
71: field(pw.pw_dir);
72: field(pw.pw_shell);
73: return &pw;
74: }
75:
76: setpwent()
77: {
78: if (pwfile != NULL)
79: rewind(pwfile);
80: }
81:
82: endpwent()
83: {
84: if (pwfile != NULL) {
85: fclose(pwfile);
86: pwfile = NULL;
87: }
88: }
89:
90: /* end of getpwent.c */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.