xref: /csrg-svn/lib/libc/gen/getpwnamuid.c (revision 16416)
1*16416Sralph /*	getpwnamuid.c	4.4	84/04/26	*/
215645Sralph 
315645Sralph #include <stdio.h>
415645Sralph #include <pwd.h>
515645Sralph #include <ndbm.h>
615645Sralph 
715645Sralph #include <sys/file.h>
815645Sralph 
915645Sralph static char PASSWD[] = "/etc/passwd";
1015645Sralph static char EMPTY[] = "";
1115645Sralph static char line[BUFSIZ+1];
1215645Sralph static struct passwd passwd;
13*16416Sralph DBM *_pw_db = 0;
14*16416Sralph int _pw_stayopen = 0;
1515645Sralph 
1615645Sralph static struct passwd *
1715645Sralph fetchpw(key)
1815645Sralph 	datum key;
1915645Sralph {
2015911Sralph         register char *cp, *tp;
2115645Sralph 
2215911Sralph         if (key.dptr == 0)
2315645Sralph                 return ((struct passwd *)NULL);
24*16416Sralph 	key = dbmfetch(_pw_db, key);
2515645Sralph 	if (key.dptr == 0)
2615645Sralph                 return ((struct passwd *)NULL);
2715645Sralph         cp = key.dptr;
2815911Sralph 	tp = line;
2915645Sralph 
3015911Sralph #define	EXPAND(e)	passwd.pw_/**/e = tp; while (*tp++ = *cp++);
3115645Sralph 	EXPAND(name);
3215645Sralph 	EXPAND(passwd);
3315645Sralph 	passwd.pw_uid = *(int *)cp; cp += sizeof (int);
3415645Sralph 	passwd.pw_gid = *(int *)cp; cp += sizeof (int);
3515645Sralph 	passwd.pw_quota = *(int *)cp; cp += sizeof (int);
3615645Sralph 	EXPAND(comment);
3715645Sralph 	EXPAND(gecos);
3815645Sralph 	EXPAND(dir);
3915645Sralph 	EXPAND(shell);
4015645Sralph         return (&passwd);
4115645Sralph }
4215645Sralph 
4315645Sralph struct passwd *
4415645Sralph getpwnam(nam)
4515645Sralph 	char *nam;
4615645Sralph {
4715645Sralph         datum key;
4815645Sralph 	register struct passwd *pw;
4915645Sralph 
50*16416Sralph         if (_pw_db == (DBM *)0 &&
51*16416Sralph 	    (_pw_db = ndbmopen(PASSWD, O_RDONLY)) == (DBM *)0) {
52*16416Sralph 	oldcode:
53*16416Sralph 		setpwent();
54*16416Sralph 		while ((pw = getpwent()) && strcmp(nam, pw->pw_name));
55*16416Sralph 		endpwent();
56*16416Sralph 		return (pw);
57*16416Sralph 	}
58*16416Sralph 	if (flock(_pw_db->db_dirf, LOCK_SH) < 0) {
59*16416Sralph 		ndbmclose(_pw_db);
60*16416Sralph 		_pw_db = (DBM *)0;
61*16416Sralph 		goto oldcode;
62*16416Sralph 	}
6315645Sralph         key.dptr = nam;
6415645Sralph         key.dsize = strlen(nam);
6515645Sralph 	pw = fetchpw(key);
66*16416Sralph 	(void) flock(_pw_db->db_dirf, LOCK_UN);
67*16416Sralph 	if (!_pw_stayopen) {
68*16416Sralph 		ndbmclose(_pw_db);
69*16416Sralph 		_pw_db = (DBM *)0;
70*16416Sralph 	}
7115645Sralph         return (pw);
7215645Sralph }
7315645Sralph 
7415645Sralph struct passwd *
7515645Sralph getpwuid(uid)
7615645Sralph 	int uid;
7715645Sralph {
7815645Sralph         datum key;
7915645Sralph 	register struct passwd *pw;
8015645Sralph 
81*16416Sralph         if (_pw_db == (DBM *)0 &&
82*16416Sralph 	    (_pw_db = ndbmopen(PASSWD, O_RDONLY)) == (DBM *)0) {
83*16416Sralph 	oldcode:
84*16416Sralph 		setpwent();
85*16416Sralph 		while ((pw = getpwent()) && pw->pw_uid != uid);
86*16416Sralph 		endpwent();
87*16416Sralph 		return (pw);
88*16416Sralph 	}
89*16416Sralph 	if (flock(_pw_db->db_dirf, LOCK_SH) < 0) {
90*16416Sralph 		ndbmclose(_pw_db);
91*16416Sralph 		_pw_db = (DBM *)0;
92*16416Sralph 		goto oldcode;
93*16416Sralph 	}
9415645Sralph         key.dptr = (char *) &uid;
9515645Sralph         key.dsize = sizeof uid;
9615645Sralph 	pw = fetchpw(key);
97*16416Sralph 	(void) flock(_pw_db->db_dirf, LOCK_UN);
98*16416Sralph 	if (!_pw_stayopen) {
99*16416Sralph 		ndbmclose(_pw_db);
100*16416Sralph 		_pw_db = (DBM *)0;
101*16416Sralph 	}
10215645Sralph         return (pw);
10315645Sralph }
104