X-Git-Url: https://git.subgeniuskitty.com/unix-history/.git/blobdiff_plain/fb7ebc6bf1fdb07ac679ba597ff8276eabe12773..ae59e04cb5f746d72d5e3e8c84dad7862c9b50e7:/usr/src/lib/libc/gen/getlogin.c diff --git a/usr/src/lib/libc/gen/getlogin.c b/usr/src/lib/libc/gen/getlogin.c index f59dcb98e0..16b116b622 100644 --- a/usr/src/lib/libc/gen/getlogin.c +++ b/usr/src/lib/libc/gen/getlogin.c @@ -1,29 +1,50 @@ -/* getlogin.c 4.3 84/04/27 */ +/* + * Copyright (c) 1988 The Regents of the University of California. + * All rights reserved. + * + * %sccs.include.redist.c% + */ +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)getlogin.c 5.9 (Berkeley) %G%"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include #include +#include +#include +#include -static char UTMP[] = "/etc/utmp"; -static struct utmp ubuf; +int _logname_valid; /* known to setlogin() */ char * getlogin() { - register int me, uf; - register char *cp; + static char logname[MAXLOGNAME + 1]; + + if (_logname_valid == 0) { + if (_getlogin(logname, sizeof(logname) - 1) < 0) + return ((char *)NULL); + _logname_valid = 1; + } + return (*logname ? logname : (char *)NULL); +} + +char * +cuserid(s) + char *s; +{ + register struct passwd *pwd; - if (!(me = ttyslot())) - return(0); - if ((uf = open(UTMP, 0)) < 0) - return (0); - lseek (uf, (long)(me*sizeof(ubuf)), 0); - if (read(uf, (char *)&ubuf, sizeof (ubuf)) != sizeof (ubuf)) { - close(uf); - return (0); + if ((pwd = getpwuid(geteuid())) == NULL) { + if (s) + *s = '\0'; + return (s); + } + if (s) { + (void)strncpy(s, pwd->pw_name, L_cuserid); + return (s); } - close(uf); - ubuf.ut_name[sizeof (ubuf.ut_name)] = ' '; - for (cp = ubuf.ut_name; *cp++ != ' '; ) - ; - *--cp = '\0'; - return (ubuf.ut_name); + return (pwd->pw_name); }