xref: /csrg-svn/lib/libc/gen/getlogin.c (revision 42624)
137086Skfall /*
237086Skfall  * Copyright (c) 1988 The Regents of the University of California.
337086Skfall  * All rights reserved.
437086Skfall  *
5*42624Sbostic  * %sccs.include.redist.c%
637086Skfall  */
737086Skfall 
826558Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*42624Sbostic static char sccsid[] = "@(#)getlogin.c	5.7 (Berkeley) 06/01/90";
1037086Skfall #endif /* LIBC_SCCS and not lint */
119193Ssam 
1237086Skfall #include <sys/types.h>
1337086Skfall #include <stdio.h>
1437086Skfall #include <pwd.h>
159109Ssam #include <utmp.h>
169109Ssam 
1738789Skarels int	_logname_valid;		/* known to setlogin() */
189109Ssam 
199109Ssam char *
209109Ssam getlogin()
219109Ssam {
2238789Skarels 	static char logname[UT_NAMESIZE+1];
239109Ssam 
2438789Skarels 	if (_logname_valid == 0) {
2538789Skarels 		if (_getlogin(logname, sizeof(logname) - 1) < 0)
2638789Skarels 			return ((char *)NULL);
2738789Skarels 		_logname_valid = 1;
289193Ssam 	}
2938789Skarels 	return (*logname ? logname : (char *)NULL);
309109Ssam }
3137086Skfall 
3237086Skfall uid_t	geteuid();
3337086Skfall 
3437086Skfall char *
3537086Skfall cuserid(s)
3638789Skarels 	char *s;
3737086Skfall {
3837086Skfall 	register struct passwd *pwd;
3937086Skfall 
4037460Sbostic 	if ((pwd = getpwuid(geteuid())) == NULL) {
4137460Sbostic 		if (s)
4237086Skfall 			*s = '\0';
4338789Skarels 		return (s);
4437086Skfall 	}
4537460Sbostic 	if (s) {
4637460Sbostic 		(void)strncpy(s, pwd->pw_name, L_cuserid);
4738789Skarels 		return (s);
4837086Skfall 	}
4938789Skarels 	return (pwd->pw_name);
5037086Skfall }
51