1 /* 2 * Copyright (c) 1988 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #if defined(LIBC_SCCS) && !defined(lint) 9 static char sccsid[] = "@(#)getlogin.c 5.9 (Berkeley) 02/23/91"; 10 #endif /* LIBC_SCCS and not lint */ 11 12 #include <sys/param.h> 13 #include <pwd.h> 14 #include <utmp.h> 15 #include <stdio.h> 16 #include <string.h> 17 #include <unistd.h> 18 19 int _logname_valid; /* known to setlogin() */ 20 21 char * 22 getlogin() 23 { 24 static char logname[MAXLOGNAME + 1]; 25 26 if (_logname_valid == 0) { 27 if (_getlogin(logname, sizeof(logname) - 1) < 0) 28 return ((char *)NULL); 29 _logname_valid = 1; 30 } 31 return (*logname ? logname : (char *)NULL); 32 } 33 34 char * 35 cuserid(s) 36 char *s; 37 { 38 register struct passwd *pwd; 39 40 if ((pwd = getpwuid(geteuid())) == NULL) { 41 if (s) 42 *s = '\0'; 43 return (s); 44 } 45 if (s) { 46 (void)strncpy(s, pwd->pw_name, L_cuserid); 47 return (s); 48 } 49 return (pwd->pw_name); 50 } 51