xref: /csrg-svn/lib/libc/gen/getlogin.c (revision 38789)
137086Skfall /*
237086Skfall  * Copyright (c) 1988 The Regents of the University of California.
337086Skfall  * All rights reserved.
437086Skfall  *
537086Skfall  * Redistribution and use in source and binary forms are permitted
637086Skfall  * provided that the above copyright notice and this paragraph are
737086Skfall  * duplicated in all such forms and that any documentation,
837086Skfall  * advertising materials, and other materials related to such
937086Skfall  * distribution and use acknowledge that the software was developed
1037086Skfall  * by the University of California, Berkeley.  The name of the
1137086Skfall  * University may not be used to endorse or promote products derived
1237086Skfall  * from this software without specific prior written permission.
1337086Skfall  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1437086Skfall  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1537086Skfall  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1637086Skfall  */
1737086Skfall 
1826558Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*38789Skarels static char sccsid[] = "@(#)getlogin.c	5.6 (Berkeley) 08/26/89";
2037086Skfall #endif /* LIBC_SCCS and not lint */
219193Ssam 
2237086Skfall #include <sys/types.h>
2337086Skfall #include <stdio.h>
2437086Skfall #include <pwd.h>
259109Ssam #include <utmp.h>
269109Ssam 
27*38789Skarels int	_logname_valid;		/* known to setlogin() */
289109Ssam 
299109Ssam char *
309109Ssam getlogin()
319109Ssam {
32*38789Skarels 	static char logname[UT_NAMESIZE+1];
339109Ssam 
34*38789Skarels 	if (_logname_valid == 0) {
35*38789Skarels 		if (_getlogin(logname, sizeof(logname) - 1) < 0)
36*38789Skarels 			return ((char *)NULL);
37*38789Skarels 		_logname_valid = 1;
389193Ssam 	}
39*38789Skarels 	return (*logname ? logname : (char *)NULL);
409109Ssam }
4137086Skfall 
4237086Skfall uid_t	geteuid();
4337086Skfall 
4437086Skfall char *
4537086Skfall cuserid(s)
46*38789Skarels 	char *s;
4737086Skfall {
4837086Skfall 	register struct passwd *pwd;
4937086Skfall 
5037460Sbostic 	if ((pwd = getpwuid(geteuid())) == NULL) {
5137460Sbostic 		if (s)
5237086Skfall 			*s = '\0';
53*38789Skarels 		return (s);
5437086Skfall 	}
5537460Sbostic 	if (s) {
5637460Sbostic 		(void)strncpy(s, pwd->pw_name, L_cuserid);
57*38789Skarels 		return (s);
5837086Skfall 	}
59*38789Skarels 	return (pwd->pw_name);
6037086Skfall }
61