$NetBSD: patch-ad,v 1.1 2005/03/06 17:50:49 hira Exp $

--- kiklib/src/kik_pty_streams.c.orig	2004-10-23 06:59:39.000000000 +0900
+++ kiklib/src/kik_pty_streams.c	2005-03-05 23:07:02.000000000 +0900
@@ -9,6 +9,7 @@
 /* When _XOPEN_SOURCE is defined,
  * u_int/u_long/... may not be defined without _BSD_SOURCE */
 #define  _BSD_SOURCE 1
+#define  _NETBSD_SOURCE 1
 
 /* for solaris */
 #define __EXTENSIONS__
@@ -34,6 +35,9 @@
 #ifdef  HAVE_SYS_STROPTS_H
 #include  <sys/stropts.h>
 #endif
+#ifdef __NetBSD__
+#include  <util.h>
+#endif
 
 #include  "kik_str.h"		/* strdup */
 #include  "kik_debug.h"
@@ -99,13 +103,30 @@
 		return  -1;
 	}
 
-	fcntl(*master, F_SETFL, O_NDELAY);
-
 	if( ( *slave = open( ttydev, O_RDWR | O_NOCTTY, 0)) < 0)
 	{
 		return -1;
 	}
 
+	if ((*slave_name = strdup(ttydev)) == NULL) {
+		close(*master);
+		close(*slave);
+		return -1;
+	}
+
+	pid = fork();
+	if (pid == -1) {
+		free(*slave_name);
+		return -1;
+	}
+	else if(pid == 0) {
+		close(*master);
+		login_tty(*slave);
+		return 0;
+	}
+
+	fcntl(*master, F_SETFL, O_NDELAY);
+
 	/*
 	 * cygwin doesn't have isastream.
 	 */
@@ -204,78 +225,13 @@
 		}
 	}
 
-	if( ( *slave_name = strdup( ttydev)) == NULL)
-	{
-		close( *master) ;
-		close( *slave) ;
-
-		return  -1 ;
-	}
-
-	pid = fork() ;
-
-	if( pid == -1)
-	{
-		/* fork failed */
-
-		free( *slave_name) ;
-
-		return  -1 ;
-	}
-	else if( pid == 0)
-	{
-		/* child */
-
-		close(*master) ;
-#ifdef HAVE_SETSID
-		setsid() ;
-#else /*HAVE_SETSID*/
-#ifdef TIOCNOTTY
-		fd = open("/dev/tty", O_RDWR | O_NOCTTY);
-		if (fd >= 0)
-		{
-			ioctl(fd, TIOCNOTTY, NULL);
-			close(fd);
-		}
-#endif /*TIOCNOTTY*/
-#endif /*HAVE_SETSID*/
-		fd = open("/dev/tty", O_RDWR | O_NOCTTY);
-		if (fd >= 0)
-		{
-			close(fd);
-		}
-		fd = open(ttydev, O_RDWR);
-		if (fd >= 0)
-		{
-			close(fd);
-		}
-		fd = open("/dev/tty", O_WRONLY);
-		if (fd < 0)
-		{
-			return -1;
-		}
-		close(fd);
-
-		dup2( *slave , 0) ;
-		dup2( *slave , 1) ;
-		dup2( *slave , 2) ;
-
-		if( *slave > STDERR_FILENO)
-		{
-			close(*slave) ;
-		}
-
-		cfsetispeed( &tio , B9600) ;
-		cfsetospeed( &tio , B9600) ;
+	cfsetispeed(&tio, B9600);
+	cfsetospeed(&tio, B9600);
 
-		if( tcsetattr(STDIN_FILENO, TCSANOW , &tio) < 0)
-		{
-		#ifdef  DEBUG
-			kik_warn_printf( KIK_DEBUG_TAG " tcsetattr() failed.\n") ;
-		#endif
-		}
-
-		return  0 ;
+	if(tcsetattr(STDIN_FILENO, TCSANOW , &tio) < 0) {
+	#ifdef  DEBUG
+		kik_warn_printf( KIK_DEBUG_TAG " tcsetattr() failed.\n") ;
+	#endif
 	}
 
 	return  pid ;
