Commit | Line | Data |
---|---|---|
18a41d70 | 1 | /* getpwnamuid.c 4.2 83/12/20 */ |
8b87132c RC |
2 | |
3 | #include <stdio.h> | |
4 | #include <pwd.h> | |
5 | #include <ndbm.h> | |
6 | ||
7 | #include <sys/file.h> | |
8 | ||
9 | static char PASSWD[] = "/etc/passwd"; | |
10 | static char EMPTY[] = ""; | |
11 | static char line[BUFSIZ+1]; | |
12 | static struct passwd passwd; | |
13 | static datum curkey; | |
14 | static DBM *db = 0; | |
15 | ||
16 | static struct passwd * | |
17 | fetchpw(key) | |
18 | datum key; | |
19 | { | |
20 | register char *cp; | |
21 | ||
22 | curkey = key; | |
23 | if (curkey.dptr == 0) | |
24 | return ((struct passwd *)NULL); | |
25 | key = dbmfetch(db, curkey); | |
26 | if (key.dptr == 0) | |
27 | return ((struct passwd *)NULL); | |
28 | cp = key.dptr; | |
29 | ||
30 | #define EXPAND(e) passwd.pw_/**/e = cp; while (*cp++); | |
31 | EXPAND(name); | |
32 | EXPAND(passwd); | |
33 | passwd.pw_uid = *(int *)cp; cp += sizeof (int); | |
34 | passwd.pw_gid = *(int *)cp; cp += sizeof (int); | |
35 | passwd.pw_quota = *(int *)cp; cp += sizeof (int); | |
36 | EXPAND(comment); | |
37 | EXPAND(gecos); | |
38 | EXPAND(dir); | |
39 | EXPAND(shell); | |
40 | return (&passwd); | |
41 | } | |
42 | ||
43 | struct passwd * | |
44 | getpwnam(nam) | |
45 | char *nam; | |
46 | { | |
47 | datum key; | |
48 | register struct passwd *pw; | |
49 | ||
50 | if ((db = ndbmopen(PASSWD, O_RDONLY)) == (DBM *)0) | |
51 | return ((struct passwd *)NULL); | |
18a41d70 RC |
52 | if (flock(db->db_dirf, LOCK_SH) < 0) |
53 | return ((struct passwd *)NULL); | |
8b87132c RC |
54 | key.dptr = nam; |
55 | key.dsize = strlen(nam); | |
56 | pw = fetchpw(key); | |
57 | ndbmclose(db); | |
58 | return (pw); | |
59 | } | |
60 | ||
61 | struct passwd * | |
62 | getpwuid(uid) | |
63 | int uid; | |
64 | { | |
65 | datum key; | |
66 | register struct passwd *pw; | |
67 | ||
68 | if ((db = ndbmopen(PASSWD, O_RDONLY)) == (DBM *)0) | |
69 | return ((struct passwd *)NULL); | |
18a41d70 RC |
70 | if (flock(db->db_dirf, LOCK_SH) < 0) |
71 | return ((struct passwd *)NULL); | |
8b87132c RC |
72 | key.dptr = (char *) &uid; |
73 | key.dsize = sizeof uid; | |
74 | pw = fetchpw(key); | |
75 | ndbmclose(db); | |
76 | return (pw); | |
77 | } |