macro and text revision (-mdoc version 3)
[unix-history] / usr / src / lib / libc / gen / getlogin.c
index f59dcb9..16b116b 100644 (file)
@@ -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 <sys/param.h>
+#include <pwd.h>
 #include <utmp.h>
 #include <utmp.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
 
 
-static char UTMP[]     = "/etc/utmp";
-static struct utmp ubuf;
+int    _logname_valid;         /* known to setlogin() */
 
 char *
 getlogin()
 {
 
 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);
 }
 }