(echo-area-clear-hook): Undo Oct 29 change.
[bpt/emacs.git] / src / sysdep.c
index cc003ee..e69c2b1 100644 (file)
@@ -1,11 +1,12 @@
 /* Interfaces to system-dependent kernel and library entries.
-   Copyright (C) 1985, 1986, 1987, 1988, 1993 Free Software Foundation, Inc.
+   Copyright (C) 1985, 86,87,88,93,94,95, 1999, 2000, 2001
+   Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
 GNU Emacs is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 1, or (at your option)
+the Free Software Foundation; either version 2, or (at your option)
 any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
@@ -15,42 +16,56 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
+the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
 
 
+#include "config.h"
 #include <signal.h>
 #include <setjmp.h>
-
-#include "config.h"
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
 #include "lisp.h"
+/* Including stdlib.h isn't necessarily enough to get srandom
+   declared, e.g. without __USE_XOPEN_EXTENDED with glibc 2.  */
+#ifdef HAVE_RANDOM
+#if 0 /* It turns out that defining _OSF_SOURCE in osf5-0.h gets
+        random prototyped as returning `int'.  It looks to me as
+        though the best way to DTRT is to prefer the rand48 functions
+        (per libc.info).  -- fx */
+extern long int random P_ ((void));
+#endif
+#if 0 /* Don't prototype srandom; it takes an unsigned argument on
+        some systems, and an unsigned long on others, like FreeBSD
+        4.1.  */
+extern void srandom P_ ((unsigned int));
+#endif
+#endif
+
 #include "blockinput.h"
 #undef NULL
 
-#define min(x,y) ((x) > (y) ? (y) : (x))
-
-/* In this file, open, read and write refer to the system calls,
-   not our sugared interfaces  sys_open, sys_read and sys_write.
-   Contrariwise, for systems where we use the system calls directly,
-   define sys_read, etc. here as aliases for them.  */
-#ifndef read
-#define sys_read read
-#define sys_write write
-#endif /* `read' is not a macro */
-
-#undef read
-#undef write
+#ifdef macintosh
+/* It is essential to include stdlib.h so that this file picks up
+   the correct definitions of rand, srand, and RAND_MAX.
+   Otherwise random numbers will not work correctly.  */
+#include <stdlib.h>
 
-#ifndef close
-#define sys_close close
-#else 
-#undef close
+#ifndef subprocesses
+/* Nonzero means delete a process right away if it exits (process.c).  */
+static int delete_exited_processes;
 #endif
-
-#ifndef open
-#define sys_open open
-#else /* `open' is a macro */
-#undef open
-#endif /* `open' is a macro */
+#endif  /* macintosh */
+
+#ifdef WINDOWSNT
+#define read sys_read
+#define write sys_write
+#include <windows.h>
+#ifndef NULL
+#define NULL 0
+#endif
+#endif /* not WINDOWSNT */
 
 /* Does anyone other than VMS need this? */
 #ifndef fwrite
@@ -64,9 +79,43 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include <sys/stat.h>
 #include <errno.h>
 
+/* Get _POSIX_VDISABLE, if it is available.  */
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+
+#ifdef HAVE_SETPGID
+#if !defined (USG) || defined (BSD_PGRPS)
+#undef setpgrp
+#define setpgrp setpgid
+#endif
+#endif
+
+/* Get SI_SRPC_DOMAIN, if it is available.  */
+#ifdef HAVE_SYS_SYSTEMINFO_H
+#include <sys/systeminfo.h>
+#endif
+
+#ifdef MSDOS   /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
+#include <dos.h>
+#include "dosfns.h"
+#include "msdos.h"
+#include <sys/param.h>
+
+#if __DJGPP__ > 1
+extern int etext;
+extern unsigned start __asm__ ("start");
+#endif
+#endif
+
+#ifndef USE_CRT_DLL
+#ifndef errno
 extern int errno;
-#ifndef VMS
-extern char *sys_errlist[];
+#endif
 #endif
 
 #ifdef VMS
@@ -89,11 +138,11 @@ extern char *sys_errlist[];
 #ifndef RAB$C_BID
 #include <rab.h>
 #endif
-#define        MAXIOSIZE ( 32 * PAGESIZE )     /* Don't I/O more than 32 blocks at a time */
+#define MAXIOSIZE (32 * PAGESIZE) /* Don't I/O more than 32 blocks at a time */
 #endif /* VMS */
 
 #ifndef BSD4_1
-#ifdef BSD /* this is done this way to avoid defined (BSD) || defined (USG)
+#ifdef BSD_SYSTEM /* avoid writing defined (BSD_SYSTEM) || defined (USG)
              because the vms compiler doesn't grok `defined' */
 #include <fcntl.h>
 #endif
@@ -104,33 +153,24 @@ extern char *sys_errlist[];
 #endif
 #endif /* not 4.1 bsd */
 
-/* Get DGUX definition for FASYNC - DJB */
-#ifdef DGUX
-#include <sys/file.h>
-#endif /* DGUX */
-
+#ifndef MSDOS
 #include <sys/ioctl.h>
-#include "systty.h"
+#endif
 
-#ifdef BSD
-#ifdef BSD4_1
-#include <wait.h>
-#else /* not 4.1 */
-#include <sys/wait.h>
-#endif /* not 4.1 */
-#endif /* BSD */
+#include "systty.h"
+#include "syswait.h"
 
 #ifdef BROKEN_TIOCGWINSZ
 #undef TIOCGWINSZ
+#undef TIOCSWINSZ
 #endif
 
-#ifdef USG
+#if defined (USG) || defined (DGUX)
 #include <sys/utsname.h>
-#include <string.h>
 #ifndef MEMORY_IN_STRING_H
 #include <memory.h>
 #endif
-#ifdef TIOCGWINSZ
+#if defined (TIOCGWINSZ) || defined (ISC4_0)
 #ifdef NEED_SIOCTL
 #include <sys/sioctl.h>
 #endif
@@ -138,11 +178,12 @@ extern char *sys_errlist[];
 #include <sys/stream.h>
 #include <sys/ptem.h>
 #endif
-#endif /* TIOCGWINSZ */
-#endif /* USG */
+#endif /* TIOCGWINSZ or ISC4_0 */
+#endif /* USG or DGUX */
 
 extern int quit_char;
 
+#include "keyboard.h"
 #include "frame.h"
 #include "window.h"
 #include "termhooks.h"
@@ -151,12 +192,43 @@ extern int quit_char;
 #include "dispextern.h"
 #include "process.h"
 
+#ifdef WINDOWSNT
+#include <direct.h>
+/* In process.h which conflicts with the local copy.  */
+#define _P_WAIT 0
+int _CRTAPI1 _spawnlp (int, const char *, const char *, ...);
+int _CRTAPI1 _getpid (void);
+#endif
+
 #ifdef NONSYSTEM_DIR_LIBRARY
 #include "ndir.h"
 #endif /* NONSYSTEM_DIR_LIBRARY */
 
 #include "syssignal.h"
 #include "systime.h"
+#ifdef HAVE_UTIME_H
+#include <utime.h>
+#endif
+
+#ifndef HAVE_UTIMES
+#ifndef HAVE_STRUCT_UTIMBUF
+/* We want to use utime rather than utimes, but we couldn't find the
+   structure declaration.  We'll use the traditional one.  */
+struct utimbuf {
+  long actime;
+  long modtime;
+};
+#endif
+#endif
+
+/* LPASS8 is new in 4.3, and makes cbreak mode provide all 8 bits.  */
+#ifndef LPASS8
+#define LPASS8 0
+#endif
+
+#ifdef BSD4_1
+#define LNOFLSH 0100000
+#endif
 
 static int baud_convert[] =
 #ifdef BAUD_CONVERT
@@ -168,16 +240,51 @@ static int baud_convert[] =
   };
 #endif
 
-extern short ospeed;
+#ifdef HAVE_SPEED_T
+#include <termios.h>
+#else
+#if defined (HAVE_LIBNCURSES) && ! defined (NCURSES_OSPEED_T)
+#else
+#if defined (HAVE_TERMIOS_H) && defined (LINUX)
+#include <termios.h>
+#endif
+#endif
+#endif
+
+int emacs_ospeed;
 
 /* The file descriptor for Emacs's input terminal.
-   Under Unix, this is always left zero;
-   under VMS, we place the input channel number here.
-   This allows us to write more code that works for both VMS and Unix.  */
-static int input_fd;
+   Under Unix, this is normally zero except when using X;
+   under VMS, we place the input channel number here.  */
+int input_fd;
+
+void croak P_ ((char *));
+
+#ifdef AIXHFT
+void hft_init ();
+void hft_reset ();
+#endif
+
+/* Temporary used by `sigblock' when defined in terms of signprocmask.  */
+
+SIGMASKTYPE sigprocmask_set;
 
+\f
+/* Specify a different file descriptor for further input operations.  */
+
+void
+change_input_fd (fd)
+     int fd;
+{
+  input_fd = fd;
+}
+
+/* Discard pending input on descriptor input_fd.  */
+
+void
 discard_tty_input ()
 {
+#ifndef WINDOWSNT
   struct emacs_tty buf;
 
   if (noninteractive)
@@ -197,78 +304,107 @@ discard_tty_input ()
 #ifdef APOLLO
   {
     int zero = 0;
-    ioctl (0, TIOCFLUSH, &zero);
+    ioctl (input_fd, TIOCFLUSH, &zero);
   }
 #else /* not Apollo */
+#ifdef MSDOS    /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
+  while (dos_keyread () != -1)
+    ;
+#else /* not MSDOS */
   EMACS_GET_TTY (input_fd, &buf);
   EMACS_SET_TTY (input_fd, &buf, 0);
+#endif /* not MSDOS */
 #endif /* not Apollo */
 #endif /* not VMS */
+#endif /* not WINDOWSNT */
 }
 
 #ifdef SIGTSTP
 
+/* Arrange for character C to be read as the next input from
+   the terminal.  */
+
+void
 stuff_char (c)
      char c;
 {
+  if (read_socket_hook)
+    return;
+
 /* Should perhaps error if in batch mode */
 #ifdef TIOCSTI
-  ioctl (0, TIOCSTI, &c);
+  ioctl (input_fd, TIOCSTI, &c);
 #else /* no TIOCSTI */
-  error ("Cannot stuff terminal input characters in this version of Unix.");
+  error ("Cannot stuff terminal input characters in this version of Unix");
 #endif /* no TIOCSTI */
 }
 
 #endif /* SIGTSTP */
-
+\f
+void
 init_baud_rate ()
 {
   if (noninteractive)
-    ospeed = 0;
+    emacs_ospeed = 0;
   else
     {
+#ifdef INIT_BAUD_RATE
+      INIT_BAUD_RATE ();
+#else
+#ifdef DOS_NT
+    emacs_ospeed = 15;
+#else  /* not DOS_NT */
 #ifdef VMS
       struct sensemode sg;
 
       SYS$QIOW (0, input_fd, IO$_SENSEMODE, &sg, 0, 0,
                &sg.class, 12, 0, 0, 0, 0 );
-      ospeed = sg.xmit_baud;
+      emacs_ospeed = sg.xmit_baud;
 #else /* not VMS */
 #ifdef HAVE_TERMIOS
       struct termios sg;
 
-      sg.c_cflag = (sg.c_cflag & ~CBAUD) | B9600;
-      tcgetattr (0, &sg);
-      ospeed = sg.c_cflag & CBAUD;
+      sg.c_cflag = B9600;
+      tcgetattr (input_fd, &sg);
+      emacs_ospeed = cfgetospeed (&sg);
+#if defined (USE_GETOBAUD) && defined (getobaud)
+      /* m88k-motorola-sysv3 needs this (ghazi@noc.rutgers.edu) 9/1/94. */
+      if (emacs_ospeed == 0)
+        emacs_ospeed = getobaud (sg.c_cflag);
+#endif
 #else /* neither VMS nor TERMIOS */
 #ifdef HAVE_TERMIO
       struct termio sg;
 
-      sg.c_cflag = (sg.c_cflag & ~CBAUD) | B9600;
+      sg.c_cflag = B9600;
 #ifdef HAVE_TCATTR
-      tcgetattr (0, &sg);
+      tcgetattr (input_fd, &sg);
 #else
       ioctl (input_fd, TCGETA, &sg);
 #endif
-      ospeed = sg.c_cflag & CBAUD;
+      emacs_ospeed = sg.c_cflag & CBAUD;
 #else /* neither VMS nor TERMIOS nor TERMIO */
       struct sgttyb sg;
       
       sg.sg_ospeed = B9600;
-      ioctl (0, TIOCGETP, &sg);
-      ospeed = sg.sg_ospeed;
+      if (ioctl (input_fd, TIOCGETP, &sg) < 0)
+       abort ();
+      emacs_ospeed = sg.sg_ospeed;
 #endif /* not HAVE_TERMIO */
 #endif /* not HAVE_TERMIOS */
 #endif /* not VMS */
+#endif /* not DOS_NT */
+#endif /* not INIT_BAUD_RATE */
     }
    
-  baud_rate = (ospeed < sizeof baud_convert / sizeof baud_convert[0]
-              ? baud_convert[ospeed] : 9600);
+  baud_rate = (emacs_ospeed < sizeof baud_convert / sizeof baud_convert[0]
+              ? baud_convert[emacs_ospeed] : 9600);
   if (baud_rate == 0)
     baud_rate = 1200;
 }
 
 /*ARGSUSED*/
+void
 set_exclusive_use (fd)
      int fd;
 {
@@ -277,12 +413,12 @@ set_exclusive_use (fd)
 #endif
   /* Ok to do nothing if this feature does not exist */
 }
-
+\f
 #ifndef subprocesses
 
 wait_without_blocking ()
 {
-#ifdef BSD
+#ifdef BSD_SYSTEM
   wait3 (0, WNOHANG | WUNTRACED, 0);
 #else
   croak ("wait_without_blocking");
@@ -293,7 +429,7 @@ wait_without_blocking ()
 #endif /* not subprocesses */
 
 int wait_debugging;   /* Set nonzero to make following function work under dbx
-                        (at least for bsd).  */
+                        (at least for bsd).  */
 
 SIGTYPE
 wait_for_termination_signal ()
@@ -302,6 +438,7 @@ wait_for_termination_signal ()
 /* Wait for subprocess with process id `pid' to terminate and
    make sure it will get eliminated (not remain forever as a zombie) */
 
+void
 wait_for_termination (pid)
      int pid;
 {
@@ -314,7 +451,7 @@ wait_for_termination (pid)
       status = SYS$FORCEX (&pid, 0, 0);
       break;
 #else /* not VMS */
-#if (defined (BSD) && !defined (LINUX)) || (defined (HPUX) && !defined (HPUX_5))
+#if defined (BSD_SYSTEM) || (defined (HPUX) && !defined (HPUX_5))
       /* Note that kill returns -1 even if the process is just a zombie now.
         But inevitably a SIGCHLD interrupt should be generated
         and child_sig will do wait3 and make the process go away. */
@@ -324,7 +461,7 @@ wait_for_termination (pid)
         if that causes the problem to go away or get worse.  */
       sigsetmask (sigmask (SIGCHLD));
       if (0 > kill (pid, 0))
-        {
+       {
          sigsetmask (SIGEMPTYMASK);
          kill (getpid (), SIGCHLD);
          break;
@@ -333,12 +470,27 @@ wait_for_termination (pid)
        sleep (1);
       else
        sigpause (SIGEMPTYMASK);
-#else /* not BSD, not LINUX, and not HPUX version >= 6 */
-#if defined (UNIPLUS) || defined (LINUX)
+#else /* not BSD_SYSTEM, and not HPUX version >= 6 */
+#if defined (UNIPLUS)
       if (0 > kill (pid, 0))
        break;
       wait (0);
-#else /* neither BSD nor UNIPLUS nor LINUX: random sysV */
+#else /* neither BSD_SYSTEM nor UNIPLUS: random sysV */
+#ifdef POSIX_SIGNALS    /* would this work for LINUX as well? */
+      sigblock (sigmask (SIGCHLD));
+      errno = 0;
+      if (kill (pid, 0) == -1 && errno == ESRCH)
+       {
+         sigunblock (sigmask (SIGCHLD));
+         break;
+       }
+
+      /* FIXME: Since sigpause is not POSIX and its use is deprecated,
+        this should probably be `sigsuspend (&empty_mask)', which is
+        POSIX.  I'm not making that change right away because the
+        release is nearing.  2001-09-20 gerd.  */
+      sigpause (SIGEMPTYMASK);
+#else /* not POSIX_SIGNALS */
 #ifdef HAVE_SYSV_SIGPAUSE
       sighold (SIGCHLD);
       if (0 > kill (pid, 0))
@@ -348,17 +500,26 @@ wait_for_termination (pid)
        }
       sigpause (SIGCHLD);
 #else /* not HAVE_SYSV_SIGPAUSE */
+#ifdef WINDOWSNT
+      wait (0);
+      break;
+#else /* not WINDOWSNT */
       if (0 > kill (pid, 0))
        break;
       /* Using sleep instead of pause avoids timing error.
         If the inferior dies just before the sleep,
         we lose just one second.  */
       sleep (1);
+#endif /* not WINDOWSNT */
 #endif /* not HAVE_SYSV_SIGPAUSE */
+#endif /* not POSIX_SIGNALS */
 #endif /* not UNIPLUS */
-#endif /* not BSD, and not HPUX version >= 6 */
+#endif /* not BSD_SYSTEM, and not HPUX version >= 6 */
 #endif /* not VMS */
 #else /* not subprocesses */
+#if __DJGPP__ > 1
+      break;
+#else /* not __DJGPP__ > 1 */
 #ifndef BSD4_1
       if (kill (pid, 0) < 0)
        break;
@@ -369,6 +530,7 @@ wait_for_termination (pid)
       if (status == pid || status == -1)
        break;
 #endif /* BSD4_1 */
+#endif /* not __DJGPP__ > 1*/
 #endif /* not subprocesses */
     }
 }
@@ -380,6 +542,7 @@ wait_for_termination (pid)
  *      (may flush input as well; it does not matter the way we use it)
  */
  
+void
 flush_pending_output (channel)
      int channel;
 {
@@ -400,16 +563,18 @@ flush_pending_output (channel)
 #endif
 #endif
 }
-
+\f
 #ifndef VMS
 /*  Set up the terminal at the other end of a pseudo-terminal that
     we will be controlling an inferior through.
     It should not echo or do line-editing, since that is done
     in Emacs.  No padding needed for insertion into an Emacs buffer.  */
 
+void
 child_setup_tty (out)
      int out;
 {
+#ifndef DOS_NT
   struct emacs_tty s;
 
   EMACS_GET_TTY (out, &s);
@@ -417,24 +582,38 @@ child_setup_tty (out)
 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
   s.main.c_oflag |= OPOST;     /* Enable output postprocessing */
   s.main.c_oflag &= ~ONLCR;    /* Disable map of NL to CR-NL on output */
+#ifdef NLDLY
   s.main.c_oflag &= ~(NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY);
                                /* No output delays */
+#endif
   s.main.c_lflag &= ~ECHO;     /* Disable echo */
   s.main.c_lflag |= ISIG;      /* Enable signals */
-  s.main.c_iflag &= ~IUCLC;    /* Disable map of upper case to lower on
-                                  input */
-  s.main.c_oflag &= ~OLCUC;    /* Disable map of lower case to upper on
-                                  output */
+  s.main.c_iflag &= ~ICRNL;    /* Disable map of CR to NL on input */
+#ifdef INLCR  /* Just being cautious, since I can't check how
+                widespread INLCR is--rms.  */
+  s.main.c_iflag &= ~INLCR;    /* Disable map of NL to CR on input */
+#endif
+#ifdef IUCLC
+  s.main.c_iflag &= ~IUCLC;    /* Disable downcasing on input.  */
+#endif
+#ifdef ISTRIP
+  s.main.c_iflag &= ~ISTRIP;   /* don't strip 8th bit on input */
+#endif
+#ifdef OLCUC
+  s.main.c_oflag &= ~OLCUC;    /* Disable upcasing on output.  */
+#endif
+  s.main.c_oflag &= ~TAB3;     /* Disable tab expansion */
+  s.main.c_cflag = (s.main.c_cflag & ~CSIZE) | CS8; /* Don't strip 8th bit */
 #if 0
-  /* Said to be unnecesary:  */
+  /* Said to be unnecessary:  */
   s.main.c_cc[VMIN] = 1;       /* minimum number of characters to accept  */
   s.main.c_cc[VTIME] = 0;      /* wait forever for at least 1 character  */
 #endif
 
   s.main.c_lflag |= ICANON;    /* Enable erase/kill and eof processing */
   s.main.c_cc[VEOF] = 04;      /* insure that EOF is Control-D */
-  s.main.c_cc[VERASE] = 0377;  /* disable erase processing */
-  s.main.c_cc[VKILL] = 0377;   /* disable kill processing */
+  s.main.c_cc[VERASE] = CDISABLE;      /* disable erase processing */
+  s.main.c_cc[VKILL] = CDISABLE;       /* disable kill processing */
 
 #ifdef HPUX
   s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
@@ -450,11 +629,22 @@ child_setup_tty (out)
      don't ignore break, but don't signal either, so it looks like NUL.  */
   s.main.c_iflag &= ~IGNBRK;
   s.main.c_iflag &= ~BRKINT;
+  /* QUIT and INTR work better as signals, so disable character forms */
+  s.main.c_cc[VINTR] = 0377;
+#ifdef SIGNALS_VIA_CHARACTERS
+  /* the QUIT and INTR character are used in process_send_signal
+     so set them here to something useful.  */
+  if (s.main.c_cc[VQUIT] == 0377)
+    s.main.c_cc[VQUIT] = '\\'&037;     /* Control-\ */
+  if (s.main.c_cc[VINTR] == 0377)
+    s.main.c_cc[VINTR] = 'C'&037;      /* Control-C */
+#else /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
   /* QUIT and INTR work better as signals, so disable character forms */
   s.main.c_cc[VQUIT] = 0377;
   s.main.c_cc[VINTR] = 0377;
-  s.main.c_cc[VEOL] = 0377;
   s.main.c_lflag &= ~ISIG;
+#endif /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
+  s.main.c_cc[VEOL] = 0377;
   s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
 #endif /* AIX */
 
@@ -462,8 +652,10 @@ child_setup_tty (out)
 
   s.main.sg_flags &= ~(ECHO | CRMOD | ANYP | ALLDELAY | RAW | LCASE
                       | CBREAK | TANDEM);
+  s.main.sg_flags |= LPASS8;
   s.main.sg_erase = 0377;
   s.main.sg_kill = 0377;
+  s.lmode = LLITOUT | s.lmode;        /* Don't strip 8th bit */
 
 #endif /* not HAVE_TERMIO */
 
@@ -479,27 +671,25 @@ child_setup_tty (out)
     ioctl (out, FIOASYNC, &zero);
   }
 #endif /* RTU */
+#endif /* not DOS_NT */
 }
 #endif /* not VMS */
 
 #endif /* subprocesses */
-
-/*ARGSUSED*/
-setpgrp_of_tty (pid)
-     int pid;
-{
-  EMACS_SET_TTY_PGRP (input_fd, &pid);
-}
-
+\f
 /* Record a signal code and the handler for it.  */
 struct save_signal
 {
   int code;
-  SIGTYPE (*handler) ();
+  SIGTYPE (*handler) P_ ((int));
 };
 
+static void save_signal_handlers P_ ((struct save_signal *));
+static void restore_signal_handlers P_ ((struct save_signal *));
+
 /* Suspend the Emacs process; give terminal to its superior.  */
 
+void
 sys_suspend ()
 {
 #ifdef VMS
@@ -543,9 +733,12 @@ sys_suspend ()
     }
   return -1;
 #else
-#ifdef SIGTSTP
+#if defined (SIGTSTP) && !defined (MSDOS)
 
-  EMACS_KILLPG (getpgrp (0), SIGTSTP);
+  {
+    int pgrp = EMACS_GETPGRP (0);
+    EMACS_KILLPG (pgrp, SIGTSTP);
+  }
 
 #else /* No SIGTSTP */
 #ifdef USG_JOBCTRL /* If you don't know what this is don't mess with it */
@@ -557,8 +750,29 @@ sys_suspend ()
 /* On a system where suspending is not implemented,
    instead fork a subshell and let it talk directly to the terminal
    while we wait.  */
-  int pid = fork ();
+  sys_subshell ();
+
+#endif /* no USG_JOBCTRL */
+#endif /* no SIGTSTP */
+#endif /* not VMS */
+}
+
+/* Fork a subshell.  */
+
+#ifndef macintosh
+void
+sys_subshell ()
+{
+#ifndef VMS
+#ifdef DOS_NT  /* Demacs 1.1.2 91/10/20 Manabu Higashida */
+  int st;
+  char oldwd[MAXPATHLEN+1]; /* Fixed length is safe on MSDOS.  */
+#endif
+  int pid;
   struct save_signal saved_handlers[5];
+  Lisp_Object dir;
+  unsigned char *str = 0;
+  int len;
 
   saved_handlers[0].code = SIGINT;
   saved_handlers[1].code = SIGQUIT;
@@ -570,79 +784,117 @@ sys_suspend ()
   saved_handlers[3].code = 0;
 #endif
 
+  /* Mentioning current_buffer->buffer would mean including buffer.h,
+     which somehow wedges the hp compiler.  So instead...  */
+
+  dir = intern ("default-directory");
+  if (NILP (Fboundp (dir)))
+    goto xyzzy;
+  dir = Fsymbol_value (dir);
+  if (!STRINGP (dir))
+    goto xyzzy;
+
+  dir = expand_and_dir_to_file (Funhandled_file_name_directory (dir), Qnil);
+  str = (unsigned char *) alloca (XSTRING (dir)->size + 2);
+  len = XSTRING (dir)->size;
+  bcopy (XSTRING (dir)->data, str, len);
+  if (str[len - 1] != '/') str[len++] = '/';
+  str[len] = 0;
+ xyzzy:
+
+#ifdef DOS_NT
+  pid = 0;
+#if __DJGPP__ > 1
+  save_signal_handlers (saved_handlers);
+  synch_process_alive = 1;
+#endif /* __DJGPP__ > 1 */
+#else  
+  pid = vfork ();
   if (pid == -1)
     error ("Can't spawn subshell");
+#endif
+
   if (pid == 0)
     {
-      char *sh;
+      char *sh = 0;
 
-      sh = (char *) egetenv ("SHELL");
+#ifdef DOS_NT    /* MW, Aug 1993 */
+      getwd (oldwd);
+      if (sh == 0)
+       sh = (char *) egetenv ("SUSPEND");      /* KFS, 1994-12-14 */
+#endif
+      if (sh == 0)
+       sh = (char *) egetenv ("SHELL");
       if (sh == 0)
        sh = "sh";
+
       /* Use our buffer's default directory for the subshell.  */
-      {
-       Lisp_Object dir;
-       unsigned char *str;
-       int len;
-
-       /* mentioning current_buffer->buffer would mean including buffer.h,
-          which somehow wedges the hp compiler.  So instead... */
-
-       dir = intern ("default-directory");
-       /* Can't use NULL */
-       if (XFASTINT (Fboundp (dir)) == XFASTINT (Qnil))
-         goto xyzzy;
-       dir = Fsymbol_value (dir);
-       if (XTYPE (dir) != Lisp_String)
-         goto xyzzy;
-
-       str = (unsigned char *) alloca (XSTRING (dir)->size + 2);
-       len = XSTRING (dir)->size;
-       bcopy (XSTRING (dir)->data, str, len);
-       if (str[len - 1] != '/') str[len++] = '/';
-       str[len] = 0;
-       chdir (str);
-      }
-    xyzzy:
+      if (str)
+       chdir ((char *) str);
+
 #ifdef subprocesses
       close_process_descs ();  /* Close Emacs's pipes/ptys */
 #endif
 
-#ifdef PRIO_PROCESS
+#ifdef SET_EMACS_PRIORITY
       {
        extern int emacs_priority;
 
-       if (emacs_priority)
+       if (emacs_priority < 0)
          nice (-emacs_priority);
       }
 #endif
 
+#ifdef MSDOS    /* Demacs 1.1.2 91/10/20 Manabu Higashida */
+      st = system (sh);
+      chdir (oldwd);
+#if 0  /* This is also reported if last command executed in subshell failed, KFS */
+      if (st)
+       report_file_error ("Can't execute subshell", Fcons (build_string (sh), Qnil));
+#endif
+#else /* not MSDOS */
+#ifdef  WINDOWSNT
+      /* Waits for process completion */
+      pid = _spawnlp (_P_WAIT, sh, sh, NULL);
+      chdir (oldwd);
+      if (pid == -1)
+       write (1, "Can't execute subshell", 22);
+#else   /* not WINDOWSNT */
       execlp (sh, sh, 0);
       write (1, "Can't execute subshell", 22);
       _exit (1);
+#endif  /* not WINDOWSNT */
+#endif /* not MSDOS */
     }
 
+  /* Do this now if we did not do it before.  */
+#if !defined (MSDOS) || __DJGPP__ == 1
   save_signal_handlers (saved_handlers);
   synch_process_alive = 1;
+#endif
+
+#ifndef DOS_NT
   wait_for_termination (pid);
+#endif
   restore_signal_handlers (saved_handlers);
-
-#endif /* no USG_JOBCTRL */
-#endif /* no SIGTSTP */
-#endif /* not VMS */
+  synch_process_alive = 0;
+#endif /* !VMS */
 }
+#endif /* !macintosh */
 
+static void
 save_signal_handlers (saved_handlers)
      struct save_signal *saved_handlers;
 {
   while (saved_handlers->code)
     {
       saved_handlers->handler
-       = (SIGTYPE (*) ()) signal (saved_handlers->code, SIG_IGN);
+       = (SIGTYPE (*) P_ ((int))) signal (saved_handlers->code, SIG_IGN);
       saved_handlers++;
     }
 }
 
+static void
 restore_signal_handlers (saved_handlers)
      struct save_signal *saved_handlers;
 {
@@ -657,74 +909,188 @@ restore_signal_handlers (saved_handlers)
 
 int old_fcntl_flags;
 
-init_sigio ()
+void
+init_sigio (fd)
+     int fd;
 {
 #ifdef FASYNC
-  old_fcntl_flags = fcntl (0, F_GETFL, 0) & ~FASYNC;
+  old_fcntl_flags = fcntl (fd, F_GETFL, 0) & ~FASYNC;
+  fcntl (fd, F_SETFL, old_fcntl_flags | FASYNC);
 #endif
-  request_sigio ();
+  interrupts_deferred = 0;
 }
 
+void
 reset_sigio ()
 {
   unrequest_sigio ();
 }
 
-#ifdef FASYNC          /* F_SETFL does not imply existance of FASYNC */
+#ifdef FASYNC          /* F_SETFL does not imply existence of FASYNC */
 
+void
 request_sigio ()
 {
+  if (read_socket_hook)
+    return;
+
 #ifdef SIGWINCH
   sigunblock (sigmask (SIGWINCH));
 #endif
-  fcntl (0, F_SETFL, old_fcntl_flags | FASYNC);
+  fcntl (input_fd, F_SETFL, old_fcntl_flags | FASYNC);
 
   interrupts_deferred = 0;
 }
 
+void
 unrequest_sigio ()
 {
+  if (read_socket_hook)
+    return;
+
 #ifdef SIGWINCH
   sigblock (sigmask (SIGWINCH));
 #endif
-  fcntl (0, F_SETFL, old_fcntl_flags);
+  fcntl (input_fd, F_SETFL, old_fcntl_flags);
   interrupts_deferred = 1;
 }
 
 #else /* no FASYNC */
 #ifdef STRIDE          /* Stride doesn't have FASYNC - use FIOASYNC */
 
+void
 request_sigio ()
 {
   int on = 1;
-  ioctl (0, FIOASYNC, &on);
+
+  if (read_socket_hook)
+    return;
+
+  ioctl (input_fd, FIOASYNC, &on);
   interrupts_deferred = 0;
 }
 
+void
 unrequest_sigio ()
 {
   int off = 0;
 
-  ioctl (0, FIOASYNC, &off);
+  if (read_socket_hook)
+    return;
+
+  ioctl (input_fd, FIOASYNC, &off);
   interrupts_deferred = 1;
 }
 
 #else /* not FASYNC, not STRIDE */
  
+#ifdef _CX_UX
+
+#include <termios.h>
+
+void
 request_sigio ()
 {
+  int on = 1;
+  sigset_t st;
+
+  if (read_socket_hook)
+    return;
+
+  sigemptyset (&st);
+  sigaddset (&st, SIGIO);
+  ioctl (input_fd, FIOASYNC, &on);
+  interrupts_deferred = 0;
+  sigprocmask (SIG_UNBLOCK, &st, (sigset_t *)0);
+}
+
+void
+unrequest_sigio ()
+{
+  int off = 0;
+
+  if (read_socket_hook)
+    return;
+
+  ioctl (input_fd, FIOASYNC, &off);
+  interrupts_deferred = 1;
+}
+
+#else /* ! _CX_UX */
+
+void
+request_sigio ()
+{
+  if (read_socket_hook)
+    return;
+
   croak ("request_sigio");
 }
  
+void
 unrequest_sigio ()
 {
+  if (read_socket_hook)
+    return;
+
   croak ("unrequest_sigio");
 }
  
+#endif /* _CX_UX */
 #endif /* STRIDE */
 #endif /* FASYNC */
 #endif /* F_SETFL */
 \f
+/* Saving and restoring the process group of Emacs's terminal.  */
+
+#ifdef BSD_PGRPS
+
+/* The process group of which Emacs was a member when it initially
+   started.
+
+   If Emacs was in its own process group (i.e. inherited_pgroup ==
+   getpid ()), then we know we're running under a shell with job
+   control (Emacs would never be run as part of a pipeline).
+   Everything is fine.
+
+   If Emacs was not in its own process group, then we know we're
+   running under a shell (or a caller) that doesn't know how to
+   separate itself from Emacs (like sh).  Emacs must be in its own
+   process group in order to receive SIGIO correctly.  In this
+   situation, we put ourselves in our own pgroup, forcibly set the
+   tty's pgroup to our pgroup, and make sure to restore and reinstate
+   the tty's pgroup just like any other terminal setting.  If
+   inherited_group was not the tty's pgroup, then we'll get a
+   SIGTTmumble when we try to change the tty's pgroup, and a CONT if
+   it goes foreground in the future, which is what should happen.  */
+int inherited_pgroup;
+
+/* Split off the foreground process group to Emacs alone.
+   When we are in the foreground, but not started in our own process
+   group, redirect the TTY to point to our own process group.  We need
+   to be in our own process group to receive SIGIO properly.  */
+void
+narrow_foreground_group ()
+{
+  int me = getpid ();
+
+  setpgrp (0, inherited_pgroup);
+  if (inherited_pgroup != me)
+    EMACS_SET_TTY_PGRP (input_fd, &me);
+  setpgrp (0, me);
+}
+
+/* Set the tty to our original foreground group.  */
+void
+widen_foreground_group ()
+{
+  if (inherited_pgroup != getpid ())
+    EMACS_SET_TTY_PGRP (input_fd, &inherited_pgroup);
+  setpgrp (0, inherited_pgroup);
+}
+
+#endif /* BSD_PGRPS */
+\f
 /* Getting and setting emacs_tty structures.  */
 
 /* Set *TC to the parameters associated with the terminal FD.
@@ -738,6 +1104,7 @@ emacs_get_tty (fd, settings)
   /* Retrieve the primary parameters - baud rate, character size, etcetera.  */
 #ifdef HAVE_TCATTR
   /* We have those nifty POSIX tcmumbleattr functions.  */
+  bzero (&settings->main, sizeof (settings->main));
   if (tcgetattr (fd, &settings->main) < 0)
     return -1;
 
@@ -756,22 +1123,23 @@ emacs_get_tty (fd, settings)
     return -1;
 
 #else
+#ifndef DOS_NT
   /* I give up - I hope you have the BSD ioctls.  */
   if (ioctl (fd, TIOCGETP, &settings->main) < 0)
     return -1;
-
+#endif /* not DOS_NT */
 #endif
 #endif
 #endif
 
   /* Suivant - Do we have to get struct ltchars data?  */
-#ifdef TIOCGLTC
+#ifdef HAVE_LTCHARS
   if (ioctl (fd, TIOCGLTC, &settings->ltchars) < 0)
     return -1;
 #endif
 
   /* How about a struct tchars and a wordful of lmode bits?  */
-#ifdef TIOCGETC
+#ifdef HAVE_TCHARS
   if (ioctl (fd, TIOCGETC, &settings->tchars) < 0
       || ioctl (fd, TIOCLGET, &settings->lmode) < 0)
     return -1;
@@ -783,27 +1151,28 @@ emacs_get_tty (fd, settings)
 
 
 /* Set the parameters of the tty on FD according to the contents of
-   *SETTINGS.  If WAITP is non-zero, we wait for all queued output to
-   be written before making the change; otherwise, we forget any
-   queued input and make the change immediately.
+   *SETTINGS.  If FLUSHP is non-zero, we discard input.
    Return 0 if all went well, and -1 if anything failed.  */
+
 int
-emacs_set_tty (fd, settings, waitp)
+emacs_set_tty (fd, settings, flushp)
      int fd;
      struct emacs_tty *settings;
-     int waitp;
+     int flushp;
 {
   /* Set the primary parameters - baud rate, character size, etcetera.  */
 #ifdef HAVE_TCATTR
+  int i;
   /* We have those nifty POSIX tcmumbleattr functions.
      William J. Smith <wjs@wiis.wang.com> writes:
-     "POSIX 1003.1 defines tcsetattr() to return success if it was
+     "POSIX 1003.1 defines tcsetattr to return success if it was
      able to perform any of the requested actions, even if some
      of the requested actions could not be performed.
      We must read settings back to ensure tty setup properly.
      AIX requires this to keep tty from hanging occasionally."  */
-  for (;;)
-    if (tcsetattr (fd, waitp ? TCSAFLUSH : TCSADRAIN, &settings->main) < 0)
+  /* This make sure that we don't loop indefinitely in here.  */
+  for (i = 0 ; i < 10 ; i++)
+    if (tcsetattr (fd, flushp ? TCSAFLUSH : TCSADRAIN, &settings->main) < 0)
       {
        if (errno == EINTR)
          continue;
@@ -814,18 +1183,27 @@ emacs_set_tty (fd, settings, waitp)
       {
        struct termios new;
 
+       bzero (&new, sizeof (new));
        /* Get the current settings, and see if they're what we asked for.  */
        tcgetattr (fd, &new);
-       if (memcmp (&new, &settings->main, sizeof (new)))
-         continue;
-       else
+       /* We cannot use memcmp on the whole structure here because under
+        * aix386 the termios structure has some reserved field that may
+        * not be filled in.
+        */
+       if (   new.c_iflag == settings->main.c_iflag
+           && new.c_oflag == settings->main.c_oflag
+           && new.c_cflag == settings->main.c_cflag
+           && new.c_lflag == settings->main.c_lflag
+           && memcmp (new.c_cc, settings->main.c_cc, NCCS) == 0)
          break;
+       else
+         continue;
       }
 
 #else
 #ifdef HAVE_TERMIO
   /* The SYSV-style interface?  */
-  if (ioctl (fd, waitp ? TCSETAW : TCSETAF, &settings->main) < 0)
+  if (ioctl (fd, flushp ? TCSETAF : TCSETAW, &settings->main) < 0)
     return -1;
 
 #else
@@ -837,22 +1215,24 @@ emacs_set_tty (fd, settings, waitp)
     return -1;
 
 #else
+#ifndef DOS_NT
   /* I give up - I hope you have the BSD ioctls.  */
-  if (ioctl (fd, (waitp) ? TIOCSETP : TIOCSETN, &settings->main) < 0)
+  if (ioctl (fd, (flushp) ? TIOCSETP : TIOCSETN, &settings->main) < 0)
     return -1;
+#endif /* not DOS_NT */
 
 #endif
 #endif
 #endif
 
   /* Suivant - Do we have to get struct ltchars data?  */
-#ifdef TIOCGLTC
+#ifdef HAVE_LTCHARS
   if (ioctl (fd, TIOCSLTC, &settings->ltchars) < 0)
     return -1;
 #endif
 
   /* How about a struct tchars and a wordful of lmode bits?  */
-#ifdef TIOCGETC
+#ifdef HAVE_TCHARS
   if (ioctl (fd, TIOCSETC, &settings->tchars) < 0
       || ioctl (fd, TIOCLSET, &settings->lmode) < 0)
     return -1;
@@ -866,7 +1246,11 @@ emacs_set_tty (fd, settings, waitp)
 /* The initial tty mode bits */
 struct emacs_tty old_tty;
 
-int term_initted;              /* 1 if outer tty status has been recorded */
+/* 1 if we have been through init_sys_modes.  */
+int term_initted;
+
+/* 1 if outer tty status has been recorded.  */
+int old_tty_valid;
 
 #ifdef BSD4_1
 /* BSD 4.1 needs to keep track of the lmode bits in order to start
@@ -874,31 +1258,48 @@ int term_initted;                /* 1 if outer tty status has been recorded */
 int lmode;
 #endif
 
+#ifndef F_SETOWN_BUG
 #ifdef F_SETOWN
 int old_fcntl_owner;
 #endif /* F_SETOWN */
+#endif /* F_SETOWN_BUG */
 
 /* This may also be defined in stdio,
    but if so, this does no harm,
    and using the same name avoids wasting the other one's space.  */
 
+#ifdef nec_ews_svr4
+extern char *_sobuf ;
+#else
 #if defined (USG) || defined (DGUX)
 unsigned char _sobuf[BUFSIZ+8];
 #else
 char _sobuf[BUFSIZ];
 #endif
+#endif
  
-#ifdef TIOCGLTC
+#ifdef HAVE_LTCHARS
 static struct ltchars new_ltchars = {-1,-1,-1,-1,-1,-1};
 #endif
-#ifdef TIOCGETC
-  static struct tchars new_tchars = {-1,-1,-1,-1,-1,-1};
+#ifdef HAVE_TCHARS
+static struct tchars new_tchars = {-1,-1,-1,-1,-1,-1};
 #endif 
 
+void
 init_sys_modes ()
 {
   struct emacs_tty tty;
 
+#ifdef macintosh
+/* cus-start.el complains if delete-exited-processes is not defined */
+#ifndef subprocesses
+  DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes,
+              doc: /* *Non-nil means delete processes immediately when they exit.
+nil means don't delete them until `list-processes' is run.  */);
+  delete_exited_processes = 0;
+#endif
+#endif /* not macintosh */
+
 #ifdef VMS
 #if 0
   static int oob_chars[2] = {0, 1 << 7}; /* catch C-g's */
@@ -906,6 +1307,8 @@ init_sys_modes ()
 #endif
 #endif
 
+  Vtty_erase_char = Qnil;
+
   if (noninteractive)
     return;
 
@@ -941,22 +1344,43 @@ init_sys_modes ()
 #endif
 #endif /* not VMS */
 
-  EMACS_GET_TTY (input_fd, &old_tty);
+#ifdef BSD_PGRPS
+  if (! read_socket_hook && EQ (Vwindow_system, Qnil))
+    narrow_foreground_group ();
+#endif
 
+#ifdef HAVE_WINDOW_SYSTEM
+  /* Emacs' window system on MSDOG uses the `internal terminal' and therefore
+     needs the initialization code below.  */
   if (!read_socket_hook && EQ (Vwindow_system, Qnil))
+#endif
     {
+      EMACS_GET_TTY (input_fd, &old_tty);
+
+      old_tty_valid = 1;
+
       tty = old_tty;
 
 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
+      XSETINT (Vtty_erase_char, old_tty.main.c_cc[VERASE]);
+
+#ifdef DGUX
+      /* This allows meta to be sent on 8th bit.  */
+      tty.main.c_iflag &= ~INPCK;      /* don't check input for parity */
+#endif
       tty.main.c_iflag |= (IGNBRK);    /* Ignore break condition */
       tty.main.c_iflag &= ~ICRNL;      /* Disable map of CR to NL on input */
+#ifdef INLCR  /* I'm just being cautious,
+                since I can't check how widespread INLCR is--rms.  */
+      tty.main.c_iflag &= ~INLCR;      /* Disable map of NL to CR on input */
+#endif
 #ifdef ISTRIP
       tty.main.c_iflag &= ~ISTRIP;     /* don't strip 8th bit on input */
 #endif
       tty.main.c_lflag &= ~ECHO;       /* Disable echo */
       tty.main.c_lflag &= ~ICANON;     /* Disable erase/kill processing */
 #ifdef IEXTEN
-      tty.main.c_iflag &= ~IEXTEN;     /* Disable other editing characters.  */
+      tty.main.c_lflag &= ~IEXTEN;     /* Disable other editing characters.  */
 #endif
       tty.main.c_lflag |= ISIG;        /* Enable signals */
       if (flow_control)
@@ -989,6 +1413,7 @@ init_sys_modes ()
       tty.main.c_cc[VSWTCH] = CDISABLE;        /* Turn off shell layering use
                                           of C-z */
 #endif /* VSWTCH */
+
 #if defined (mips) || defined (HAVE_TCATTR)
 #ifdef VSUSP
       tty.main.c_cc[VSUSP] = CDISABLE; /* Turn off mips handling of C-z.  */
@@ -999,10 +1424,47 @@ init_sys_modes ()
 #ifdef VDSUSP /* Some systems have VDSUSP, some have V_DSUSP.  */
       tty.main.c_cc[VDSUSP] = CDISABLE;
 #endif /* VDSUSP */
+#ifdef VLNEXT
+      tty.main.c_cc[VLNEXT] = CDISABLE;
+#endif /* VLNEXT */
+#ifdef VREPRINT
+      tty.main.c_cc[VREPRINT] = CDISABLE;
+#endif /* VREPRINT */
+#ifdef VWERASE
+      tty.main.c_cc[VWERASE] = CDISABLE;
+#endif /* VWERASE */
+#ifdef VDISCARD
+      tty.main.c_cc[VDISCARD] = CDISABLE;
+#endif /* VDISCARD */
+
+      if (flow_control)
+       {
+#ifdef VSTART
+         tty.main.c_cc[VSTART] = '\021';
+#endif /* VSTART */
+#ifdef VSTOP
+         tty.main.c_cc[VSTOP] = '\023';
+#endif /* VSTOP */
+       }
+      else
+       {
+#ifdef VSTART
+         tty.main.c_cc[VSTART] = CDISABLE;
+#endif /* VSTART */
+#ifdef VSTOP
+         tty.main.c_cc[VSTOP] = CDISABLE;
+#endif /* VSTOP */
+       }
 #endif /* mips or HAVE_TCATTR */
+
+#ifdef SET_LINE_DISCIPLINE
+      /* Need to explicitly request TERMIODISC line discipline or
+         Ultrix's termios does not work correctly.  */
+      tty.main.c_line = SET_LINE_DISCIPLINE;
+#endif
 #ifdef AIX
 #ifndef IBMR2AIX
-      /* AIX enhanced edit loses NULs, so disable it */
+      /* AIX enhanced edit loses NULs, so disable it */
       tty.main.c_line = 0;
       tty.main.c_iflag &= ~ASCEDIT;
 #else
@@ -1011,6 +1473,15 @@ init_sys_modes ()
       tty.main.c_cc[VSUSP] = 255;
       tty.main.c_cc[VDSUSP] = 255;
 #endif /* IBMR2AIX */
+      if (flow_control)
+       {
+#ifdef VSTART
+         tty.main.c_cc[VSTART] = '\021';
+#endif /* VSTART */
+#ifdef VSTOP
+         tty.main.c_cc[VSTOP] = '\023';
+#endif /* VSTOP */
+       }
       /* Also, PTY overloads NUL and BREAK.
         don't ignore break, but don't signal either, so it looks like NUL.
         This really serves a purpose only if running in an XTERM window
@@ -1029,10 +1500,13 @@ init_sys_modes ()
        tty.main.tt_char &= ~TT$M_TTSYNC;
       tty.main.tt2_char |= TT2$M_PASTHRU | TT2$M_XON;
 #else /* not VMS (BSD, that is) */
+#ifndef DOS_NT
+      XSETINT (Vtty_erase_char, tty.main.sg_erase);
       tty.main.sg_flags &= ~(ECHO | CRMOD | XTABS);
       if (meta_key)
        tty.main.sg_flags |= ANYP;
       tty.main.sg_flags |= interrupt_input ? RAW : CBREAK;
+#endif /* not DOS_NT */
 #endif /* not VMS (BSD, that is) */
 #endif /* not HAVE_TERMIO */
 
@@ -1042,7 +1516,7 @@ init_sys_modes ()
         control for user coming over network on 4.2; in this case,
         only t_stopc and t_startc really matter.  */
 #ifndef HAVE_TERMIO
-#ifdef TIOCGETC
+#ifdef HAVE_TCHARS
       /* Note: if not using CBREAK mode, it makes no difference how we
         set this */
       tty.tchars = new_tchars;
@@ -1053,27 +1527,28 @@ init_sys_modes ()
          tty.tchars.t_stopc = '\023';
        }
 
-/* LPASS8 is new in 4.3, and makes cbreak mode provide all 8 bits.  */
-#ifndef LPASS8
-#define LPASS8 0
-#endif
-
-#ifdef BSD4_1
-#define LNOFLSH 0100000
-#endif
-
       tty.lmode = LDECCTQ | LLITOUT | LPASS8 | LNOFLSH | old_tty.lmode;
+#ifdef ultrix
+      /* Under Ultrix 4.2a, leaving this out doesn't seem to hurt
+        anything, and leaving it in breaks the meta key.  Go figure.  */
+      tty.lmode &= ~LLITOUT;
+#endif
       
 #ifdef BSD4_1
       lmode = tty.lmode;
 #endif
 
-#endif /* TIOCGETC */
+#endif /* HAVE_TCHARS */
 #endif /* not HAVE_TERMIO */
 
-#ifdef TIOCGLTC
+#ifdef HAVE_LTCHARS
       tty.ltchars = new_ltchars;
-#endif /* TIOCGLTC */
+#endif /* HAVE_LTCHARS */
+#ifdef MSDOS   /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
+      if (!term_initted)
+       internal_terminal_init ();
+      dos_ttraw ();
+#endif
 
       EMACS_SET_TTY (input_fd, &tty, 0);
 
@@ -1081,15 +1556,21 @@ init_sys_modes ()
         we have an unlocked terminal at the start. */
 
 #ifdef TCXONC
-      if (!flow_control) ioctl (0, TCXONC, 1);
+      if (!flow_control) ioctl (input_fd, TCXONC, 1);
 #endif
 #ifndef APOLLO
 #ifdef TIOCSTART
-      if (!flow_control) ioctl (0, TIOCSTART, 0);
+      if (!flow_control) ioctl (input_fd, TIOCSTART, 0);
 #endif
 #endif
 
-#ifdef AIX
+#if defined (HAVE_TERMIOS) || defined (HPUX9)
+#ifdef TCOON
+      if (!flow_control) tcflow (input_fd, TCOON);
+#endif
+#endif
+
+#ifdef AIXHFT
       hft_init ();
 #ifdef IBMR2AIX
       {
@@ -1101,7 +1582,7 @@ init_sys_modes ()
          write (1, "\033[20l", 5);
       }
 #endif
-#endif
+#endif /* AIXHFT */
 
 #ifdef VMS
 /*  Appears to do nothing when in PASTHRU mode.
@@ -1113,19 +1594,22 @@ init_sys_modes ()
     }
 
 #ifdef F_SETFL
-#ifdef F_GETOWN                /* F_SETFL does not imply existance of F_GETOWN */
-  if (interrupt_input)
+#ifndef F_SETOWN_BUG
+#ifdef F_GETOWN                /* F_SETFL does not imply existence of F_GETOWN */
+  if (interrupt_input
+      && ! read_socket_hook && EQ (Vwindow_system, Qnil))
     {
-      old_fcntl_owner = fcntl (0, F_GETOWN, 0);
-      fcntl (0, F_SETOWN, getpid ());
-      init_sigio ();
+      old_fcntl_owner = fcntl (input_fd, F_GETOWN, 0);
+      fcntl (input_fd, F_SETOWN, getpid ());
+      init_sigio (input_fd);
     }
 #endif /* F_GETOWN */
+#endif /* F_SETOWN_BUG */
 #endif /* F_SETFL */
 
 #ifdef BSD4_1
   if (interrupt_input)
-    init_sigio ();
+    init_sigio (input_fd);
 #endif
 
 #ifdef VMS  /* VMS sometimes has this symbol but lacks setvbuf.  */
@@ -1135,11 +1619,28 @@ init_sys_modes ()
   /* This symbol is defined on recent USG systems.
      Someone says without this call USG won't really buffer the file
      even with a call to setbuf. */
-  setvbuf (stdout, _sobuf, _IOFBF, sizeof _sobuf);
+  setvbuf (stdout, (char *) _sobuf, _IOFBF, sizeof _sobuf);
 #else
-  setbuf (stdout, _sobuf);
+  setbuf (stdout, (char *) _sobuf);
+#endif
+#ifdef HAVE_WINDOW_SYSTEM
+  /* Emacs' window system on MSDOG uses the `internal terminal' and therefore
+     needs the initialization code below.  */
+  if (EQ (Vwindow_system, Qnil)
+#ifndef WINDOWSNT
+      /* When running in tty mode on NT/Win95, we have a read_socket
+        hook, but still need the rest of the initialization code below.  */
+      && (! read_socket_hook)
 #endif
-  set_terminal_modes ();
+      )
+#endif
+    set_terminal_modes ();
+
+  if (!term_initted
+      && FRAMEP (Vterminal_frame)
+      && FRAME_TERMCAP_P (XFRAME (Vterminal_frame)))
+    init_frame_faces (XFRAME (Vterminal_frame));
+
   if (term_initted && no_redraw_on_reenter)
     {
       if (display_completed)
@@ -1148,10 +1649,8 @@ init_sys_modes ()
   else
     {
       frame_garbaged = 1;
-#ifdef MULTI_FRAME
       if (FRAMEP (Vterminal_frame))
        FRAME_GARBAGED_P (XFRAME (Vterminal_frame)) = 1;
-#endif
     }
 
   term_initted = 1;
@@ -1160,6 +1659,7 @@ init_sys_modes ()
 /* Return nonzero if safe to use tabs in output.
    At the time this is called, init_sys_modes has not been done yet.  */
    
+int
 tabs_safe_p ()
 {
   struct emacs_tty tty;
@@ -1167,11 +1667,12 @@ tabs_safe_p ()
   EMACS_GET_TTY (input_fd, &tty);
   return EMACS_TTY_TABS_OK (&tty);
 }
-
+\f
 /* Get terminal size from system.
-   Store number of lines into *heightp and width into *widthp.
-   If zero or a negative number is stored, the value is not valid.  */
+   Store number of lines into *HEIGHTP and width into *WIDTHP.
+   We store 0 if there's no valid information.  */
 
+void
 get_frame_size (widthp, heightp)
      int *widthp, *heightp;
 {
@@ -1213,21 +1714,65 @@ get_frame_size (widthp, heightp)
   *widthp = tty.scr_wid;
   *heightp = tty.scr_len;
 
+#else
+#ifdef MSDOS
+  *widthp = ScreenCols ();
+  *heightp = ScreenRows ();
 #else /* system doesn't know size */
-
   *widthp = 0;
   *heightp = 0;
+#endif
 
 #endif /* not VMS */
 #endif /* not SunOS-style */
 #endif /* not BSD-style */
 }
 
+/* Set the logical window size associated with descriptor FD
+   to HEIGHT and WIDTH.  This is used mainly with ptys.  */
+
+int
+set_window_size (fd, height, width)
+     int fd, height, width;
+{
+#ifdef TIOCSWINSZ
+
+  /* BSD-style.  */
+  struct winsize size;
+  size.ws_row = height;
+  size.ws_col = width;
+
+  if (ioctl (fd, TIOCSWINSZ, &size) == -1)
+    return 0; /* error */
+  else
+    return 1;
+
+#else
+#ifdef TIOCSSIZE
+
+  /* SunOS - style.  */
+  struct ttysize size;  
+  size.ts_lines = height;
+  size.ts_cols = width;
+
+  if (ioctl (fd, TIOCGSIZE, &size) == -1)
+    return 0;
+  else
+    return 1;
+#else
+  return -1;
+#endif /* not SunOS-style */
+#endif /* not BSD-style */
+}
+
 \f
 /* Prepare the terminal for exiting Emacs; move the cursor to the
    bottom of the frame, turn off interrupt-driven I/O, etc.  */
+void
 reset_sys_modes ()
 {
+  struct frame *sf;
+  
   if (noninteractive)
     {
       fflush (stdout);
@@ -1235,13 +1780,24 @@ reset_sys_modes ()
     }
   if (!term_initted)
     return;
-  if (read_socket_hook || !EQ (Vwindow_system, Qnil))
+#ifdef HAVE_WINDOW_SYSTEM
+  /* Emacs' window system on MSDOG uses the `internal terminal' and therefore
+     needs the clean-up code below.  */
+  if (!EQ (Vwindow_system, Qnil)
+#ifndef WINDOWSNT
+      /* When running in tty mode on NT/Win95, we have a read_socket
+        hook, but still need the rest of the clean-up code below.  */
+      || read_socket_hook
+#endif
+      )
     return;
-  cursor_to (FRAME_HEIGHT (selected_frame) - 1, 0);
-  clear_end_of_line (FRAME_WIDTH (selected_frame));
+#endif
+  sf = SELECTED_FRAME ();
+  cursor_to (FRAME_HEIGHT (sf) - 1, 0);
+  clear_end_of_line (FRAME_WIDTH (sf));
   /* clear_end_of_line may move the cursor */
-  cursor_to (FRAME_HEIGHT (selected_frame) - 1, 0);
-#ifdef IBMR2AIX
+  cursor_to (FRAME_HEIGHT (sf) - 1, 0);
+#if defined (IBMR2AIX) && defined (AIXHFT)
   {
     /* HFT devices normally use ^J as a LF/CR.  We forced it to 
        do the LF only.  Now, we need to reset it. */
@@ -1254,7 +1810,7 @@ reset_sys_modes ()
 
   reset_terminal_modes ();
   fflush (stdout);
-#ifdef BSD
+#ifdef BSD_SYSTEM
 #ifndef BSD4_1
   /* Avoid possible loss of output when changing terminal modes.  */
   fsync (fileno (stdout));
@@ -1262,31 +1818,53 @@ reset_sys_modes ()
 #endif
 
 #ifdef F_SETFL
-#ifdef F_SETOWN                /* F_SETFL does not imply existance of F_SETOWN */
+#ifndef F_SETOWN_BUG
+#ifdef F_SETOWN                /* F_SETFL does not imply existence of F_SETOWN */
   if (interrupt_input)
     {
       reset_sigio ();
-      fcntl (0, F_SETOWN, old_fcntl_owner);
+      fcntl (input_fd, F_SETOWN, old_fcntl_owner);
     }
 #endif /* F_SETOWN */
+#endif /* F_SETOWN_BUG */
+#ifdef O_NDELAY
+  fcntl (input_fd, F_SETFL, fcntl (input_fd, F_GETFL, 0) & ~O_NDELAY);
+#endif
 #endif /* F_SETFL */
 #ifdef BSD4_1
   if (interrupt_input)
     reset_sigio ();
 #endif /* BSD4_1 */
 
-  while (EMACS_SET_TTY (input_fd, &old_tty, 0) < 0 && errno == EINTR)
-    ;
+  if (old_tty_valid)
+    while (EMACS_SET_TTY (input_fd, &old_tty, 0) < 0 && errno == EINTR)
+      ;
 
-#ifdef AIX
+#ifdef MSDOS   /* Demacs 1.1.2 91/10/20 Manabu Higashida */
+  dos_ttcooked ();
+#endif
+
+#ifdef SET_LINE_DISCIPLINE
+  /* Ultrix's termios *ignores* any line discipline except TERMIODISC.
+     A different old line discipline is therefore not restored, yet.
+     Restore the old line discipline by hand.  */
+  ioctl (0, TIOCSETD, &old_tty.main.c_line);
+#endif
+
+#ifdef AIXHFT
   hft_reset ();
 #endif
+
+#ifdef BSD_PGRPS
+  widen_foreground_group ();
+#endif
 }
 \f
 #ifdef HAVE_PTYS
 
 /* Set up the proper status flags for use of a pty.  */
 
+void
 setup_pty (fd)
      int fd;
 {
@@ -1314,7 +1892,7 @@ setup_pty (fd)
      Since the latter lossage is more benign, we may as well
      lose that way.  -- cph */
 #ifdef FIONBIO
-#ifdef SYSV_PTYS
+#if defined(SYSV_PTYS) || defined(UNIX98_PTYS)
   {
     int on = 1;
     ioctl (fd, FIONBIO, &on);
@@ -1338,6 +1916,7 @@ setup_pty (fd)
    This is called each time Emacs is resumed, also, but does nothing
    because input_chain is no longer zero.  */
 
+void
 init_vms_input ()
 {
   int status;
@@ -1352,6 +1931,7 @@ init_vms_input ()
 
 /* Deassigning the input channel is done before exiting.  */
 
+void
 stop_vms_input ()
 {
   return SYS$DASSGN (input_fd);
@@ -1362,6 +1942,7 @@ short input_buffer;
 /* Request reading one character into the keyboard buffer.
    This is done as soon as the buffer becomes empty.  */
 
+void
 queue_kbd_input ()
 {
   int status;
@@ -1379,6 +1960,7 @@ int input_count;
 /* Ast routine that is called when keyboard input comes in
    in accord with the SYS$QIO above.  */
 
+void
 kbd_input_ast ()
 {
   register int c = -1;
@@ -1415,12 +1997,8 @@ kbd_input_ast ()
     {
       struct input_event e;
       e.kind = ascii_keystroke;
-      XSET (e.code, Lisp_Int, c);
-#ifdef MULTI_FRAME
-      XSET(e.frame_or_window, Lisp_Frame, selected_frame);
-#else
-      e.frame_or_window = Qnil;
-#endif
+      XSETINT (e.code, c);
+      e.frame_or_window = selected_frame;
       kbd_buffer_store_event (&e);
     }
   if (input_available_clear_time)
@@ -1430,6 +2008,7 @@ kbd_input_ast ()
 
 /* Wait until there is something in kbd_buffer.  */
 
+void
 wait_for_kbd_input ()
 {
   extern int have_process_input, process_exited;
@@ -1462,7 +2041,8 @@ wait_for_kbd_input ()
          if (dsp)
            {
              update_mode_lines++;
-             redisplay_preserve_echo_area ();
+             prepare_menu_bars ();
+             redisplay_preserve_echo_area (18);
            }
        }
     }
@@ -1475,6 +2055,7 @@ wait_for_kbd_input ()
    and therefore there is no I/O request queued when we return.
    SYS$SETAST is used to avoid a timing error.  */
 
+void
 end_kbd_input ()
 {
 #ifdef ASTDEBUG
@@ -1501,6 +2082,7 @@ end_kbd_input ()
 
 /* Wait for either input available or time interval expiry.  */
 
+void
 input_wait_timeout (timeval)
      int timeval;              /* Time to wait, in seconds */
 {
@@ -1547,7 +2129,9 @@ sys_sleep (timeval)
     SYS$WAITFR (timer_ef);       /* Wait for timer expiry only */
 }
 
-init_sigio ()
+void
+init_sigio (fd)
+     int fd;
 {
   request_sigio ();
 }
@@ -1557,11 +2141,13 @@ reset_sigio ()
   unrequest_sigio ();
 }
 
+void
 request_sigio ()
 {
   croak ("request sigio");
 }
 
+void
 unrequest_sigio ()
 {
   croak ("unrequest sigio");
@@ -1590,7 +2176,8 @@ unrequest_sigio ()
  *
  */
 
-#ifndef CANNOT_UNEXEC
+#if !(defined (__NetBSD__) && defined (__ELF__))
+#ifndef HAVE_TEXT_START
 char *
 start_of_text ()
 {
@@ -1606,7 +2193,8 @@ start_of_text ()
 #endif /* GOULD */
 #endif /* TEXT_START */
 }
-#endif /* not CANNOT_UNEXEC */
+#endif /* not HAVE_TEXT_START */
+#endif
 
 /*
  *     Return the address of the start of the data segment prior to
@@ -1629,7 +2217,7 @@ start_of_text ()
  *     at least on UniPlus, is temacs will have to be made unshared so
  *     that text and data are contiguous.  Then once loadup is complete,
  *     unexec will produce a shared executable where the data can be
- *     at the normal shared text boundry and the startofdata variable
+ *     at the normal shared text boundary and the startofdata variable
  *     will be patched by unexec to the correct value.
  *
  */
@@ -1649,7 +2237,7 @@ start_of_data ()
    */
   extern char **environ;
 
-  return((char *) &environ);
+  return ((char *) &environ);
 #else
   extern int data_start;
   return ((char *) &data_start);
@@ -1695,82 +2283,185 @@ end_of_data ()
 
 #endif /* not CANNOT_DUMP */
 \f
-/* Get_system_name returns as its value
a string for the Lisp function system-name to return. */
+/* init_system_name sets up the string for the Lisp function
  system-name to return. */
 
 #ifdef BSD4_1
 #include <whoami.h>
 #endif
 
-/* Can't have this within the function since `static' is #defined to 
-   nothing for some USG systems.  */
-#ifdef USG
-#ifdef HAVE_GETHOSTNAME
-static char get_system_name_name[256];
-#else /* not HAVE_GETHOSTNAME */
-static struct utsname get_system_name_name;
-#endif /* not HAVE_GETHOSTNAME */
-#endif /* USG */
+extern Lisp_Object Vsystem_name;
 
-char *
-get_system_name ()
+#ifndef BSD4_1
+#ifndef VMS
+#ifdef HAVE_SOCKETS
+#include <sys/socket.h>
+#include <netdb.h>
+#endif /* HAVE_SOCKETS */
+#endif /* not VMS */
+#endif /* not BSD4_1 */
+
+#ifdef TRY_AGAIN
+#ifndef HAVE_H_ERRNO
+extern int h_errno;
+#endif
+#endif /* TRY_AGAIN */
+
+void
+init_system_name ()
 {
-#ifdef USG
-#ifdef HAVE_GETHOSTNAME
-  gethostname (get_system_name_name, sizeof (get_system_name_name));
-  return get_system_name_name;
-#else /* not HAVE_GETHOSTNAME */
-  uname (&get_system_name_name);
-  return (get_system_name_name.nodename);
-#endif /* not HAVE_GETHOSTNAME */
-#else /* Not USG */
 #ifdef BSD4_1
-  return sysname;
-#else /* not USG, not 4.1 */
-  static char system_name_saved[32];
+  Vsystem_name = build_string (sysname);
+#else
 #ifdef VMS
-  char *sp;
+  char *sp, *end;
   if ((sp = egetenv ("SYS$NODE")) == 0)
-    sp = "vax-vms";
+    Vsystem_name = build_string ("vax-vms");
+  else if ((end = index (sp, ':')) == 0)
+    Vsystem_name = build_string (sp);
   else
+    Vsystem_name = make_string (sp, end - sp);
+#else
+#ifndef HAVE_GETHOSTNAME
+  struct utsname uts;
+  uname (&uts);
+  Vsystem_name = build_string (uts.nodename);
+#else /* HAVE_GETHOSTNAME */
+  unsigned int hostname_size = 256;
+  char *hostname = (char *) alloca (hostname_size);
+
+  /* Try to get the host name; if the buffer is too short, try
+     again.  Apparently, the only indication gethostname gives of
+     whether the buffer was large enough is the presence or absence
+     of a '\0' in the string.  Eech.  */
+  for (;;)
     {
-      char *end;
+      gethostname (hostname, hostname_size - 1);
+      hostname[hostname_size - 1] = '\0';
 
-      if ((end = index (sp, ':')) != 0)
-       *end = '\0';
+      /* Was the buffer large enough for the '\0'?  */
+      if (strlen (hostname) < hostname_size - 1)
+       break;
+
+      hostname_size <<= 1;
+      hostname = (char *) alloca (hostname_size);
     }
-  strcpy (system_name_saved, sp);
-#else /* not VMS */
-  gethostname (system_name_saved, sizeof (system_name_saved));
-#endif /* not VMS */
-  return system_name_saved;
-#endif /* not USG, not 4.1 */
-#endif /* not USG */
-}
+#ifdef HAVE_SOCKETS
+  /* Turn the hostname into the official, fully-qualified hostname.
+     Don't do this if we're going to dump; this can confuse system
+     libraries on some machines and make the dumped emacs core dump. */
+#ifndef CANNOT_DUMP
+  if (initialized)
+#endif /* not CANNOT_DUMP */
+    if (! index (hostname, '.'))
+      {
+       struct hostent *hp;
+       int count;
+       for (count = 0;; count++)
+         {
+#ifdef TRY_AGAIN
+           h_errno = 0;
+#endif
+           hp = gethostbyname (hostname);
+#ifdef TRY_AGAIN
+           if (! (hp == 0 && h_errno == TRY_AGAIN))
+#endif
+             break;
+           if (count >= 5)
+             break;
+           Fsleep_for (make_number (1), Qnil);
+         }
+       if (hp)
+         {
+           char *fqdn = (char *) hp->h_name;
+           char *p;
+
+           if (!index (fqdn, '.'))
+             {
+               /* We still don't have a fully qualified domain name.
+                  Try to find one in the list of alternate names */
+               char **alias = hp->h_aliases;
+               while (*alias && !index (*alias, '.'))
+                 alias++;
+               if (*alias)
+                 fqdn = *alias;
+             }
+           hostname = fqdn;
+#if 0
+           /* Convert the host name to lower case.  */
+           /* Using ctype.h here would introduce a possible locale
+              dependence that is probably wrong for hostnames.  */
+           p = hostname;
+           while (*p)
+             {
+               if (*p >= 'A' && *p <= 'Z')
+                 *p += 'a' - 'A';
+               p++;
+             }
+#endif
+         }
+      }
+#endif /* HAVE_SOCKETS */
+  /* We used to try using getdomainname here,
+     but NIIBE Yutaka <gniibe@etl.go.jp> says that
+     getdomainname gets the NIS/YP domain which often is not the same
+     as in Internet domain name.  */
+#if 0 /* Turned off because sysinfo is not really likely to return the
+        correct Internet domain.  */
+#if (HAVE_SYSINFO && defined (SI_SRPC_DOMAIN))
+  if (! index (hostname, '.'))
+    {
+      /* The hostname is not fully qualified.  Append the domain name.  */
 
-#ifdef VMS
-#ifndef HAVE_GETHOSTNAME
-void gethostname(buf, len)
-    char *buf;
-    int len;
-{
-    char *s;
-    s = getenv ("SYS$NODE");
-    if (s == NULL)
-        buf[0] = '\0';
-    else {
-        strncpy (buf, s, len - 2);
-        buf[len - 1] = '\0';
-    } /* else */
-} /* static void gethostname */
-#endif /* ! HAVE_GETHOSTNAME */
-#endif /* VMS */
+      int hostlen = strlen (hostname);
+      int domain_size = 256;
 
+      for (;;)
+       {
+         char *domain = (char *) alloca (domain_size + 1);
+         char *fqdn = (char *) alloca (hostlen + 1 + domain_size + 1);
+         int sys_domain_size = sysinfo (SI_SRPC_DOMAIN, domain, domain_size);
+         if (sys_domain_size <= 0)
+           break;
+         if (domain_size < sys_domain_size)
+           {
+             domain_size = sys_domain_size;
+             continue;
+           }
+         strcpy (fqdn, hostname);
+         if (domain[0] == '.')
+           strcpy (fqdn + hostlen, domain);
+         else if (domain[0] != 0)
+           {
+             fqdn[hostlen] = '.';
+             strcpy (fqdn + hostlen + 1, domain);
+           }
+         hostname = fqdn;
+         break;
+       }
+    }
+#endif /* HAVE_SYSINFO && defined (SI_SRPC_DOMAIN) */
+#endif /* 0 */
+  Vsystem_name = build_string (hostname);
+#endif /* HAVE_GETHOSTNAME */
+#endif /* VMS */
+#endif /* BSD4_1 */
+  {
+    unsigned char *p;
+    for (p = XSTRING (Vsystem_name)->data; *p; p++)
+      if (*p == ' ' || *p == '\t')
+       *p = '-';
+  }
+}
 \f
+#ifndef MSDOS
 #ifndef VMS
-#ifndef HAVE_SELECT
+#if !defined (HAVE_SELECT) || defined (BROKEN_SELECT_NON_X)
 
-#ifdef HAVE_X_WINDOWS
+#include "sysselect.h"
+#undef select
+
+#if defined (HAVE_X_WINDOWS) && !defined (HAVE_SELECT)
 /* Cause explanatory error message at compile time,
    since the select emulation is not good enough for X.  */
 int *x = &x_windows_lose_if_no_select_system_call;
@@ -1805,61 +2496,78 @@ select_alarm ()
     longjmp (read_alarm_throw, 1);
 }
 
+#ifndef WINDOWSNT
 /* Only rfds are checked.  */
 int
-select (nfds, rfds, wfds, efds, timeout)
+sys_select (nfds, rfds, wfds, efds, timeout)
      int nfds;
-     int *rfds, *wfds, *efds, *timeout;
+     SELECT_TYPE *rfds, *wfds, *efds;
+     EMACS_TIME *timeout;
 {
-  int ravail = 0, orfds = 0, old_alarm;
-  int timeoutval = timeout ? *timeout : 100000;
-  int *local_timeout = &timeoutval;
+  int ravail = 0;
+  SELECT_TYPE orfds;
+  int timeoutval;
+  int *local_timeout;
   extern int proc_buffered_char[];
 #ifndef subprocesses
   int process_tick = 0, update_tick = 0;
 #else
   extern int process_tick, update_tick;
 #endif
-  SIGTYPE (*old_trap) ();
   unsigned char buf;
 
+#if defined (HAVE_SELECT) && defined (HAVE_X_WINDOWS)
+  /* If we're using X, then the native select will work; we only need the
+     emulation for non-X usage.  */
+  if (!NILP (Vwindow_system))
+    return select (nfds, rfds, wfds, efds, timeout);
+#endif
+  timeoutval = timeout ? EMACS_SECS (*timeout) : 100000;
+  local_timeout = &timeoutval;
+  FD_ZERO (&orfds);
   if (rfds)
     {
       orfds = *rfds;
-      *rfds = 0;
+      FD_ZERO (rfds);
     }
   if (wfds)
-    *wfds = 0;
+    FD_ZERO (wfds);
   if (efds)
-    *efds = 0;
+    FD_ZERO (efds);
 
   /* If we are looking only for the terminal, with no timeout,
      just read it and wait -- that's more efficient.  */
-  if (orfds == 1 && *local_timeout == 100000 && process_tick == update_tick)
+  if (*local_timeout == 100000 && process_tick == update_tick
+      && FD_ISSET (0, &orfds))
     {
+      int fd;
+      for (fd = 1; fd < nfds; ++fd)
+       if (FD_ISSET (fd, &orfds))
+         goto hardway;
       if (! detect_input_pending ())
        read_input_waiting ();
-      *rfds = 1;
+      FD_SET (0, rfds);
       return 1;
     }
 
+ hardway:
   /* Once a second, till the timer expires, check all the flagged read
    * descriptors to see if any input is available.  If there is some then
    * set the corresponding bit in the return copy of rfds.
    */ 
   while (1)
     {
-      register int to_check, bit, fd;
+      register int to_check, fd;
 
       if (rfds)
        {
-         for (to_check = nfds, bit = 1, fd = 0; --to_check >= 0; bit <<= 1, fd++)
+         for (to_check = nfds, fd = 0; --to_check >= 0; fd++)
            {
-             if (orfds & bit)
+             if (FD_ISSET (fd, &orfds))
                {
                  int avail = 0, status = 0;
 
-                 if (bit == 1)
+                 if (fd == 0)
                    avail = detect_input_pending (); /* Special keyboard handler */
                  else
                    {
@@ -1880,7 +2588,7 @@ select (nfds, rfds, wfds, efds, timeout)
                    }
                  if (status >= 0 && avail > 0)
                    {
-                     (*rfds) |= bit;
+                     FD_SET (fd, rfds);
                      ravail++;
                    }
                }
@@ -1888,10 +2596,12 @@ select (nfds, rfds, wfds, efds, timeout)
        }
       if (*local_timeout == 0 || ravail != 0 || process_tick != update_tick)
        break;
-      old_alarm = alarm (0);
-      old_trap = signal (SIGALRM, select_alarm);
+
+      turn_on_atimers (0);
+      signal (SIGALRM, select_alarm);
       select_alarmed = 0;
       alarm (SELECT_PAUSE);
+      
       /* Wait for a SIGALRM (or maybe a SIGTINT) */
       while (select_alarmed == 0 && *local_timeout != 0
             && process_tick == update_tick)
@@ -1899,7 +2609,7 @@ select (nfds, rfds, wfds, efds, timeout)
          /* If we are interested in terminal input,
             wait by reading the terminal.
             That makes instant wakeup for terminal input at least.  */
-         if (orfds & 1)
+         if (FD_ISSET (0, &orfds))
            {
              read_input_waiting ();
              if (detect_input_pending ())
@@ -1909,98 +2619,103 @@ select (nfds, rfds, wfds, efds, timeout)
            pause ();
        }
       (*local_timeout) -= SELECT_PAUSE;
-      /* Reset the old alarm if there was one */
-      alarm (0);
-      signal (SIGALRM, old_trap);
-      if (old_alarm != 0)
-       {
-         /* Reset or forge an interrupt for the original handler. */
-         old_alarm -= SELECT_PAUSE;
-         if (old_alarm <= 0)
-           kill (getpid (), SIGALRM); /* Fake an alarm with the orig' handler */
-         else
-           alarm (old_alarm);
-       }
+      
+      /* Reset the old alarm if there was one.  */
+      turn_on_atimers (1);
+      
       if (*local_timeout == 0)  /* Stop on timer being cleared */
        break;
     }
   return ravail;
 }
+#endif /* not WINDOWSNT */
 
 /* Read keyboard input into the standard buffer,
    waiting for at least one character.  */
 
-/* Make all keyboard buffers much bigger when using X windows.  */
-#ifdef HAVE_X_WINDOWS
+/* Make all keyboard buffers much bigger when using a window system.  */
+#ifdef HAVE_WINDOW_SYSTEM
 #define BUFFER_SIZE_FACTOR 16
 #else
 #define BUFFER_SIZE_FACTOR 1
 #endif
 
+void
 read_input_waiting ()
 {
-  char buf[256 * BUFFER_SIZE_FACTOR];
   struct input_event e;
   int nread, i;
   extern int quit_char;
 
   if (read_socket_hook)
     {
+      struct input_event buf[256];
+
       read_alarm_should_throw = 0;
       if (! setjmp (read_alarm_throw))
-       nread = (*read_socket_hook) (0, buf, 256 * BUFFER_SIZE_FACTOR, 1, 0);
+       nread = (*read_socket_hook) (0, buf, 256, 1);
       else
        nread = -1;
+
+      /* Scan the chars for C-g and store them in kbd_buffer.  */
+      for (i = 0; i < nread; i++)
+       {
+         kbd_buffer_store_event (&buf[i]);
+         /* Don't look at input that follows a C-g too closely.
+            This reduces lossage due to autorepeat on C-g.  */
+         if (buf[i].kind == ascii_keystroke
+             && buf[i].code == quit_char)
+           break;
+       }
     }
   else
-    nread = read (fileno (stdin), buf, 1);
-
-  /* Scan the chars for C-g and store them in kbd_buffer.  */
-  e.kind = ascii_keystroke;
-  e.frame_or_window = selected_frame;
-  e.modifiers = 0;
-  for (i = 0; i < nread; i++)
     {
-      XSET (e.code, Lisp_Int, buf[i]);
-      kbd_buffer_store_event (&e);
-      /* Don't look at input that follows a C-g too closely.
-        This reduces lossage due to autorepeat on C-g.  */
-      if (buf[i] == quit_char)
-       break;
+      char buf[3];
+      nread = read (fileno (stdin), buf, 1);
+
+      /* Scan the chars for C-g and store them in kbd_buffer.  */
+      e.kind = ascii_keystroke;
+      e.frame_or_window = selected_frame;
+      e.modifiers = 0;
+      for (i = 0; i < nread; i++)
+       {
+         /* Convert chars > 0177 to meta events if desired.
+            We do this under the same conditions that read_avail_input does.  */
+         if (read_socket_hook == 0)
+           {
+             /* If the user says she has a meta key, then believe her. */
+             if (meta_key == 1 && (buf[i] & 0x80))
+               e.modifiers = meta_modifier;
+             if (meta_key != 2)
+               buf[i] &= ~0x80;
+           }
+
+         XSETINT (e.code, buf[i]);
+         kbd_buffer_store_event (&e);
+         /* Don't look at input that follows a C-g too closely.
+            This reduces lossage due to autorepeat on C-g.  */
+         if (buf[i] == quit_char)
+           break;
+       }
     }
 }
 
 #endif /* not HAVE_SELECT */
 #endif /* not VMS */
+#endif /* not MSDOS */
 \f
 #ifdef BSD4_1
-/*
- * Partially emulate 4.2 open call.
- * open is defined as this in 4.1.
- *
- * - added by Michael Bloom @ Citicorp/TTI
- *
- */
-
-int
-sys_open (path, oflag, mode)
-     char *path;
-     int oflag, mode;
-{
-  if (oflag & O_CREAT) 
-    return creat (path, mode);
-  else
-    return open (path, oflag);
-}
-
-init_sigio ()
+void
+init_sigio (fd)
+     int fd;
 {
   if (noninteractive)
     return;
   lmode = LINTRUP | lmode;
-  ioctl (0, TIOCLSET, &lmode);
+  ioctl (fd, TIOCLSET, &lmode);
 }
 
+void
 reset_sigio ()
 {
   if (noninteractive)
@@ -2009,6 +2724,7 @@ reset_sigio ()
   ioctl (0, TIOCLSET, &lmode);
 }
 
+void
 request_sigio ()
 {
   sigrelse (SIGTINT);
@@ -2016,6 +2732,7 @@ request_sigio ()
   interrupts_deferred = 0;
 }
 
+void
 unrequest_sigio ()
 {
   sighold (SIGTINT);
@@ -2028,6 +2745,7 @@ unrequest_sigio ()
 
 int sigheld; /* Mask of held signals */
 
+void
 sigholdx (signum)
      int signum;
 {
@@ -2035,12 +2753,14 @@ sigholdx (signum)
   sighold (signum);
 }
 
+void
 sigisheld (signum)
      int signum;
 {
   sigheld |= sigbit (signum);
 }
 
+void
 sigunhold (signum)
      int signum;
 {
@@ -2048,6 +2768,7 @@ sigunhold (signum)
   sigrelse (signum);
 }
 
+void
 sigfree ()    /* Free all held signals */
 {
   int i;
@@ -2057,6 +2778,7 @@ sigfree ()    /* Free all held signals */
   sigheld = 0;
 }
 
+int
 sigbit (i)
 {
   return 1 << (i - 1);
@@ -2069,29 +2791,24 @@ sigbit (i)
 
 #ifdef POSIX_SIGNALS
 
-sigset_t old_mask, empty_mask, full_mask, temp_mask;
-static struct sigaction new_action, old_action;
-
-init_signals ()
-{
-  sigemptyset (&empty_mask);
-  sigfillset (&full_mask);
-}
+sigset_t empty_mask, full_mask;
 
 signal_handler_t
 sys_signal (int signal_number, signal_handler_t action)
 {
-#ifdef DGUX
-  /* This gets us restartable system calls for efficiency.
-     The "else" code will works as well. */
-  return (berk_signal (signal_number, action));
-#else
+  struct sigaction new_action, old_action;
   sigemptyset (&new_action.sa_mask);
   new_action.sa_handler = action;
-  new_action.sa_flags = NULL;
+#ifdef SA_RESTART
+  /* Emacs mostly works better with restartable system services. If this
+   * flag exists, we probably want to turn it on here.
+   */
+  new_action.sa_flags = SA_RESTART;
+#else
+  new_action.sa_flags = 0;
+#endif
   sigaction (signal_number, &new_action, &old_action);
   return (old_action.sa_handler);
-#endif /* DGUX */
 }
 
 #ifndef __GNUC__
@@ -2107,14 +2824,6 @@ sys_sigmask (int sig)
 }
 #endif
 
-int
-sys_sigpause (sigset_t new_mask)
-{
-  /* pause emulating berk sigpause... */
-  sigsuspend (&new_mask);
-  return (EINTR);
-}
-
 /* I'd like to have these guys return pointers to the mask storage in here,
    but there'd be trouble if the code was saving multiple masks.  I'll be
    safe and pass the structure.  It normally won't be more than 2 bytes
@@ -2146,113 +2855,259 @@ sys_sigsetmask (sigset_t new_mask)
 
 #endif /* POSIX_SIGNALS */
 \f
-#ifndef BSTRING
+#if !defined HAVE_STRSIGNAL && !defined SYS_SIGLIST_DECLARED
+static char *my_sys_siglist[NSIG];
+# ifdef sys_siglist
+#  undef sys_siglist
+# endif
+# define sys_siglist my_sys_siglist
+#endif
 
 void
-bzero (b, length)
-     register char *b;
-     register int length;
+init_signals ()
 {
-#ifdef VMS
-  short zero = 0;
-  long max_str = 65535;
+#ifdef POSIX_SIGNALS
+  sigemptyset (&empty_mask);
+  sigfillset (&full_mask);
+#endif
 
-  while (length > max_str) {
-    (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
-    length -= max_str;
-    b += max_str;
-  }
-  max_str = length;
-  (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
-#else
-  while (length-- > 0)
-    *b++ = 0;
-#endif /* not VMS */
+#if !defined HAVE_STRSIGNAL && !defined SYS_SIGLIST_DECLARED
+  if (! initialized)
+    {
+# ifdef SIGABRT
+      sys_siglist[SIGABRT] = "Aborted";
+# endif
+# ifdef SIGAIO
+      sys_siglist[SIGAIO] = "LAN I/O interrupt";
+# endif
+# ifdef SIGALRM
+      sys_siglist[SIGALRM] = "Alarm clock";
+# endif
+# ifdef SIGBUS
+      sys_siglist[SIGBUS] = "Bus error";
+# endif
+# ifdef SIGCLD
+      sys_siglist[SIGCLD] = "Child status changed";
+# endif
+# ifdef SIGCHLD
+      sys_siglist[SIGCHLD] = "Child status changed";
+# endif
+# ifdef SIGCONT
+      sys_siglist[SIGCONT] = "Continued";
+# endif
+# ifdef SIGDANGER
+      sys_siglist[SIGDANGER] = "Swap space dangerously low";
+# endif
+# ifdef SIGDGNOTIFY
+      sys_siglist[SIGDGNOTIFY] = "Notification message in queue";
+# endif
+# ifdef SIGEMT
+      sys_siglist[SIGEMT] = "Emulation trap";
+# endif
+# ifdef SIGFPE
+      sys_siglist[SIGFPE] = "Arithmetic exception";
+# endif
+# ifdef SIGFREEZE
+      sys_siglist[SIGFREEZE] = "SIGFREEZE";
+# endif
+# ifdef SIGGRANT
+      sys_siglist[SIGGRANT] = "Monitor mode granted";
+# endif
+# ifdef SIGHUP
+      sys_siglist[SIGHUP] = "Hangup";
+# endif
+# ifdef SIGILL
+      sys_siglist[SIGILL] = "Illegal instruction";
+# endif
+# ifdef SIGINT
+      sys_siglist[SIGINT] = "Interrupt";
+# endif
+# ifdef SIGIO
+      sys_siglist[SIGIO] = "I/O possible";
+# endif
+# ifdef SIGIOINT
+      sys_siglist[SIGIOINT] = "I/O intervention required";
+# endif
+# ifdef SIGIOT
+      sys_siglist[SIGIOT] = "IOT trap";
+# endif
+# ifdef SIGKILL
+      sys_siglist[SIGKILL] = "Killed";
+# endif
+# ifdef SIGLOST
+      sys_siglist[SIGLOST] = "Resource lost";
+# endif
+# ifdef SIGLWP
+      sys_siglist[SIGLWP] = "SIGLWP";
+# endif
+# ifdef SIGMSG
+      sys_siglist[SIGMSG] = "Monitor mode data available";
+# endif
+# ifdef SIGPHONE
+      sys_siglist[SIGWIND] = "SIGPHONE";
+# endif
+# ifdef SIGPIPE
+      sys_siglist[SIGPIPE] = "Broken pipe";
+# endif
+# ifdef SIGPOLL
+      sys_siglist[SIGPOLL] = "Pollable event occurred";
+# endif
+# ifdef SIGPROF
+      sys_siglist[SIGPROF] = "Profiling timer expired";
+# endif
+# ifdef SIGPTY
+      sys_siglist[SIGPTY] = "PTY I/O interrupt";
+# endif
+# ifdef SIGPWR
+      sys_siglist[SIGPWR] = "Power-fail restart";
+# endif
+# ifdef SIGQUIT
+      sys_siglist[SIGQUIT] = "Quit";
+# endif
+# ifdef SIGRETRACT
+      sys_siglist[SIGRETRACT] = "Need to relinguish monitor mode";
+# endif
+# ifdef SIGSAK
+      sys_siglist[SIGSAK] = "Secure attention";
+# endif
+# ifdef SIGSEGV
+      sys_siglist[SIGSEGV] = "Segmentation violation";
+# endif
+# ifdef SIGSOUND
+      sys_siglist[SIGSOUND] = "Sound completed";
+# endif
+# ifdef SIGSTOP
+      sys_siglist[SIGSTOP] = "Stopped (signal)";
+# endif
+# ifdef SIGSTP
+      sys_siglist[SIGSTP] = "Stopped (user)";
+# endif
+# ifdef SIGSYS
+      sys_siglist[SIGSYS] = "Bad argument to system call";
+# endif
+# ifdef SIGTERM
+      sys_siglist[SIGTERM] = "Terminated";
+# endif
+# ifdef SIGTHAW
+      sys_siglist[SIGTHAW] = "SIGTHAW";
+# endif
+# ifdef SIGTRAP
+      sys_siglist[SIGTRAP] = "Trace/breakpoint trap";
+# endif
+# ifdef SIGTSTP
+      sys_siglist[SIGTSTP] = "Stopped (user)";
+# endif
+# ifdef SIGTTIN
+      sys_siglist[SIGTTIN] = "Stopped (tty input)";
+# endif
+# ifdef SIGTTOU
+      sys_siglist[SIGTTOU] = "Stopped (tty output)";
+# endif
+# ifdef SIGURG
+      sys_siglist[SIGURG] = "Urgent I/O condition";
+# endif
+# ifdef SIGUSR1
+      sys_siglist[SIGUSR1] = "User defined signal 1";
+# endif
+# ifdef SIGUSR2
+      sys_siglist[SIGUSR2] = "User defined signal 2";
+# endif
+# ifdef SIGVTALRM
+      sys_siglist[SIGVTALRM] = "Virtual timer expired";
+# endif
+# ifdef SIGWAITING
+      sys_siglist[SIGWAITING] = "Process's LWPs are blocked";
+# endif
+# ifdef SIGWINCH
+      sys_siglist[SIGWINCH] = "Window size changed";
+# endif
+# ifdef SIGWIND
+      sys_siglist[SIGWIND] = "SIGWIND";
+# endif
+# ifdef SIGXCPU
+      sys_siglist[SIGXCPU] = "CPU time limit exceeded";
+# endif
+# ifdef SIGXFSZ
+      sys_siglist[SIGXFSZ] = "File size limit exceeded";
+# endif
+    }
+#endif /* !defined HAVE_STRSIGNAL && !defined SYS_SIGLIST_DECLARED */
 }
+\f
+#ifndef HAVE_RANDOM
+#ifdef random
+#define HAVE_RANDOM
+#endif
+#endif
 
-/* Saying `void' requires a declaration, above, where bcopy is used
-   and that declaration causes pain for systems where bcopy is a macro.  */
-bcopy (b1, b2, length)
-     register char *b1;
-     register char *b2;
-     register int length;
-{
-#ifdef VMS
-  long max_str = 65535;
-
-  while (length > max_str) {
-    (void) LIB$MOVC3 (&max_str, b1, b2);
-    length -= max_str;
-    b1 += max_str;
-    b2 += max_str;
-  }
-  max_str = length;
-  (void) LIB$MOVC3 (&length, b1, b2);
-#else
-  while (length-- > 0)
-    *b2++ = *b1++;
-#endif /* not VMS */
-}
+/* Figure out how many bits the system's random number generator uses.
+   `random' and `lrand48' are assumed to return 31 usable bits.
+   BSD `rand' returns a 31 bit value but the low order bits are unusable;
+   so we'll shift it and treat it like the 15-bit USG `rand'.  */
+
+#ifndef RAND_BITS
+# ifdef HAVE_RANDOM
+#  define RAND_BITS 31
+# else /* !HAVE_RANDOM */
+#  ifdef HAVE_LRAND48
+#   define RAND_BITS 31
+#   define random lrand48
+#  else /* !HAVE_LRAND48 */
+#   define RAND_BITS 15
+#   if RAND_MAX == 32767
+#    define random rand
+#   else /* RAND_MAX != 32767 */
+#    if RAND_MAX == 2147483647
+#     define random() (rand () >> 16)
+#    else /* RAND_MAX != 2147483647 */
+#     ifdef USG
+#      define random rand
+#     else
+#      define random() (rand () >> 16)
+#     endif /* !USG */
+#    endif /* RAND_MAX != 2147483647 */
+#   endif /* RAND_MAX != 32767 */
+#  endif /* !HAVE_LRAND48 */
+# endif /* !HAVE_RANDOM */
+#endif /* !RAND_BITS */
 
-int
-bcmp (b1, b2, length)  /* This could be a macro! */
-     register char *b1;
-     register char *b2;
-     register int length;
+void
+seed_random (arg)
+     long arg;
 {
-#ifdef VMS
-  struct dsc$descriptor_s src1 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b1};
-  struct dsc$descriptor_s src2 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b2};
-
-  return STR$COMPARE (&src1, &src2);
+#ifdef HAVE_RANDOM
+  srandom ((unsigned int)arg);
 #else
-  while (length-- > 0)
-    if (*b1++ != *b2++)
-      return 1;
-
-  return 0;
-#endif /* not VMS */
+# ifdef HAVE_LRAND48
+  srand48 (arg);
+# else
+  srand ((unsigned int)arg);
+# endif
+#endif
 }
-#endif /* not BSTRING */
-\f
-#ifndef HAVE_RANDOM
-#ifdef USG
+
 /*
- *     The BSD random returns numbers in the range of
- *     0 to 2e31 - 1.  The USG rand returns numbers in the
- *     range of 0 to 2e15 - 1.  This is probably not significant
- *     in this usage.
+ * Build a full Emacs-sized word out of whatever we've got.
+ * This suffices even for a 64-bit architecture with a 15-bit rand.
  */
-  
 long
-random ()
-{
-  /* Arrange to return a range centered on zero.  */
-  return (rand () << 15) + rand () - (1 << 29);
-}
-
-srandom (arg)
-     int arg;
-{
-  srand (arg);
-}
-
-#endif /* USG */
-
-#ifdef BSD4_1
-long random ()
-{
-  /* Arrange to return a range centered on zero.  */
-  return (rand () << 15) + rand () - (1 << 29);
+get_random ()
+{
+  long val = random ();
+#if VALBITS > RAND_BITS
+  val = (val << RAND_BITS) ^ random ();
+#if VALBITS > 2*RAND_BITS
+  val = (val << RAND_BITS) ^ random ();
+#if VALBITS > 3*RAND_BITS
+  val = (val << RAND_BITS) ^ random ();
+#if VALBITS > 4*RAND_BITS
+  val = (val << RAND_BITS) ^ random ();
+#endif /* need at least 5 */
+#endif /* need at least 4 */
+#endif /* need at least 3 */
+#endif /* need at least 2 */
+  return val & ((1L << VALBITS) - 1);
 }
-
-srandom (arg)
-     int arg;
-{
-  srand (arg);
-}
-#endif /* BSD4_1 */
-#endif
 \f
 #ifdef WRONG_NAME_INSQUE
 
@@ -2320,8 +3175,8 @@ sys_abort ()
 \f
 #ifdef VMS
 #ifdef LINK_CRTL_SHARE
-#ifdef SHAREABLE_LIB_BUG
-/* Variables declared noshare and initialized in shareable libraries
+#ifdef SHARABLE_LIB_BUG
+/* Variables declared noshare and initialized in sharable libraries
    cannot be shared.  The VMS linker incorrectly forces you to use a private
    version which is uninitialized... If not for this "feature", we
    could use the C library definition of sys_nerr and sys_errlist. */
@@ -2366,45 +3221,65 @@ char *sys_errlist[] =
     "I/O stream empty",
     "vax/vms specific error code nontranslatable error"
   };
-#endif /* SHAREABLE_LIB_BUG */
+#endif /* SHARABLE_LIB_BUG */
 #endif /* LINK_CRTL_SHARE */
 #endif /* VMS */
-\f
-#ifdef INTERRUPTIBLE_OPEN
 
+#ifndef HAVE_STRERROR
+#ifndef WINDOWSNT
+char *
+strerror (errnum)
+     int errnum;
+{
+  extern char *sys_errlist[];
+  extern int sys_nerr;
+
+  if (errnum >= 0 && errnum < sys_nerr)
+    return sys_errlist[errnum];
+  return (char *) "Unknown error";
+}
+#endif /* not WINDOWSNT */
+#endif /* ! HAVE_STRERROR */
+\f
 int
-/* VARARGS 2 */
-sys_open (path, oflag, mode)
+emacs_open (path, oflag, mode)
      char *path;
      int oflag, mode;
 {
   register int rtnval;
+
+#ifdef BSD4_1
+  if (oflag & O_CREAT) 
+    return creat (path, mode);
+#endif
   
   while ((rtnval = open (path, oflag, mode)) == -1
         && (errno == EINTR));
   return (rtnval);
 }
 
-#endif /* INTERRUPTIBLE_OPEN */
-
-#ifdef INTERRUPTIBLE_CLOSE
-
-sys_close (fd)
+int
+emacs_close (fd)
      int fd;
 {
+  int did_retry = 0;
   register int rtnval;
 
   while ((rtnval = close (fd)) == -1
-        && (errno == EINTR));
-  return rtnval;
-}
+        && (errno == EINTR))
+    did_retry = 1;
 
-#endif /* INTERRUPTIBLE_CLOSE */
+  /* If close is interrupted SunOS 4.1 may or may not have closed the
+     file descriptor.  If it did the second close will fail with
+     errno = EBADF.  That means we have succeeded.  */
+  if (rtnval == -1 && did_retry && errno == EBADF)
+    return 0;
 
-#ifdef INTERRUPTIBLE_IO
+  return rtnval;
+}
 
 int
-sys_read (fildes, buf, nbyte)
+emacs_read (fildes, buf, nbyte)
      int fildes;
      char *buf;
      unsigned int nbyte;
@@ -2417,19 +3292,33 @@ sys_read (fildes, buf, nbyte)
 }
 
 int
-sys_write (fildes, buf, nbyte)
+emacs_write (fildes, buf, nbyte)
      int fildes;
      char *buf;
      unsigned int nbyte;
 {
-  register int rtnval;
+  register int rtnval, bytes_written;
 
-  while ((rtnval = write (fildes, buf, nbyte)) == -1
-        && (errno == EINTR));
-  return (rtnval);
-}
+  bytes_written = 0;
+
+  while (nbyte > 0)
+    {
+      rtnval = write (fildes, buf, nbyte);
+
+      if (rtnval == -1)
+       {
+         if (errno == EINTR)
+           continue;
+         else
+           return (bytes_written ? bytes_written : -1);
+       }
 
-#endif /* INTERRUPTIBLE_IO */
+      buf += rtnval;
+      nbyte -= rtnval;
+      bytes_written += rtnval;
+    }
+  return (bytes_written);
+}
 \f
 #ifdef USG
 /*
@@ -2442,74 +3331,11 @@ sys_write (fildes, buf, nbyte)
  *     to names for our own functions in sysdep.c that do the system call
  *     with retries.  Actually, for portability reasons, it is good
  *     programming practice, as this example shows, to limit all actual
- *     system calls to a single occurance in the source.  Sure, this
+ *     system calls to a single occurrence in the source.  Sure, this
  *     adds an extra level of function call overhead but it is almost
  *     always negligible.   Fred Fish, Unisoft Systems Inc.
  */
 
-#ifndef HAVE_SYS_SIGLIST
-char *sys_siglist[NSIG + 1] =
-{
-#ifdef AIX
-/* AIX has changed the signals a bit */
-  "bogus signal",                      /* 0 */
-  "hangup",                            /* 1  SIGHUP */
-  "interrupt",                         /* 2  SIGINT */
-  "quit",                              /* 3  SIGQUIT */
-  "illegal instruction",               /* 4  SIGILL */
-  "trace trap",                                /* 5  SIGTRAP */
-  "IOT instruction",                   /* 6  SIGIOT */
-  "crash likely",                      /* 7  SIGDANGER */
-  "floating point exception",          /* 8  SIGFPE */
-  "kill",                              /* 9  SIGKILL */
-  "bus error",                         /* 10 SIGBUS */
-  "segmentation violation",            /* 11 SIGSEGV */
-  "bad argument to system call",       /* 12 SIGSYS */
-  "write on a pipe with no one to read it", /* 13 SIGPIPE */
-  "alarm clock",                       /* 14 SIGALRM */
-  "software termination signum",       /* 15 SIGTERM */
-  "user defined signal 1",             /* 16 SIGUSR1 */
-  "user defined signal 2",             /* 17 SIGUSR2 */
-  "death of a child",                  /* 18 SIGCLD */
-  "power-fail restart",                        /* 19 SIGPWR */
-  "bogus signal",                      /* 20 */
-  "bogus signal",                      /* 21 */
-  "bogus signal",                      /* 22 */
-  "bogus signal",                      /* 23 */
-  "bogus signal",                      /* 24 */
-  "LAN I/O interrupt",                 /* 25 SIGAIO */
-  "PTY I/O interrupt",                 /* 26 SIGPTY */
-  "I/O intervention required",         /* 27 SIGIOINT */
-  "HFT grant",                         /* 28 SIGGRANT */
-  "HFT retract",                       /* 29 SIGRETRACT */
-  "HFT sound done",                    /* 30 SIGSOUND */
-  "HFT input ready",                   /* 31 SIGMSG */
-#else /* not AIX */
-  "bogus signal",                      /* 0 */
-  "hangup",                            /* 1  SIGHUP */
-  "interrupt",                         /* 2  SIGINT */
-  "quit",                              /* 3  SIGQUIT */
-  "illegal instruction",               /* 4  SIGILL */
-  "trace trap",                                /* 5  SIGTRAP */
-  "IOT instruction",                   /* 6  SIGIOT */
-  "EMT instruction",                   /* 7  SIGEMT */
-  "floating point exception",          /* 8  SIGFPE */
-  "kill",                              /* 9  SIGKILL */
-  "bus error",                         /* 10 SIGBUS */
-  "segmentation violation",            /* 11 SIGSEGV */
-  "bad argument to system call",       /* 12 SIGSYS */
-  "write on a pipe with no one to read it", /* 13 SIGPIPE */
-  "alarm clock",                       /* 14 SIGALRM */
-  "software termination signum",       /* 15 SIGTERM */
-  "user defined signal 1",             /* 16 SIGUSR1 */
-  "user defined signal 2",             /* 17 SIGUSR2 */
-  "death of a child",                  /* 18 SIGCLD */
-  "power-fail restart",                        /* 19 SIGPWR */
-#endif /* not AIX */
-  0
-  };
-#endif HAVE_SYS_SIGLIST
-
 /*
  *     Warning, this function may not duplicate 4.2 action properly
  *     under error conditions.
@@ -2531,6 +3357,11 @@ getwd (pathname)
 
   BLOCK_INPUT;                 /* getcwd uses malloc */
   spath = npath = getcwd ((char *) 0, MAXPATHLEN);
+  if (spath == 0)
+    {
+      UNBLOCK_INPUT;
+      return spath;
+    }
   /* On Altos 3068, getcwd can return @hostname/dir, so discard
      up to first slash.  Should be harmless on other systems.  */
   while (*npath && *npath != '/')
@@ -2552,8 +3383,8 @@ getwd (pathname)
 #ifndef HAVE_RENAME
 
 rename (from, to)
-     char *from;
-     char *to;
+     const char *from;
+     const char *to;
 {
   if (access (from, 0) == 0)
     {
@@ -2567,50 +3398,6 @@ rename (from, to)
 
 #endif
 
-#ifndef HAVE_VFORK
-
-/*
- *     Substitute fork for vfork on USG flavors.
- */
-
-vfork ()
-{
-  return (fork ());
-}
-
-#endif /* not HAVE_VFORK */
-
-#ifdef MISSING_UTIMES
-
-/* HPUX (among others) sets HAVE_TIMEVAL but does not implement utimes.  */
-
-utimes ()
-{
-}
-#endif
-
-#ifdef IRIS_UTIME
-
-/* The IRIS (3.5) has timevals, but uses sys V utime, and doesn't have the
-   utimbuf structure defined anywhere but in the man page. */
-
-struct utimbuf
- {
-   long actime;
-   long modtime;
- };
-
-utimes (name, tvp)
-     char *name;
-     struct timeval tvp[];
-{
-  struct utimbuf utb;
-  utb.actime  = tvp[0].tv_sec;
-  utb.modtime = tvp[1].tv_sec;
-  utime (name, &utb);
-}
-#endif /* IRIS_UTIME */
-
 
 #ifdef HPUX
 #ifndef HAVE_PERROR
@@ -2639,12 +3426,10 @@ dup2 (oldd, newd)
 {
   register int fd, ret;
   
-  sys_close (newd);
+  emacs_close (newd);
 
 #ifdef F_DUPFD
-  fd = fcntl (oldd, F_DUPFD, newd);
-  if (fd != newd)
-    error ("can't dup2 (%i,%i) : %s", oldd, newd, sys_errlist[errno]);
+  return fcntl (oldd, F_DUPFD, newd);
 #else
   fd = dup (old);
   if (fd == -1)
@@ -2652,7 +3437,7 @@ dup2 (oldd, newd)
   if (fd == new)
     return new;
   ret = dup2 (old,new);
-  sys_close (fd);
+  emacs_close (fd);
   return ret;
 #endif
 }
@@ -2671,6 +3456,7 @@ dup2 (oldd, newd)
 #ifdef HAVE_TIMEVAL
  
 /* ARGSUSED */
+int
 gettimeofday (tp, tzp)
      struct timeval *tp;
      struct timezone *tzp;
@@ -2679,7 +3465,9 @@ gettimeofday (tp, tzp)
 
   tp->tv_sec = time ((long *)0);    
   tp->tv_usec = 0;
-  tzp->tz_minuteswest = -1;
+  if (tzp != 0)
+    tzp->tz_minuteswest = -1;
+  return 0;
 }
  
 #endif
@@ -2691,6 +3479,7 @@ gettimeofday (tp, tzp)
  *     This function will go away as soon as all the stubs fixed. (fnf)
  */
 
+void
 croak (badfunc)
      char *badfunc;
 {
@@ -2701,96 +3490,33 @@ croak (badfunc)
 
 #endif /* USG */
 \f
-#ifdef DGUX
-
-char *sys_siglist[NSIG + 1] =
-{
-  "null signal",                        /*  0 SIGNULL   */
-  "hangup",                             /*  1 SIGHUP    */
-  "interrupt",                          /*  2 SIGINT    */
-  "quit",                               /*  3 SIGQUIT   */
-  "illegal instruction",                /*  4 SIGILL    */
-  "trace trap",                                 /*  5 SIGTRAP   */
-  "abort termination",                  /*  6 SIGABRT   */
-  "SIGEMT",                             /*  7 SIGEMT    */
-  "floating point exception",           /*  8 SIGFPE    */
-  "kill",                               /*  9 SIGKILL   */
-  "bus error",                          /* 10 SIGBUS    */
-  "segmentation violation",             /* 11 SIGSEGV   */
-  "bad argument to system call",        /* 12 SIGSYS    */
-  "write on a pipe with no reader",     /* 13 SIGPIPE   */
-  "alarm clock",                        /* 14 SIGALRM   */
-  "software termination signal",        /* 15 SIGTERM   */
-  "user defined signal 1",              /* 16 SIGUSR1   */
-  "user defined signal 2",              /* 17 SIGUSR2   */
-  "child stopped or terminated",        /* 18 SIGCLD    */
-  "power-fail restart",                         /* 19 SIGPWR    */
-  "window size changed",                /* 20 SIGWINCH  */
-  "undefined",                          /* 21           */
-  "pollable event occured",             /* 22 SIGPOLL   */
-  "sendable stop signal not from tty",  /* 23 SIGSTOP   */
-  "stop signal from tty",               /* 24 SIGSTP    */
-  "continue a stopped process",                 /* 25 SIGCONT   */
-  "attempted background tty read",      /* 26 SIGTTIN   */
-  "attempted background tty write",     /* 27 SIGTTOU   */
-  "undefined",                          /* 28           */
-  "undefined",                          /* 29           */
-  "undefined",                          /* 30           */
-  "undefined",                          /* 31           */
-  "undefined",                          /* 32           */
-  "socket (TCP/IP) urgent data arrival", /* 33 SIGURG    */
-  "I/O is possible",                    /* 34 SIGIO     */
-  "exceeded cpu time limit",            /* 35 SIGXCPU   */
-  "exceeded file size limit",           /* 36 SIGXFSZ   */
-  "virtual time alarm",                         /* 37 SIGVTALRM */
-  "profiling time alarm",               /* 38 SIGPROF   */
-  "undefined",                          /* 39           */
-  "file record locks revoked",          /* 40 SIGLOST   */
-  "undefined",                          /* 41           */
-  "undefined",                          /* 42           */
-  "undefined",                          /* 43           */
-  "undefined",                          /* 44           */
-  "undefined",                          /* 45           */
-  "undefined",                          /* 46           */
-  "undefined",                          /* 47           */
-  "undefined",                          /* 48           */
-  "undefined",                          /* 49           */
-  "undefined",                          /* 50           */
-  "undefined",                          /* 51           */
-  "undefined",                          /* 52           */
-  "undefined",                          /* 53           */
-  "undefined",                          /* 54           */
-  "undefined",                          /* 55           */
-  "undefined",                          /* 56           */
-  "undefined",                          /* 57           */
-  "undefined",                          /* 58           */
-  "undefined",                          /* 59           */
-  "undefined",                          /* 60           */
-  "undefined",                          /* 61           */
-  "undefined",                          /* 62           */
-  "undefined",                          /* 63           */
-  "notification message in mess. queue", /* 64 SIGDGNOTIFY */
-  0
-};
-
-#endif /* DGUX */
-\f
 /* Directory routines for systems that don't have them. */
 
 #ifdef SYSV_SYSTEM_DIR
 
 #include <dirent.h>
 
-#ifndef AIX
+#if defined (BROKEN_CLOSEDIR) || !defined (HAVE_CLOSEDIR)
+
 int
 closedir (dirp)
      register DIR *dirp;              /* stream from opendir */
 {
-  sys_close (dirp->dd_fd);
-  xfree ((char *) dirp->dd_buf);       /* directory block defined in <dirent.h> */
+  int rtnval;
+
+  rtnval = emacs_close (dirp->dd_fd);
+
+  /* Some systems (like Solaris) allocate the buffer and the DIR all
+     in one block.  Why in the world are we freeing this ourselves
+     anyway?  */
+#if ! (defined (sun) && defined (USG5_4))
+  xfree ((char *) dirp->dd_buf); /* directory block defined in <dirent.h> */
+#endif
   xfree ((char *) dirp);
+
+  return rtnval;
 }
-#endif /* not AIX */
+#endif /* BROKEN_CLOSEDIR or not HAVE_CLOSEDIR */
 #endif /* SYSV_SYSTEM_DIR */
 
 #ifdef NONSYSTEM_DIR_LIBRARY
@@ -2803,16 +3529,16 @@ opendir (filename)
   register int fd;             /* file descriptor for read */
   struct stat sbuf;            /* result of fstat */
 
-  fd = sys_open (filename, 0);
+  fd = emacs_open (filename, O_RDONLY, 0);
   if (fd < 0)
     return 0;
 
   BLOCK_INPUT;
   if (fstat (fd, &sbuf) < 0
       || (sbuf.st_mode & S_IFMT) != S_IFDIR
-      || (dirp = (DIR *) malloc (sizeof (DIR))) == 0)
+      || (dirp = (DIR *) xmalloc (sizeof (DIR))) == 0)
     {
-      sys_close (fd);
+      emacs_close (fd);
       UNBLOCK_INPUT;
       return 0;                /* bad luck today */
     }
@@ -2828,7 +3554,7 @@ void
 closedir (dirp)
      register DIR *dirp;               /* stream from opendir */
 {
-  sys_close (dirp->dd_fd);
+  emacs_close (dirp->dd_fd);
   xfree ((char *) dirp);
 }
 
@@ -2862,7 +3588,7 @@ readdir (dirp)
        dirp->dd_loc = dirp->dd_size = 0;
 
       if (dirp->dd_size == 0   /* refill buffer */
-         && (dirp->dd_size = sys_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
+         && (dirp->dd_size = emacs_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
        return 0;
 
 #ifndef VMS
@@ -2936,6 +3662,156 @@ readdirver (dirp)
 #endif /* VMS */
 
 #endif /* NONSYSTEM_DIR_LIBRARY */
+
+\f
+int
+set_file_times (filename, atime, mtime)
+     char *filename;
+     EMACS_TIME atime, mtime;
+{
+#ifdef HAVE_UTIMES
+  struct timeval tv[2];
+  tv[0] = atime;
+  tv[1] = mtime;
+  return utimes (filename, tv);
+#else /* not HAVE_UTIMES */
+  struct utimbuf utb;
+  utb.actime = EMACS_SECS (atime);
+  utb.modtime = EMACS_SECS (mtime);
+  return utime (filename, &utb);
+#endif /* not HAVE_UTIMES */
+}
+\f
+/* mkdir and rmdir functions, for systems which don't have them.  */
+
+#ifndef HAVE_MKDIR
+/*
+ * Written by Robert Rother, Mariah Corporation, August 1985.
+ *
+ * If you want it, it's yours.  All I ask in return is that if you
+ * figure out how to do this in a Bourne Shell script you send me
+ * a copy.
+ *                                     sdcsvax!rmr or rmr@uscd
+ *
+ * Severely hacked over by John Gilmore to make a 4.2BSD compatible
+ * subroutine. 11Mar86; hoptoad!gnu
+ *
+ * Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
+ * subroutine didn't return EEXIST.  It does now.
+ */
+
+/*
+ * Make a directory.
+ */
+#ifdef MKDIR_PROTOTYPE
+MKDIR_PROTOTYPE
+#else
+int
+mkdir (dpath, dmode)
+     char *dpath;
+     int dmode;
+#endif
+{
+  int cpid, status, fd;
+  struct stat statbuf;
+
+  if (stat (dpath, &statbuf) == 0)
+    {
+      errno = EEXIST;          /* Stat worked, so it already exists */
+      return -1;
+    }
+
+  /* If stat fails for a reason other than non-existence, return error */
+  if (errno != ENOENT)
+    return -1;
+
+  synch_process_alive = 1;
+  switch (cpid = fork ())
+    {
+
+    case -1:                   /* Error in fork */
+      return (-1);             /* Errno is set already */
+
+    case 0:                    /* Child process */
+      /*
+                * Cheap hack to set mode of new directory.  Since this
+                * child process is going away anyway, we zap its umask.
+                * FIXME, this won't suffice to set SUID, SGID, etc. on this
+                * directory.  Does anybody care?
+                */
+      status = umask (0);      /* Get current umask */
+      status = umask (status | (0777 & ~dmode));       /* Set for mkdir */
+      fd = emacs_open ("/dev/null", O_RDWR, 0);
+      if (fd >= 0)
+        {
+         dup2 (fd, 0);
+         dup2 (fd, 1);
+         dup2 (fd, 2);
+        }
+      execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
+      _exit (-1);              /* Can't exec /bin/mkdir */
+
+    default:                   /* Parent process */
+      wait_for_termination (cpid);
+    }
+
+  if (synch_process_death != 0 || synch_process_retcode != 0)
+    {
+      errno = EIO;             /* We don't know why, but */
+      return -1;               /* /bin/mkdir failed */
+    }
+
+  return 0;
+}
+#endif /* not HAVE_MKDIR */
+
+#ifndef HAVE_RMDIR
+int
+rmdir (dpath)
+     char *dpath;
+{
+  int cpid, status, fd;
+  struct stat statbuf;
+
+  if (stat (dpath, &statbuf) != 0)
+    {
+      /* Stat just set errno.  We don't have to */
+      return -1;
+    }
+
+  synch_process_alive = 1;
+  switch (cpid = fork ())
+    {
+
+    case -1:                   /* Error in fork */
+      return (-1);             /* Errno is set already */
+
+    case 0:                    /* Child process */
+      fd = emacs_open ("/dev/null", O_RDWR, 0);
+      if (fd >= 0)
+        {
+         dup2 (fd, 0);
+         dup2 (fd, 1);
+         dup2 (fd, 2);
+        }
+      execl ("/bin/rmdir", "rmdir", dpath, (char *) 0);
+      _exit (-1);              /* Can't exec /bin/rmdir */
+
+    default:                   /* Parent process */
+      wait_for_termination (cpid);
+    }
+
+  if (synch_process_death != 0 || synch_process_retcode != 0)
+    {
+      errno = EIO;             /* We don't know why, but */
+      return -1;               /* /bin/rmdir failed */
+    }
+
+  return 0;
+}
+#endif /* !HAVE_RMDIR */
+
+
 \f
 /* Functions for VMS */
 #ifdef VMS
@@ -3046,14 +3922,15 @@ sys_access (path, mode)
 #else /* not VMS4_4 */
 
 #include <prvdef.h>
-#define        ACE$M_WRITE     2
-#define        ACE$C_KEYID     1
+#define ACE$M_WRITE     2
+#define ACE$C_KEYID     1
 
 static unsigned short memid, grpid;
 static unsigned int uic;
 
 /* Called from init_sys_modes, so it happens not very often
    but at least each time Emacs is loaded.  */
+void
 sys_access_reinit ()
 {
   uic = 0;
@@ -3080,13 +3957,13 @@ sys_access (filename, type)
       grpid = uic >> 16;
     }
 
-  if (type != 2)               /* not checking write access */
+  if (type != 2)                /* not checking write access */
     return access (filename, type);
 
   /* Check write protection. */
     
-#define        CHECKPRIV(bit)    (prvmask.bit)
-#define        WRITEABLE(field)  (! ((xab.xab$w_pro >> field) & XAB$M_NOWRITE))
+#define CHECKPRIV(bit)    (prvmask.bit)
+#define WRITABLE(field)  (! ((xab.xab$w_pro >> field) & XAB$M_NOWRITE))
 
   /* Find privilege bits */
   status = SYS$SETPRV (0, 0, 0, prvmask);
@@ -3107,7 +3984,7 @@ sys_access (filename, type)
     return -1;
   SYS$CLOSE (&fab, 0, 0);
   /* Check system access */
-  if (CHECKPRIV (PRV$V_SYSPRV) && WRITEABLE (XAB$V_SYS))
+  if (CHECKPRIV (PRV$V_SYSPRV) && WRITABLE (XAB$V_SYS))
     return 0;
   /* Check ACL entries, if any */
   acl_controlled = 0;
@@ -3133,16 +4010,16 @@ sys_access (filename, type)
        return -1;
     }
   /* No ACL entries specified, check normal protection */
-  if (WRITEABLE (XAB$V_WLD))   /* World writeable */
+  if (WRITABLE (XAB$V_WLD))    /* World writable */
     return 0;
-  if (WRITEABLE (XAB$V_GRP) &&
+  if (WRITABLE (XAB$V_GRP) &&
       (unsigned short) (xab.xab$l_uic >> 16) == grpid)
-    return 0;                  /* Group writeable */
-  if (WRITEABLE (XAB$V_OWN) &&
+    return 0;                  /* Group writable */
+  if (WRITABLE (XAB$V_OWN) &&
       (xab.xab$l_uic & 0xFFFF) == memid)
-    return 0;                  /* Owner writeable */
+    return 0;                  /* Owner writable */
 
-  return -1;   /* Not writeable */
+  return -1;   /* Not writable */
 }
 #endif /* not VMS4_4 */
 #endif /* access */
@@ -3288,19 +4165,25 @@ char *
 getwd (pathname)
      char *pathname;
 {
-  char *ptr;
+  char *ptr, *val;
   extern char *getcwd ();
 
 #define MAXPATHLEN 1024
 
   ptr = xmalloc (MAXPATHLEN);
-  getcwd (ptr, MAXPATHLEN);
+  val = getcwd (ptr, MAXPATHLEN);
+  if (val == 0)
+    {
+      xfree (ptr);
+      return val;
+    }
   strcpy (pathname, ptr);
   xfree (ptr);
   
  return pathname;
 }
 
+int
 getppid ()
 {
   long item_code = JPI$_OWNER;
@@ -3323,6 +4206,7 @@ sys_getuid ()
   return (getgid () << 16) | getuid ();
 }
 
+#undef read
 int
 sys_read (fildes, buf, nbyte)
      int fildes;
@@ -3362,6 +4246,7 @@ sys_write (fildes, buf, nbyte)
  *     Thus we do this stupidity below.
  */
 
+#undef write
 int
 sys_write (fildes, buf, nbytes)
      int fildes;
@@ -3496,10 +4381,11 @@ creat_copy_attrs (old, new)
 #endif
 #endif
 
+int
 sys_creat (va_alist)
      va_dcl
 {
-  va_list list_incrementor;
+  va_list list_incrementer;
   char *name;
   int mode;
   int rfd;                     /* related file descriptor */
@@ -3513,12 +4399,12 @@ sys_creat (va_alist)
   extern int vms_stmlf_recfm;
 
   va_count (count);
-  va_start (list_incrementor);
-  name = va_arg (list_incrementor, char *);
-  mode = va_arg (list_incrementor, int);
+  va_start (list_incrementer);
+  name = va_arg (list_incrementer, char *);
+  mode = va_arg (list_incrementer, int);
   if (count > 2)
-    rfd = va_arg (list_incrementor, int);
-  va_end (list_incrementor);
+    rfd = va_arg (list_incrementer, int);
+  va_end (list_incrementer);
   if (count > 2)
     {
       /* Use information from the related file descriptor to set record
@@ -3612,6 +4498,7 @@ sys_creat (va_alist)
 #endif /* creat */
 
 /* fwrite to stdout is S L O W.  Speed it up by using fputc...*/
+int
 sys_fwrite (ptr, size, num, fp)
      register char * ptr;
      FILE * fp;
@@ -3620,6 +4507,7 @@ sys_fwrite (ptr, size, num, fp)
 
   while (tot--)
     fputc (*ptr++, fp);
+  return num;
 }
 
 /*
@@ -3820,7 +4708,7 @@ cnv_uaf_pw (up)
   retpw.pw_uid = up->uaf$w_mem;
   retpw.pw_gid = up->uaf$w_grp;
 
-  /* I suppose this is not the best sytle, to possibly overwrite one
+  /* I suppose this is not the best style, to possibly overwrite one
      byte beyond the end of the field, but what the heck... */
   ptr = &up->uaf$t_username[UAF$S_USERNAME];
   while (ptr[-1] == ' ')
@@ -3907,6 +4795,7 @@ getpwuid (uid)
 /* return total address space available to the current process.  This is
    the sum of the current p0 size, p1 size and free page table entries
    available. */
+int
 vlimit ()
 {
   int item_code;
@@ -3942,6 +4831,7 @@ vlimit ()
   return free_pages + frep0va + (0x7fffffff - frep1va);
 }
 
+int
 define_logical_name (varname, string)
      char *varname;
      char *string;
@@ -3956,6 +4846,7 @@ define_logical_name (varname, string)
   return LIB$SET_LOGICAL (&envdsc, &strdsc, &lnmdsc, 0, 0);
 }
 
+int
 delete_logical_name (varname)
      char *varname;
 {
@@ -3967,15 +4858,23 @@ delete_logical_name (varname)
   return LIB$DELETE_LOGICAL (&envdsc, &lnmdsc);
 }
 
+int
 ulimit ()
-{}
+{
+  return 0;
+}
 
+int
 setpgrp ()
-{}
+{
+  return 0;
+}
 
+int
 execvp ()
 {
   error ("execvp system call not implemented");
+  return -1;
 }
 
 int
@@ -4027,7 +4926,7 @@ rename (from, to)
    are renaming.
 
    We could use the chmod function, but Eunichs uses 3 bits per user category
-   to describe the protection, and VMS uses 4 (write and delete are seperate
+   to describe the protection, and VMS uses 4 (write and delete are separate
    bits).  To maintain portability, the VMS implementation of `chmod' wires
    the W and D bits together.  */
 
@@ -4098,6 +4997,7 @@ rename_sans_version (from,to)
   return 0;
 }
 
+int
 link (file, new)
      char * file, * new;
 {
@@ -4165,6 +5065,7 @@ link (file, new)
   return 0;
 }
 
+void
 croak (badfunc)
      char *badfunc;
 {
@@ -4180,15 +5081,17 @@ random ()
   return rand () - (1 << 30);
 }
 
+void
 srandom (seed)
 {
   srand (seed);
 }
 #endif /* VMS */
 \f
-#ifdef AIX
+#ifdef AIXHFT
 
 /* Called from init_sys_modes.  */
+void
 hft_init ()
 {
   int junk;
@@ -4209,7 +5112,7 @@ hft_init ()
      there's no way to determine the old mapping, so in reset_sys_modes
      we need to assume that the normal map had been present.  Of course, this
      code also doesn't help if on a terminal emulator which doesn't understand
-     HFT VTD's. */
+     HFT VTD's.  */
   {
     struct hfbuf buf;
     struct hfkeymap keymap;
@@ -4240,8 +5143,9 @@ hft_init ()
   line_ins_del_ok = char_ins_del_ok = 0;
 }
 
-/* Reset the rubout key to backspace. */
+/* Reset the rubout key to backspace.  */
 
+void
 hft_reset ()
 {
   struct hfbuf buf;
@@ -4278,4 +5182,133 @@ hft_reset ()
   hftctl (0, HFSKBD, &buf);
 }
 
-#endif /* AIX */
+#endif /* AIXHFT */
+
+#ifdef USE_DL_STUBS
+
+/* These are included on Sunos 4.1 when we do not use shared libraries.
+   X11 libraries may refer to these functions but (we hope) do not
+   actually call them.  */
+
+void *
+dlopen ()
+{
+  return 0;
+}
+
+void *
+dlsym ()
+{
+  return 0;
+}
+
+int
+dlclose ()
+{
+  return -1;
+}
+
+#endif /* USE_DL_STUBS */
+\f
+#ifndef BSTRING
+
+#ifndef bzero
+
+void
+bzero (b, length)
+     register char *b;
+     register int length;
+{
+#ifdef VMS
+  short zero = 0;
+  long max_str = 65535;
+
+  while (length > max_str) {
+    (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
+    length -= max_str;
+    b += max_str;
+  }
+  max_str = length;
+  (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
+#else
+  while (length-- > 0)
+    *b++ = 0;
+#endif /* not VMS */
+}
+
+#endif /* no bzero */
+#endif /* BSTRING */
+
+#if (!defined (BSTRING) && !defined (bcopy)) || defined (NEED_BCOPY)
+#undef bcopy
+
+/* Saying `void' requires a declaration, above, where bcopy is used
+   and that declaration causes pain for systems where bcopy is a macro.  */
+bcopy (b1, b2, length)
+     register char *b1;
+     register char *b2;
+     register int length;
+{
+#ifdef VMS
+  long max_str = 65535;
+
+  while (length > max_str) {
+    (void) LIB$MOVC3 (&max_str, b1, b2);
+    length -= max_str;
+    b1 += max_str;
+    b2 += max_str;
+  }
+  max_str = length;
+  (void) LIB$MOVC3 (&length, b1, b2);
+#else
+  while (length-- > 0)
+    *b2++ = *b1++;
+#endif /* not VMS */
+}
+#endif /* (!defined (BSTRING) && !defined (bcopy)) || defined (NEED_BCOPY) */
+
+#ifndef BSTRING
+#ifndef bcmp
+int
+bcmp (b1, b2, length)  /* This could be a macro! */
+     register char *b1;
+     register char *b2;
+     register int length;
+{
+#ifdef VMS
+  struct dsc$descriptor_s src1 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b1};
+  struct dsc$descriptor_s src2 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b2};
+
+  return STR$COMPARE (&src1, &src2);
+#else
+  while (length-- > 0)
+    if (*b1++ != *b2++)
+      return 1;
+
+  return 0;
+#endif /* not VMS */
+}
+#endif /* no bcmp */
+#endif /* not BSTRING */
+\f
+#ifndef HAVE_STRSIGNAL
+char *
+strsignal (code)
+     int code;
+{
+  char *signame = 0;
+
+  if (0 <= code && code < NSIG)
+    {
+#ifdef VMS
+      signame = sys_errlist[code];
+#else
+      /* Cast to suppress warning if the table has const char *.  */
+      signame = (char *) sys_siglist[code];
+#endif
+    }
+
+  return signame;
+}
+#endif /* HAVE_STRSIGNAL */
+