xref: /csrg-svn/lib/libc/gen/getpwnamuid.c (revision 33066)
121342Sdist /*
221342Sdist  * Copyright (c) 1983 Regents of the University of California.
321342Sdist  * All rights reserved.  The Berkeley software License Agreement
421342Sdist  * specifies the terms and conditions for redistribution.
521342Sdist  */
615645Sralph 
726561Sdonn #if defined(LIBC_SCCS) && !defined(lint)
8*33066Sbostic static char sccsid[] = "@(#)getpwnamuid.c	5.3 (Berkeley) 12/21/87";
926561Sdonn #endif LIBC_SCCS and not lint
1021342Sdist 
1115645Sralph #include <stdio.h>
1215645Sralph #include <pwd.h>
1315645Sralph #include <ndbm.h>
1415645Sralph 
1515645Sralph #include <sys/file.h>
1615645Sralph 
1715645Sralph static char line[BUFSIZ+1];
1815645Sralph static struct passwd passwd;
1915645Sralph 
2017921Sserge /*
2117921Sserge  * The following are shared with getpwent.c
2217921Sserge  */
2317921Sserge extern	char *_pw_file;
2417921Sserge DBM	*_pw_db;
2517921Sserge int	_pw_stayopen;
2617921Sserge 
2715645Sralph static struct passwd *
fetchpw(key)2815645Sralph fetchpw(key)
2915645Sralph 	datum key;
3015645Sralph {
3115911Sralph         register char *cp, *tp;
3215645Sralph 
3315911Sralph         if (key.dptr == 0)
3415645Sralph                 return ((struct passwd *)NULL);
3517030Sralph 	key = dbm_fetch(_pw_db, key);
3615645Sralph 	if (key.dptr == 0)
3715645Sralph                 return ((struct passwd *)NULL);
3815645Sralph         cp = key.dptr;
3915911Sralph 	tp = line;
4015645Sralph 
41*33066Sbostic #define	EXPAND(e)	passwd.e = tp; while (*tp++ = *cp++);
42*33066Sbostic 	EXPAND(pw_name);
43*33066Sbostic 	EXPAND(pw_passwd);
4417689Sralph 	bcopy(cp, (char *)&passwd.pw_uid, sizeof (int));
4517689Sralph 	cp += sizeof (int);
4617689Sralph 	bcopy(cp, (char *)&passwd.pw_gid, sizeof (int));
4717689Sralph 	cp += sizeof (int);
4817689Sralph 	bcopy(cp, (char *)&passwd.pw_quota, sizeof (int));
4917689Sralph 	cp += sizeof (int);
50*33066Sbostic 	EXPAND(pw_comment);
51*33066Sbostic 	EXPAND(pw_gecos);
52*33066Sbostic 	EXPAND(pw_dir);
53*33066Sbostic 	EXPAND(pw_shell);
5415645Sralph         return (&passwd);
5515645Sralph }
5615645Sralph 
5715645Sralph struct passwd *
getpwnam(nam)5815645Sralph getpwnam(nam)
5915645Sralph 	char *nam;
6015645Sralph {
6115645Sralph         datum key;
6215645Sralph 	register struct passwd *pw;
6315645Sralph 
6416416Sralph         if (_pw_db == (DBM *)0 &&
6517921Sserge 	    (_pw_db = dbm_open(_pw_file, O_RDONLY)) == (DBM *)0) {
6616416Sralph 	oldcode:
6716416Sralph 		setpwent();
6817921Sserge 		while ((pw = getpwent()) && strcmp(nam, pw->pw_name))
6917921Sserge 			;
7017921Sserge 		if (!_pw_stayopen)
7117921Sserge 			endpwent();
7216416Sralph 		return (pw);
7316416Sralph 	}
7417921Sserge 	if (flock(dbm_dirfno(_pw_db), LOCK_SH) < 0) {
7517030Sralph 		dbm_close(_pw_db);
7616416Sralph 		_pw_db = (DBM *)0;
7716416Sralph 		goto oldcode;
7816416Sralph 	}
7915645Sralph         key.dptr = nam;
8015645Sralph         key.dsize = strlen(nam);
8115645Sralph 	pw = fetchpw(key);
8217921Sserge 	(void) flock(dbm_dirfno(_pw_db), LOCK_UN);
8316416Sralph 	if (!_pw_stayopen) {
8417030Sralph 		dbm_close(_pw_db);
8516416Sralph 		_pw_db = (DBM *)0;
8616416Sralph 	}
8715645Sralph         return (pw);
8815645Sralph }
8915645Sralph 
9015645Sralph struct passwd *
getpwuid(uid)9115645Sralph getpwuid(uid)
9215645Sralph 	int uid;
9315645Sralph {
9415645Sralph         datum key;
9515645Sralph 	register struct passwd *pw;
9615645Sralph 
9716416Sralph         if (_pw_db == (DBM *)0 &&
9817921Sserge 	    (_pw_db = dbm_open(_pw_file, O_RDONLY)) == (DBM *)0) {
9916416Sralph 	oldcode:
10016416Sralph 		setpwent();
10117921Sserge 		while ((pw = getpwent()) && pw->pw_uid != uid)
10217921Sserge 			;
10317921Sserge 		if (!_pw_stayopen)
10417921Sserge 			endpwent();
10516416Sralph 		return (pw);
10616416Sralph 	}
10717921Sserge 	if (flock(dbm_dirfno(_pw_db), LOCK_SH) < 0) {
10817030Sralph 		dbm_close(_pw_db);
10916416Sralph 		_pw_db = (DBM *)0;
11016416Sralph 		goto oldcode;
11116416Sralph 	}
11215645Sralph         key.dptr = (char *) &uid;
11315645Sralph         key.dsize = sizeof uid;
11415645Sralph 	pw = fetchpw(key);
11517921Sserge 	(void) flock(dbm_dirfno(_pw_db), LOCK_UN);
11616416Sralph 	if (!_pw_stayopen) {
11717030Sralph 		dbm_close(_pw_db);
11816416Sralph 		_pw_db = (DBM *)0;
11916416Sralph 	}
12015645Sralph         return (pw);
12115645Sralph }
122