xref: /original-bsd/lib/libc/gen/getlogin.c (revision fac09079)
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.7 (Berkeley) 06/01/90";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/types.h>
13 #include <stdio.h>
14 #include <pwd.h>
15 #include <utmp.h>
16 
17 int	_logname_valid;		/* known to setlogin() */
18 
19 char *
20 getlogin()
21 {
22 	static char logname[UT_NAMESIZE+1];
23 
24 	if (_logname_valid == 0) {
25 		if (_getlogin(logname, sizeof(logname) - 1) < 0)
26 			return ((char *)NULL);
27 		_logname_valid = 1;
28 	}
29 	return (*logname ? logname : (char *)NULL);
30 }
31 
32 uid_t	geteuid();
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