$OpenBSD: patch-Wnn_uum_jhlp_c,v 1.7 2017/04/29 14:31:40 espie Exp $
--- Wnn/uum/jhlp.c.orig
+++ Wnn/uum/jhlp.c
@@ -49,7 +49,11 @@ static char *rcs_id = "$Id: jhlp.c,v 1.40 1994/08/19 0
 /*	Version 4.0
  */
 #include <stdio.h>
+#include <stdlib.h>
 #include <signal.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
 #include "jllib.h"
 #include "commonhd.h"
 #include "sdefine.h"
@@ -61,7 +65,7 @@ static char *rcs_id = "$Id: jhlp.c,v 1.40 1994/08/19 0
 #ifndef SYSVR2
 #include <fcntl.h>
 #endif /* !SYSVR2 */
-#include <sys/errno.h>
+#include <errno.h>
 #include <pwd.h>
 #include <sys/time.h>
 #ifdef UX386
@@ -80,6 +84,9 @@ struct passwd *getpwuid();
 
 jmp_buf kk_env;
 
+#if (defined(__unix__) || defined(unix)) && !defined(USG)
+#include <sys/param.h>
+#endif
 #ifdef SYSVR2
 #	include <sys/param.h>
 #endif /* SYSVR2 */
@@ -88,9 +95,21 @@ jmp_buf kk_env;
 #	include <sys/wait.h>
 #endif /* HAVE_WAIT3 */
 
+#if defined(__OpenBSD__)
+#define USE_TERMIOS
+#else
 #ifdef BSD42
+#define USE_SGTTY
+#endif
+#endif
+
+#ifdef linux
+#define USE_TERMIOS
+#endif
+
+#ifdef USE_SGTTY
 #       include <sgtty.h>
-#endif /* BSD42 */
+#endif
 
 #ifdef SYSVR2
 #ifdef TERMINFO
@@ -101,7 +120,7 @@ jmp_buf kk_env;
 #if defined(SVR4) || defined(hpux) || defined(sgi)
 #include <sys/termio.h>
 #endif
-#ifdef linux
+#ifdef USE_TERMIOS
 #include <termios.h>
 #endif
 
@@ -133,11 +152,10 @@ char *cmdnm = "csh";		/* char *cmdnm = "csh"; */
 int child_id;
 char *prog;
 
-extern int errno;
 extern char *optarg;
 extern int optind;
 
-extern char *ttyname(), *malloc(), *getenv();
+extern char *ttyname();
 
 static void save_signals();
 static void restore_signals();
@@ -148,7 +166,7 @@ static void do_end(), open_pty(), open_ttyp(), do_main
   change_size(), default_usage();
 
 /** ᥤ */
-void
+int
 main(argc, argv)
 int argc;
 char **argv;
@@ -166,14 +184,14 @@ char **argv;
     flow_control = FLOW_CONTROL;
     code_trans = default_code_trans;
 
-    strcpy(username, getpwuid(getuid())->pw_name);
+    strlcpy(username, getpwuid(getuid())->pw_name, PATHNAMELEN);
     if((name = getenv(WNN_USERNAME_ENV)) != NULL){
-	strcpy(username, name);
+	strlcpy(username, name, PATHNAMELEN);
     }
     for (i = 1; i < argc;) {
 	if (!strcmp(argv[i++], "-L")) {
 	    if (i >= argc || argv[i][0] == '-') default_usage();
-	    strcpy(lang_dir, argv[i++]);
+	    strlcpy(lang_dir, argv[i++], 32);
 	    for (;i < argc; i++) {
 		argv[i - 2] = argv[i];
 	    }
@@ -185,12 +203,7 @@ char **argv;
 
     if(*lang_dir == '\0') {
 	if ((p = getenv("LANG")) != NULL) {
-	    if (strlen(p) >= 4) {
-		strncpy(lang_dir, p, 5);
-		lang_dir[5] = '\0';
-	    } else {
-		strcpy(lang_dir, p);
-	    }
+	    strlcpy(lang_dir, p, 5);
 	}
     }
     for (f = function_db; *lang_dir && f && f->lang; f++) {
@@ -233,8 +246,8 @@ char **argv;
 	    server_env = WNN_DEF_SERVER_ENV;
 	}
 	if(name = getenv(server_env)) {
-	    strcpy(def_servername, name);
-	    strcpy(def_reverse_servername, name);
+	    strlcpy(def_servername, name, PATHNAMELEN);
+	    strlcpy(def_reverse_servername, name, PATHNAMELEN);
 	}
     }
 
@@ -244,7 +257,7 @@ char **argv;
     }
 
     if((p = getenv(WNN_COUNTDOWN_ENV)) == NULL) {
-      setenv(WNN_COUNTDOWN_ENV,"0");
+      setenv(WNN_COUNTDOWN_ENV,"0", 1);
     } else if (atoi(p) <= 0) {
 	puteustring(MSG_GET(4), stdout);
 	/*
@@ -253,7 +266,7 @@ char **argv;
 	exit(126);
     } else {
       sprintf(p,"%d",atoi(p) - 1);
-      setenv(WNN_COUNTDOWN_ENV,p);
+      setenv(WNN_COUNTDOWN_ENV,p, 1);
     }
 
     if((tname = getenv("TERM")) == NULL) {
@@ -263,9 +276,11 @@ char **argv;
 
 
 #if defined(BSD42) && !defined(DGUX)
+#if !(defined(BSD) && (BSD >= 199306))
     if (saveutmp() < 0) {
 	puts("Can't save utmp\n");
     }
+#endif
 #endif /* BSD42 */
 
 
@@ -312,7 +327,7 @@ char **argv;
 #endif /* TERMCAP */
 
     open_pty();
-#ifndef linux
+#if !(defined(linux) || defined(__OpenBSD__))
     open_ttyp();
 #endif
     exec_cmd(argv);
@@ -492,7 +507,7 @@ static int do_X_opt()
 
 static int do_k_opt()
 {
-    strcpy(uumkey_name_in_uumrc, optarg);
+    strlcpy(uumkey_name_in_uumrc, optarg, PATHNAMELEN);
     if (*uumkey_name_in_uumrc == '\0') {
 	return -1;
     }
@@ -502,7 +517,7 @@ static int do_k_opt()
 
 static int do_c_opt()
 {
-    strcpy(convkey_name_in_uumrc, optarg);
+    strlcpy(convkey_name_in_uumrc, optarg, PATHNAMELEN);
     if (*convkey_name_in_uumrc == '\0') {
 	return -1;
     }
@@ -512,7 +527,7 @@ static int do_c_opt()
 
 static int do_r_opt()
 {
-    strcpy(rkfile_name_in_uumrc, optarg);
+    strlcpy(rkfile_name_in_uumrc, optarg, PATHNAMELEN);
     if (*rkfile_name_in_uumrc == '\0') {
 	return -1;
     }
@@ -528,8 +543,8 @@ static int do_l_opt()
 
 static int do_D_opt()
 {
-    strcpy(def_servername, optarg);
-    strcpy(def_reverse_servername, optarg);
+    strlcpy(def_servername, optarg, PATHNAMELEN);
+    strlcpy(def_reverse_servername, optarg, PATHNAMELEN);
     if (*def_servername == '\0') {
 	return -1;
     }
@@ -538,7 +553,7 @@ static int do_D_opt()
 
 static int do_n_opt()
 {
-    strcpy(username, optarg);
+    strlcpy(username, optarg, PATHNAMELEN);
     if (*username == '\0') {
 	return -1;
     }
@@ -581,14 +596,14 @@ char **argv;
 #endif	/* SYSVR2 */
   extern char *index();
   
-  strcpy(ostr, default_getoptstr);
-  strcat(ostr, lang_db->getoptstr);
+  strlcpy(ostr, default_getoptstr, sizeof(ostr));
+  strlcat(ostr, lang_db->getoptstr, sizeof(ostr));
   while ((c = getopt(argc, argv, ostr)) != EOF) {
     if (!(p = index(default_ostr, c)) || (*do_opt[p - default_ostr])() < 0) {
 	if (!(p = index(lang_db->ostr, c)) ||
 	    (*lang_db->do_opt[p - lang_db->ostr])() < 0) {
-	    strcpy(ostr, default_ostr);
-	    strcat(ostr, lang_db->ostr);
+	    strlcpy(ostr, default_ostr, sizeof(ostr));
+	    strlcat(ostr, lang_db->ostr, sizeof(ostr));
 	    usage(ostr);
 	}
     }
@@ -600,7 +615,7 @@ char **argv;
 
 /** tty Ф ioctl Υå */
 
-#ifdef BSD42
+#ifdef USE_SGTTY
 struct sgttyb savetmio;
 /*
   struct sgttyb ttyb =
@@ -642,11 +657,11 @@ j_term_init()
 }
 #endif /* BSD42 */
 
-#ifdef SYSVR2
+#if defined(SYSVR2) || defined(USE_TERMIOS)
 #define UNDEF_STTY 0xff 
 
-struct termio savetmio;
-struct termio terms = {
+struct termios savetmio;
+struct termios terms = {
     IGNBRK | ICRNL | IXON,	/* c_iflag */
     ONLCR | OPOST,		/* c_oflag */
     B9600 | CS8 | CSTOPB | CREAD | CLOCAL, /* c_cflag */
@@ -678,13 +693,13 @@ get_rubout()
 int
 j_term_init()
 {
-    struct termio buf1;
+    struct termios buf1;
 #if defined(uniosu)
     struct jtermio buf2;
 #endif	/* defined(uniosu) */
 
     buf1 = savetmio;
-#ifdef linux
+#ifdef USE_TERMIOS
     buf1.c_lflag &= ~(ECHONL | ECHOK | ECHOE | ECHO | XCASE | ICANON | ISIG);
     buf1.c_iflag = 0;
     buf1.c_oflag &= ~OPOST;
@@ -705,7 +720,7 @@ j_term_init()
     buf1.c_cc[VEOF] = 1;	/* cf. ICANON */
     buf1.c_cc[VEOL] = 0;
 #endif /* linux */
-    if(ioctl(ttyfd, TCSETA, &buf1) == -1){
+    if (tcsetattr(ttyfd, TCSANOW, &buf1) == -1) {
 	fprintf(stderr, "error in ioctl TCSETA.\n");
 	exit(1);
     }
@@ -764,19 +779,20 @@ intfntype
 chld_handler()
 {
 #ifdef HAVE_WAIT3
-#ifdef linux
     int status;
-#else
-    union wait status;
-#endif
     int pid;
 
-    if ((pid = wait3(&status, WNOHANG | WUNTRACED, NULL)) == child_id) {
+/*
+ * Remove warning.
+ * Patched by Hidekazu Kuroki(hidekazu@cs.titech.ac.jp)		1996/8/20
+ */
+/*  if ((pid = wait3(&status, WNOHANG | WUNTRACED, NULL)) == child_id) { */
+    if ((pid = wait3((int *)&status, WNOHANG | WUNTRACED, NULL)) == child_id) {
 	if (WIFSTOPPED(status)) {
 #ifdef SIGCONT
 	    kill(pid, SIGCONT);
 #ifndef SYSVR2
-	    pid = getpgrp(pid);
+	    pid = getpgrp();
 	    killpg(pid, SIGCONT);
 #else
 #if defined(uniosu)
@@ -1107,9 +1123,9 @@ char **argv;
 #ifdef BSD42
     int pid;
 #endif
-#ifdef linux
+#if defined(linux) || defined(__OpenBSD__)
     struct winsize win;
-    extern Term_RowWidth,crow;
+    extern int Term_RowWidth,crow;
 #endif
 #if defined(SYSVR2) && !defined(linux)
 	setpgrp();
@@ -1140,16 +1156,18 @@ char **argv;
 	setpgrp(0, pid);
 #endif /* BSD42 */
 
+#if !(defined(BSD) && (BSD >= 199306))
 	if (setutmp(ttypfd) == ERROR) {
 	    puts("Can't set utmp.");
 	} 
+#endif
 
-#ifdef linux
+#if defined(linux) || defined(__OpenBSD__)
 	setsid();
 	open_ttyp();
 	close(ptyfd);
 	ioctl(ttyfd, TIOCGWINSZ, &win);
-	ioctl(ttypfd, TCSETA, &savetmio);
+	tcsetattr(ttypfd, TCSANOW, &savetmio);
 	dup2(ttypfd, 0);
 	dup2(ttypfd, 1);
 	dup2(ttypfd, 2);
@@ -1187,7 +1205,7 @@ char **argv;
 	    err("Can't set utmp.");
 	} 
 */
-#ifdef linux
+#if defined(linux) || defined(__OpenBSD__)
  	crow = win.ws_row = Term_RowWidth = win.ws_row - conv_lines;
 	ioctl(ttyfd, TIOCSWINSZ, &win);
 	setgid(getgid());
@@ -1198,6 +1216,7 @@ char **argv;
     }
 }
 
+#if 0
 /** ĶѿΥå */
 void
 setenv(var, value)
@@ -1234,6 +1253,7 @@ char *value;
     strcat(environ[i], "=");
     strcat(environ[i], value);
 }
+#endif
 
 #ifdef SVR4
 static int
@@ -1342,7 +1362,7 @@ open_ttyp()
     chown(nmbuf, getuid(), getgid());
     chmod(nmbuf, 0622);
 #endif /* !linux */
-#ifdef BSD42
+#ifdef USE_SGTTY
     ioctl(ttyfd, TIOCGETC, &tcharsv);
     ioctl(ttyfd, TIOCGLTC, &ltcharsv);
     ioctl(ttyfd, TIOCGETP, &ttyb);
@@ -1446,11 +1466,23 @@ open_ttyp()
     close(open(ttyname(ttypfd), O_WRONLY, 0));
 */
 #endif /* defined(SYSVR2) && !defined(linux) */
+#ifdef __OpenBSD__
+    {
+    struct winsize win;
+
+    if (ioctl(ttyfd, TIOCGWINSZ, &win) != -1) {
+	win.ws_row = crow;
+	win.ws_col = maxlength;
+	ioctl(ttypfd, TIOCSWINSZ, &win);
+    }
+    }
+#else
 #ifdef TIOCSSIZE
     pty_rowcol.ts_lines = crow; /* instead of lines */
     pty_rowcol.ts_cols = maxlength; /* instead of columns */
     ioctl(ttypfd, TIOCSSIZE, &pty_rowcol);
 #endif /* TIOCSSIZE */
+#endif
 }
 
 /** pty Υץ */
@@ -1524,15 +1556,13 @@ do_end()
     signal(SIGCHLD, SIG_DFL);
     fcntl(ttyfd, F_SETFL, 0);
 
-#ifdef BSD42
+#ifdef USE_SGTTY
     ioctl(ttyfd, TIOCSETP, &savetmio);
-#endif /* BSD42 */
-#if defined(BSD43) || defined(DGUX)
     ioctl(ttyfd, TIOCLSET, &local_mode_sv);
 #endif /* BSD43 */
 
-#ifdef SYSVR2
-    if(ioctl(ttyfd, TCSETA, &savetmio) < 0) {
+#ifdef USE_TERMIOS
+    if(tcsetattr(ttyfd, TCSANOW, &savetmio) < 0) {
 	fprintf(stderr, "error in ioctl TCSETA.\n");
 	exit(1);
     }
@@ -1562,9 +1592,11 @@ do_end()
 	perror(prog);
     }
 
+#if !(defined(BSD) && (BSD >= 199306))
     if (resetutmp(ttypfd) == ERROR) {
 	printf("Can't reset utmp.");
     }
+#endif
 #ifdef TIOCSSIZE
     pty_rowcol.ts_lines = 0;
     pty_rowcol.ts_cols = 0;
@@ -1636,7 +1668,16 @@ ptyname(b, pty, no)
 char *b, *pty;
 int no;
 {
+/*
+ * Change pseudo-devices.
+ * Because FreeBSD's master pseudo-devices are pty[p-sP-S][0-9a-v].
+ * Patched by Hidekazu Kuroki(hidekazu@cs.titech.ac.jp)		1996/8/20
+ */
+#if (defined(BSD) && (BSD >= 199306)) /* 4.4BSD-Lite by Taoka */
+    sprintf(b, "%s%1c%1c", pty, "pqrsPQRS"[(no >> 5)], ((no & 0x1f > 9)? 'a' : '0') + (no & 0x1f));
+#else /* ! 4.4BSD-Lite */
     sprintf(b, "%s%1c%1x", pty, 'p' + (no >> 4), no & 0x0f);
+#endif /* ! 4.4BSD-Lite */
 }
 #endif /* !sgi */
 
@@ -1722,7 +1763,7 @@ setsize()
 {
 	register int i;
 	struct winsize win;
-	extern Term_LineWidth,Term_RowWidth,maxlength,crow;
+	extern int Term_LineWidth,Term_RowWidth,maxlength,crow;
 
 	if (ioctl(ttyfd, TIOCGWINSZ, &win) < 0) {
 		/* Default set at getTermData() */
@@ -1744,7 +1785,7 @@ change_size()
 {
 	register int i;
 	struct winsize win;
-	extern Term_LineWidth,Term_RowWidth,maxlength,crow;
+	extern int Term_LineWidth,Term_RowWidth,maxlength,crow;
 
 	if (ioctl(ttyfd, TIOCGWINSZ, &win) < 0) {
 		/* Default set at getTermData() */
@@ -1755,14 +1796,14 @@ change_size()
 
 		if ((i = win.ws_row) != 0) {
 			crow = Term_RowWidth = i - conv_lines;
-#ifdef linux
+#if defined(linux) || defined(__OpenBSD__)
 			win.ws_row = crow;
 #endif
 		}
 		if ((i = win.ws_col) != 0) {
 			maxlength = Term_LineWidth = i;
 		}
-#ifdef linux
+#if defined(linux) || defined(__OpenBSD__)
 		ioctl(ttypfd, TIOCSWINSZ, &win);
 #else /* linux */
 #ifdef	TIOCSSIZE
@@ -1770,7 +1811,8 @@ change_size()
     		pty_rowcol.ts_cols = maxlength; /* instead of columns */
     		ioctl(ttypfd, TIOCSSIZE, &pty_rowcol);
 #endif	/* TIOCSSIZE */
-#ifdef	sun	/* When your machine needs SIGWINCH, add your machine */
+#endif
+#if defined(sun) || defined(__OpenBSD__)	/* When your machine needs SIGWINCH, add your machine */
 		{
 		int grp;
 		ioctl(ptyfd, TIOCGPGRP, &grp);
@@ -1781,7 +1823,6 @@ change_size()
 #endif
 		}
 #endif	/* sun */
-#endif /* linux */
 
 		set_scroll_region(0, crow - 1);
 		if (henkan_off_flag) {
