xref: /csrg-svn/lib/libc/gen/getpwnamuid.c (revision 15751)
1*15751Sralph /*	getpwnamuid.c	4.2	83/12/20	*/
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;
1315645Sralph static datum curkey;
1415645Sralph static DBM *db = 0;
1515645Sralph 
1615645Sralph static struct passwd *
1715645Sralph fetchpw(key)
1815645Sralph 	datum key;
1915645Sralph {
2015645Sralph         register char *cp;
2115645Sralph 
2215645Sralph         curkey = key;
2315645Sralph         if (curkey.dptr == 0)
2415645Sralph                 return ((struct passwd *)NULL);
2515645Sralph 	key = dbmfetch(db, curkey);
2615645Sralph 	if (key.dptr == 0)
2715645Sralph                 return ((struct passwd *)NULL);
2815645Sralph         cp = key.dptr;
2915645Sralph 
3015645Sralph #define	EXPAND(e)	passwd.pw_/**/e = cp; while (*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 
5015645Sralph         if ((db = ndbmopen(PASSWD, O_RDONLY)) == (DBM *)0)
5115645Sralph                 return ((struct passwd *)NULL);
52*15751Sralph 	if (flock(db->db_dirf, LOCK_SH) < 0)
53*15751Sralph                 return ((struct passwd *)NULL);
5415645Sralph         key.dptr = nam;
5515645Sralph         key.dsize = strlen(nam);
5615645Sralph 	pw = fetchpw(key);
5715645Sralph 	ndbmclose(db);
5815645Sralph         return (pw);
5915645Sralph }
6015645Sralph 
6115645Sralph struct passwd *
6215645Sralph getpwuid(uid)
6315645Sralph 	int uid;
6415645Sralph {
6515645Sralph         datum key;
6615645Sralph 	register struct passwd *pw;
6715645Sralph 
6815645Sralph         if ((db = ndbmopen(PASSWD, O_RDONLY)) == (DBM *)0)
6915645Sralph                 return ((struct passwd *)NULL);
70*15751Sralph 	if (flock(db->db_dirf, LOCK_SH) < 0)
71*15751Sralph                 return ((struct passwd *)NULL);
7215645Sralph         key.dptr = (char *) &uid;
7315645Sralph         key.dsize = sizeof uid;
7415645Sralph 	pw = fetchpw(key);
7515645Sralph 	ndbmclose(db);
7615645Sralph         return (pw);
7715645Sralph }
78