$NetBSD: patch-al,v 1.9 2023/09/06 16:00:15 vins Exp $

Make extra calls to remove utmp entries when utmpx is used.
For non-login entries we only update utmpx, as it stores
more information, and utilities are reading both.

Do not truncate hostnames that have two colons (:) as they
are likely IPv6 addresses. Wrap them in brackets ([])
unless already wrapped (e.g. from $DISPLAY).

--- utmp.c.orig	2023-08-16 00:29:26.000000000 +0000
+++ utmp.c
@@ -48,6 +48,12 @@ extern char *LoginName;
 extern int real_uid, eff_uid;
 
 
+#ifdef NetBSD_UTMP
+extern void utmp_login __P((char *));
+extern void utmp_logout __P((char *));
+#endif
+
+
 /*
  *  UTNOKEEP: A (ugly) hack for apollo that does two things:
  *    1) Always close and reopen the utmp file descriptor. (I don't know
@@ -315,6 +321,9 @@ RemoveLoginSlot()
 
   ASSERT(display);
   debug("RemoveLoginSlot: removing your logintty\n");
+#ifdef NetBSD_UTMP
+  utmp_logout(stripdev(D_usertty));
+#endif
   D_loginslot = TtyNameSlot(D_usertty);
   if (D_loginslot == (slot_t)0 || D_loginslot == (slot_t)-1)
     return;
@@ -379,6 +388,9 @@ RestoreLoginSlot()
 
   debug("RestoreLoginSlot()\n");
   ASSERT(display);
+#ifdef NetBSD_UTMP
+  utmp_login(stripdev(D_usertty));
+#endif
   if (utmpok && D_loginslot != (slot_t)0 && D_loginslot != (slot_t)-1)
     {
       debug1(" logging you in again (slot %#lx)\n", (long)D_loginslot);
@@ -410,7 +422,7 @@ struct win *wi;
   struct utmp u;
   int saved_ut;
 #ifdef UTHOST
-  char *p;
+  char *m, *p;
   char host[sizeof(D_loginhost) + 15];
 #else
   char *host = 0;
@@ -449,12 +461,37 @@ struct win *wi;
 	   * "faui45.informati"......:s.0
 	   * HPUX uses host:0.0, so chop at "." and ":" (Eric Backus)
 	   */
-	  for (p = host; *p; p++)
-	    if ((*p < '0' || *p > '9') && (*p != '.'))
+	  for (m = p = host; *p; p++)
+	    if (*p == ':')
 	      break;
+	  if (*p == ':')
+	    for (m = p + 1; *m; m++)
+	      if (*m == ':')
+		{
+		  if (host[0] != '[')
+		    {
+		      strncpy(host + 1, D_loginhost, sizeof(host) - 15 - 2);
+		      host[0] = '[';
+		      p = host + strlen(host);
+		      *p++ = ']';
+		    }
+		  break;
+		}
+	  else
+	    for (p = host; *p; p++)
+	      if ((*p < '0' || *p > '9') && (*p != '.'))
+		break;
 	  if (*p)
 	    {
-	      for (p = host; *p; p++)
+	      p = host;
+	      if (host[0] == '[')
+		for (; *p; p++)
+		  if (*p == ']')
+		    {
+		      p++;
+		      break;
+		    }
+	      for (; *p; p++)
 		if (*p == '.' || (*p == ':' && p != host))
 		  {
 		    *p = '\0';
