1: /* 2: * Copyright (c) 1983 Regents of the University of California. 3: * All rights reserved. The Berkeley software License Agreement 4: * specifies the terms and conditions for redistribution. 5: */ 6: 7: #if defined(LIBC_SCCS) && !defined(lint) 8: static char sccsid[] = "@(#)getpwnamuid.c 5.2 (Berkeley) 3/9/86"; 9: #endif LIBC_SCCS and not lint 10: 11: #include <stdio.h> 12: #include <pwd.h> 13: #include <ndbm.h> 14: 15: #include <sys/file.h> 16: 17: static char line[BUFSIZ+1]; 18: static struct passwd passwd; 19: 20: /* 21: * The following are shared with getpwent.c 22: */ 23: extern char *_pw_file; 24: DBM *_pw_db; 25: int _pw_stayopen; 26: 27: static struct passwd * 28: fetchpw(key) 29: datum key; 30: { 31: register char *cp, *tp; 32: 33: if (key.dptr == 0) 34: return ((struct passwd *)NULL); 35: key = dbm_fetch(_pw_db, key); 36: if (key.dptr == 0) 37: return ((struct passwd *)NULL); 38: cp = key.dptr; 39: tp = line; 40: 41: #define EXPAND(e) passwd.pw_/**/e = tp; while (*tp++ = *cp++); 42: EXPAND(name); 43: EXPAND(passwd); 44: bcopy(cp, (char *)&passwd.pw_uid, sizeof (int)); 45: cp += sizeof (int); 46: bcopy(cp, (char *)&passwd.pw_gid, sizeof (int)); 47: cp += sizeof (int); 48: bcopy(cp, (char *)&passwd.pw_quota, sizeof (int)); 49: cp += sizeof (int); 50: EXPAND(comment); 51: EXPAND(gecos); 52: EXPAND(dir); 53: EXPAND(shell); 54: return (&passwd); 55: } 56: 57: struct passwd * 58: getpwnam(nam) 59: char *nam; 60: { 61: datum key; 62: register struct passwd *pw; 63: 64: if (_pw_db == (DBM *)0 && 65: (_pw_db = dbm_open(_pw_file, O_RDONLY)) == (DBM *)0) { 66: oldcode: 67: setpwent(); 68: while ((pw = getpwent()) && strcmp(nam, pw->pw_name)) 69: ; 70: if (!_pw_stayopen) 71: endpwent(); 72: return (pw); 73: } 74: if (flock(dbm_dirfno(_pw_db), LOCK_SH) < 0) { 75: dbm_close(_pw_db); 76: _pw_db = (DBM *)0; 77: goto oldcode; 78: } 79: key.dptr = nam; 80: key.dsize = strlen(nam); 81: pw = fetchpw(key); 82: (void) flock(dbm_dirfno(_pw_db), LOCK_UN); 83: if (!_pw_stayopen) { 84: dbm_close(_pw_db); 85: _pw_db = (DBM *)0; 86: } 87: return (pw); 88: } 89: 90: struct passwd * 91: getpwuid(uid) 92: int uid; 93: { 94: datum key; 95: register struct passwd *pw; 96: 97: if (_pw_db == (DBM *)0 && 98: (_pw_db = dbm_open(_pw_file, O_RDONLY)) == (DBM *)0) { 99: oldcode: 100: setpwent(); 101: while ((pw = getpwent()) && pw->pw_uid != uid) 102: ; 103: if (!_pw_stayopen) 104: endpwent(); 105: return (pw); 106: } 107: if (flock(dbm_dirfno(_pw_db), LOCK_SH) < 0) { 108: dbm_close(_pw_db); 109: _pw_db = (DBM *)0; 110: goto oldcode; 111: } 112: key.dptr = (char *) &uid; 113: key.dsize = sizeof uid; 114: pw = fetchpw(key); 115: (void) flock(dbm_dirfno(_pw_db), LOCK_UN); 116: if (!_pw_stayopen) { 117: dbm_close(_pw_db); 118: _pw_db = (DBM *)0; 119: } 120: return (pw); 121: }