$NetBSD: patch-al,v 1.4 2013/03/28 21:17:57 joerg Exp $

--- utmp.c.orig	2001-12-17 22:43:25.000000000 +0000
+++ utmp.c
@@ -7,7 +7,11 @@
 
 #include "mgetty.h"
 
-#if defined(sunos4) || defined(BSD)
+#ifdef __NetBSD__
+#include <utmpx.h>
+#endif
+
+#if defined(sunos4) || ( defined(BSD) && !defined(NETBSD_UTMPX) )
 
 #include <stdio.h>
 #include <string.h>
@@ -20,10 +24,11 @@
 #include <time.h>
 #include <string.h>
 #include <sys/types.h>
-#include <sys/stat.h>
-
-#ifndef ENOENT
-#include <errno.h>
+#if !defined(SVR4) && !defined(NETBSD_UTMPX) && !defined(__GLIBC__) || __GLIBC__ < 2
+# include <sys/stat.h>
+# ifndef ENOENT
+#  include <errno.h>
+# endif
 #endif
 
 #if defined(_3B1_) || defined(MEIBE) || defined(ISC)
@@ -42,8 +47,14 @@ typedef short pid_t;
 # endif
 #endif
 
+#ifndef WTMPX_FILE
+# ifdef _PATH_WTMPX
+#  define WTMPX_FILE _PATH_WTMPX	/* NetBSD */
+# endif
+#endif
+
 
-#if defined(sunos4) || defined(BSD) || defined(ultrix)
+#if ( defined(sunos4) || defined(BSD) || defined(ultrix) ) && !defined(NETBSD_UTMPX)
 /* on SunOS (and other BSD-derived systems), the getty process does *
  * not have to care for the utmp entries, login and init do all the work
  * Anyway, we have to _read_ it to get the number of users logged in.
@@ -109,26 +120,74 @@ void make_utmp_wtmp _P4( (line, ut_type,
 {
 struct utmp *utmp;
 pid_t	pid;
+#ifdef NETBSD_UTMPX
+struct utmpx ut;
+char *id;
+#else
+# if !defined(SVR4) && !defined(__GLIBC__) || __GLIBC__ < 2
 struct stat	st;
+# endif
+#endif
 FILE *	fp;
 
+#ifdef NETBSD_UTMPX
+    id = line;
+    if ( strlen( line ) > sizeof( ut.ut_id ) )
+	id += strlen( line ) - sizeof( ut.ut_id );
+
+    if ( ut_type == UT_INIT ) {
+	lprintf(L_JUNK,
+	    "creating initial utmp entry... (ID: %s)",
+	    id);
+
+	memset( &ut, 0, sizeof( ut ) );
+	strncpy( ut.ut_id, id, sizeof( ut.ut_id ) );
+	strncpy( ut.ut_line, line, sizeof( ut.ut_line ) );
+	gettimeofday( &ut.ut_tv, NULL );
+	ut.ut_type = INIT_PROCESS;
+	login( &ut );
+
+	lprintf(L_NOISE, "initial utmp + wtmp entry made");
+	return;
+    }
+
+    if ((pid = checklock( line )) == NO_LOCK) {
+	pid = getpid();
+    }
+
+    lprintf(L_JUNK,
+	"looking for utmp entry... (ID: %s)", id);
+#else
     pid = getpid();
+
     lprintf(L_JUNK, "looking for utmp entry... (my PID: %d)", pid);
+#endif
 
+    setutent();
     while ((utmp = getutent()) != (struct utmp *) NULL)
     {
-	if (utmp->ut_pid == pid &&
-	    (utmp->ut_type == INIT_PROCESS || utmp->ut_type == LOGIN_PROCESS))
+#ifdef NETBSD_UTMPX
+	if ( strncmp( utmp->ut_id, id, sizeof( utmp->ut_id ) ) == 0 &&
+#else
+	if ( utmp->ut_pid == pid &&
+#endif
+	    (utmp->ut_type == INIT_PROCESS || utmp->ut_type == LOGIN_PROCESS) )
 	{
-	    strcpy(utmp->ut_line, line );
+	    strncpy(utmp->ut_line, line, sizeof( utmp->ut_line) );
 
+#ifdef NETBSD_UTMPX
+	    utmp->ut_pid = pid;
+	    gettimeofday( &utmp->ut_tv, NULL );
+#else
 	    utmp->ut_time = time( NULL );
+#endif
 
 	    utmp->ut_type = ut_type;	/* {INIT,LOGIN,USER}_PROCESS */
-	                                /* "LOGIN", "uugetty", "dialout" */
+
 	    strncpy( utmp->ut_user, ut_user, sizeof( utmp->ut_user ) );
+	                                /* "LOGIN", "uugetty", "dialout" */
 
-#if defined(SVR4) || defined(linux)
+#if defined(SVR4) || defined(linux) || defined(NETBSD_UTMPX)
 	    if (ut_host != NULL)
 	    {
 	    	strncpy( utmp->ut_host, ut_host, sizeof( utmp->ut_host ) - 1);
@@ -138,7 +197,7 @@ FILE *	fp;
 	    }
 #endif		/* SVR4 */
 
-#if defined(M_UNIX) || defined(__GLIBC__)
+#if defined(M_UNIX) || defined(__GLIBC__) || defined(NETBSD_UTMPX)
 	    if ( pututline(utmp) == NULL )
 	    {
 		lprintf( L_ERROR, "cannot create utmp entry" );
@@ -151,7 +210,7 @@ FILE *	fp;
 	    /* write same record to end of wtmp
 	     * if wtmp file exists
 	     */
-#ifdef SVR4
+#if defined(SVR4) || defined(NETBSD_UTMPX)
 	    updwtmpx(WTMPX_FILE, utmp);
 #else
 # if defined(__GLIBC__) && __GLIBC__ >= 2
