xref: /csrg-svn/lib/libc/gen/getlogin.c (revision 37460)
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*37460Sbostic static char sccsid[] = "@(#)getlogin.c	5.5 (Berkeley) 04/20/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 
2737086Skfall static	char	logname[UT_NAMESIZE+1];
289109Ssam 
299109Ssam char *
309109Ssam getlogin()
319109Ssam {
3237086Skfall 	static int notcalled = 1;
339109Ssam 
3437086Skfall 	if (notcalled) {
35*37460Sbostic 		if (getlogname(logname, sizeof(logname) - 1) < 0)
36*37460Sbostic 			return((char *)NULL);
3737086Skfall 		notcalled = 0;
389193Ssam 	}
39*37460Sbostic 	return(*logname ? logname : (char *)NULL);
409109Ssam }
4137086Skfall 
4237086Skfall uid_t	geteuid();
4337086Skfall 
4437086Skfall char *
4537086Skfall cuserid(s)
4637086Skfall 	char	*s;
4737086Skfall {
4837086Skfall 	register struct passwd *pwd;
4937086Skfall 
50*37460Sbostic 	if ((pwd = getpwuid(geteuid())) == NULL) {
51*37460Sbostic 		if (s)
5237086Skfall 			*s = '\0';
5337086Skfall 		return(NULL);
5437086Skfall 	}
55*37460Sbostic 	if (s) {
56*37460Sbostic 		(void)strncpy(s, pwd->pw_name, L_cuserid);
5737086Skfall 		return(s);
5837086Skfall 	}
5937086Skfall 	return(pwd->pw_name);
6037086Skfall }
61