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