diff -ur xemacs-21.1.orig/configure.in xemacs-21.1/configure.in
--- xemacs-21.1.orig/configure.in	Tue Apr 25 12:56:09 2000
+++ xemacs-21.1/configure.in	Tue Apr 25 16:11:54 2000
@@ -3105,6 +3105,11 @@
 
 AC_CHECK_FUNCS(cbrt closedir dup2 eaccess fmod fpathconf frexp ftime gethostname getpagesize gettimeofday getcwd getwd logb lrand48 matherr mkdir mktime perror poll random rename res_init rint rmdir select setitimer setpgid setlocale setsid sigblock sighold sigprocmask snprintf strcasecmp strerror tzset ulimit usleep utimes waitpid vsnprintf)
 
+# Check for UNIX98 PTYs.
+# getpt is a glibc addition which emulates the master device on
+# systems without kernel support.
+AC_CHECK_FUNCS(grantpt unlockpt getpt ptsname)
+
 dnl realpath is buggy on linux, decosf and aix4
 
 dnl The realpath() in linux libc (4.6.27) sometimes fails with ELOOP.
diff -ur xemacs-21.1.orig/src/config.h.in xemacs-21.1/src/config.h.in
--- xemacs-21.1.orig/src/config.h.in	Tue Oct 28 12:36:08 1997
+++ xemacs-21.1/src/config.h.in	Tue Apr 25 16:11:54 2000
@@ -297,6 +297,14 @@
 #undef HAVE_UTIMES
 #undef HAVE_WAITPID
 #undef HAVE_VSNPRINTF
+/* UNIX98 PTY support functions
+   getpt is a glibc addition which emulates the master device on
+   systems without kernel support. */
+#undef HAVE_GRANTPT
+#undef HAVE_UNLOCKPT
+#undef HAVE_GETPT
+#undef HAVE_PTSNAME
+
 #undef HAVE_SOCKETS
 #undef HAVE_SOCKADDR_SUN_LEN
 #undef HAVE_MULTICAST
diff -ur xemacs-21.1.orig/src/s/gnu.h xemacs-21.1/src/s/gnu.h
--- xemacs-21.1.orig/src/s/gnu.h	Sun Jul 20 00:12:29 1997
+++ xemacs-21.1/src/s/gnu.h	Tue Apr 25 16:11:54 2000
@@ -50,3 +50,49 @@
 #ifndef NOT_C_CODE
 #include <fcntl.h>
 #endif
+
+#if defined(HAVE_GRANTPT) && defined(HAVE_UNLOCKPT) && defined(HAVE_PTSNAME)
+/* UNIX98 PTYs are available.
+   Added by Florian Weimer <Florian.Weimer@RUS.Uni-Stuttgart.DE>,
+   RUS-CERT, University of Stuttgart.  Based on Emacs code for DGUX. */
+
+#define PTY_ITERATION for (i = 0; i < 1; i++)
+/* no iteration at all */
+
+/* Use getpt() if it's available, because it provides Unix98 PTY
+   emulation for kernels which doesn't support it natively. */
+
+#ifdef HAVE_GETPT
+#define PTY_OPEN                                 \
+  do {                                           \
+    fd = getpt();                             \
+    if (fcntl (fd, F_SETFL, O_NDELAY) == -1)  \
+      fatal ("could not set master PTY to non-block mode"); \
+  } while (0)
+
+#else
+/* the master PTY device */
+#define PTY_NAME_SPRINTF strcpy (pty_name, "/dev/ptmx");
+#endif
+
+/* This sets the name of the slave side of the PTY.  grantpt(3) and
+   unlockpt(3) may fork a subprocess, so keep sigchld_handler() from
+   intercepting that death. */
+
+#define PTY_TTY_NAME_SPRINTF			\
+  {						\
+    char *ptsname(), *ptyname;			\
+						\
+    sigblock(sigmask(SIGCHLD));			\
+    if (grantpt(fd) == -1)			\
+      fatal("could not grant slave pty");	\
+    if (unlockpt(fd) == -1)			\
+      fatal("could not unlock slave pty");	\
+    if (!(ptyname = ptsname(fd)))		\
+      fatal ("could not enable slave pty");	\
+    strncpy(pty_name, ptyname, sizeof(pty_name)); \
+    pty_name[sizeof(pty_name) - 1] = 0;		\
+    sigsetmask(siggetmask() & ~sigmask(SIGCHLD));	\
+  }
+
+#endif
diff -ur xemacs-21.1.orig/src/s/hpux.h xemacs-21.1/src/s/hpux.h
--- xemacs-21.1.orig/src/s/hpux.h	Mon Jul 14 00:43:10 1997
+++ xemacs-21.1/src/s/hpux.h	Tue Apr 25 16:11:54 2000
@@ -157,6 +157,59 @@
 /* This is needed for HPUX version 6.2; it may not be needed for 6.2.1.  */
 #define SHORT_CAST_BUG
 
+#if defined(HAVE_GRANTPT) && defined(HAVE_UNLOCKPT) && defined(HAVE_PTSNAME)
+/* UNIX98 PTYs are available.
+   Added by Florian Weimer <Florian.Weimer@RUS.Uni-Stuttgart.DE>,
+   RUS-CERT, University of Stuttgart.  Based on Emacs code for DGUX. */
+
+#ifdef emacs
+#include <grp.h>
+#include <sys/stropts.h>
+#endif
+
+#define PTY_ITERATION for (i = 0; i < 1; i++)
+/* no iteration at all */
+
+/* the master PTY device */
+#define PTY_NAME_SPRINTF strcpy (pty_name, "/dev/ptmx");
+
+/* This sets the name of the slave side of the PTY.  grantpt(3) and
+   unlockpt(3) may fork a subprocess, so keep sigchld_handler() from
+   intercepting that death.  grantpt() behavior on HP-UX differs from
+   what's specified in the man page: the group of the slave PTY is set
+   to the user's primary group, and we fix that.  */
+
+#define PTY_TTY_NAME_SPRINTF			\
+  {						\
+    char *ptsname(), *ptyname;			\
+    struct group *getgrnam (), *tty_group = getgrnam ("tty"); \
+    if (tty_group == NULL)                      \
+      fatal ("group tty not found");            \
+						\
+    sigblock(sigmask(SIGCHLD));			\
+    if (grantpt(fd) == -1)			\
+      fatal("could not grant slave pty");	\
+    if (!(ptyname = ptsname(fd)))		\
+      fatal ("could not enable slave pty");	\
+    strncpy(pty_name, ptyname, sizeof(pty_name)); \
+    pty_name[sizeof(pty_name) - 1] = 0;		\
+    if (chown (pty_name, (uid_t) -1, tty_group->gr_gid) == -1) \
+      fatal ("could not chown slave pty");      \
+    if (unlockpt(fd) == -1)			\
+      fatal("could not unlock slave pty");	\
+    sigunblock(sigmask(SIGCHLD));		\
+  }
+
+/* Push various streams modules onto a PTY channel. */
+
+#define SETUP_SLAVE_PTY \
+  if (ioctl (xforkin, I_PUSH, "ptem") == -1)	\
+    fatal ("ioctl I_PUSH ptem", errno);		\
+  if (ioctl (xforkin, I_PUSH, "ldterm") == -1)	\
+    fatal ("ioctl I_PUSH ldterm", errno);
+
+#else /* no UNIX98 PTYs */
+
 /* This is how to get the device name of the tty end of a pty.  */
 #define PTY_TTY_NAME_SPRINTF \
             sprintf (pty_name, "/dev/pty/tty%c%x", c, i);
@@ -164,6 +217,8 @@
 /* This is how to get the device name of the control end of a pty.  */
 #define PTY_NAME_SPRINTF \
 	sprintf (pty_name, "/dev/ptym/pty%c%x", c, i);
+
+#endif /* UNIX 98 PTYs */
 
 /* This triggers a conditional in xfaces.c.  */
 #define XOS_NEEDS_TIME_H
diff -ur xemacs-21.1.orig/src/s/linux.h xemacs-21.1/src/s/linux.h
--- xemacs-21.1.orig/src/s/linux.h	Tue Apr 25 12:56:09 2000
+++ xemacs-21.1/src/s/linux.h	Tue Apr 25 16:12:54 2000
@@ -194,3 +194,49 @@
 
 /* XEmacs: removed setpgrp() definition because we use setpgid() when
    it's available, and autodetect it. */
+
+#if defined(HAVE_GRANTPT) && defined(HAVE_UNLOCKPT) && defined(HAVE_PTSNAME)
+/* UNIX98 PTYs are available.
+   Added by Florian Weimer <Florian.Weimer@RUS.Uni-Stuttgart.DE>,
+   RUS-CERT, University of Stuttgart.  Based on Emacs code for DGUX. */
+
+#define PTY_ITERATION for (i = 0; i < 1; i++)
+/* no iteration at all */
+
+/* Use getpt() if it's available, because it provides Unix98 PTY
+   emulation for kernels which doesn't support it natively. */
+
+#ifdef HAVE_GETPT
+#define PTY_OPEN                                 \
+  do {                                           \
+    fd = getpt();                             \
+    if (fcntl (fd, F_SETFL, O_NDELAY) == -1)  \
+      fatal ("could not set master PTY to non-block mode"); \
+  } while (0)
+
+#else
+/* the master PTY device */
+#define PTY_NAME_SPRINTF strcpy (pty_name, "/dev/ptmx");
+#endif
+
+/* This sets the name of the slave side of the PTY.  grantpt(3) and
+   unlockpt(3) may fork a subprocess, so keep sigchld_handler() from
+   intercepting that death. */
+
+#define PTY_TTY_NAME_SPRINTF			\
+  {						\
+    char *ptsname(), *ptyname;			\
+						\
+    sigblock(sigmask(SIGCHLD));			\
+    if (grantpt(fd) == -1)			\
+      fatal("could not grant slave pty");	\
+    if (unlockpt(fd) == -1)			\
+      fatal("could not unlock slave pty");	\
+    if (!(ptyname = ptsname(fd)))		\
+      fatal ("could not enable slave pty");	\
+    strncpy(pty_name, ptyname, sizeof(pty_name)); \
+    pty_name[sizeof(pty_name) - 1] = 0;		\
+    sigsetmask(siggetmask() & ~sigmask(SIGCHLD));	\
+  }
+
+#endif
--- xemacs-21.1.10/src/editfns.c~	Wed Jul  7 05:10:33 1999
+++ xemacs-21.1.10/src/editfns.c	Wed May 10 12:05:10 2000
@@ -602,6 +602,12 @@
   return make_char (BUF_FETCH_CHAR (b, n));
 }
 
+#if !defined(WINDOWSNT) && !defined(MSDOS)
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <limits.h>
+#endif
 
 DEFUN ("temp-directory", Ftemp_directory, 0, 0, 0, /*
 Return the pathname to the directory to use for temporary files.
@@ -621,7 +627,47 @@
 #else /* WINDOWSNT || MSDOS */
  tmpdir = getenv ("TMPDIR");
  if (!tmpdir)
+    {
+      struct stat st;
+      char * logname = user_login_name(NULL);
+      int myuid      = getuid();
+      static char path[1+_POSIX_PATH_MAX];
+
+      strcpy(path, "/tmp/"); strncat(path, logname, _POSIX_PATH_MAX);
+      if (lstat(path, &st) < 0 && errno == ENOENT)
+	{
+	  mkdir(path, 0700);	/* ignore retval -- checked next anyway. */
+	}
+      if (lstat(path, &st) == 0 && st.st_uid == myuid && S_ISDIR(st.st_mode))
+	{
+	  tmpdir = path;
+	}
+      else
+	{
+	  strcpy(path, getenv("HOME")); strncat(path, "/tmp/", _POSIX_PATH_MAX);
+	  if (stat(path, &st) < 0 && errno == ENOENT)
+	    {
+	      int fd;
+	      char warnpath[1+_POSIX_PATH_MAX];
+	      mkdir(path, 0700);	/* ignore retvals */
+	      strcpy(warnpath, path);
+	      strncat(warnpath, ".created_by_xemacs", _POSIX_PATH_MAX);
+	      if ((fd = open(warnpath, O_WRONLY|O_CREAT, 0644)) > 0)
+		{
+		  write(fd, "XEmacs created this directory because /tmp/<yourname> was unavailable -- \nPlease check !\n", 89);
+		  close(fd);
+		}
+	    }
+	  if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
+	    {
+	      tmpdir = path;
+	    }
+	  else
+	    {
    tmpdir = "/tmp";
+	    }
+	}
+    }
 #endif
 
   return build_ext_string (tmpdir, FORMAT_FILENAME);
