Include <unistd.h> unilaterally.
[bpt/emacs.git] / src / callproc.c
index fdfa4c7..5002541 100644 (file)
@@ -1,6 +1,6 @@
 /* Synchronous subprocess invocation for GNU Emacs.
    Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1999, 2000, 2001,
-                 2002, 2003, 2004, 2005, 2006, 2007, 2008
+                 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
                  Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
@@ -23,54 +23,25 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <signal.h>
 #include <errno.h>
 #include <stdio.h>
-
-#ifndef USE_CRT_DLL
-extern int errno;
-#endif
-
-/* Define SIGCHLD as an alias for SIGCLD.  */
-
-#if !defined (SIGCHLD) && defined (SIGCLD)
-#define SIGCHLD SIGCLD
-#endif /* SIGCLD */
-
+#include <setjmp.h>
 #include <sys/types.h>
-
-#ifdef HAVE_UNISTD_H
 #include <unistd.h>
-#endif
 
 #include <sys/file.h>
-#ifdef HAVE_FCNTL_H
-#define INCLUDED_FCNTL
 #include <fcntl.h>
-#endif
 
 #ifdef WINDOWSNT
 #define NOMINMAX
 #include <windows.h>
-#include <stdlib.h>    /* for proper declaration of environ */
-#include <fcntl.h>
 #include "w32.h"
 #define _P_NOWAIT 1    /* from process.h */
 #endif
 
 #ifdef MSDOS   /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
-#define INCLUDED_FCNTL
-#include <fcntl.h>
 #include <sys/stat.h>
 #include <sys/param.h>
-#include <errno.h>
 #endif /* MSDOS */
 
-#ifndef O_RDONLY
-#define O_RDONLY 0
-#endif
-
-#ifndef O_WRONLY
-#define O_WRONLY 1
-#endif
-
 #include "lisp.h"
 #include "commands.h"
 #include "buffer.h"
@@ -95,7 +66,7 @@ extern char **environ;
 #endif
 
 #ifdef HAVE_SETPGID
-#if !defined (USG) || defined (BSD_PGRPS)
+#if !defined (USG)
 #undef setpgrp
 #define setpgrp setpgid
 #endif
@@ -104,7 +75,9 @@ extern char **environ;
 Lisp_Object Vexec_path, Vexec_directory, Vexec_suffixes;
 Lisp_Object Vdata_directory, Vdoc_directory;
 Lisp_Object Vconfigure_info_directory, Vshared_game_score_directory;
-Lisp_Object Vtemp_file_name_pattern;
+
+/* Pattern used by call-process-region to make temp files.  */
+static Lisp_Object Vtemp_file_name_pattern;
 
 Lisp_Object Vshell_file_name;
 
@@ -119,7 +92,7 @@ Lisp_Object Qbuffer_file_type;
 int synch_process_alive;
 
 /* Nonzero => this is a string explaining death of synchronous subprocess.  */
-char *synch_process_death;
+const char *synch_process_death;
 
 /* Nonzero => this is the signal number that terminated the subprocess.  */
 int synch_process_termsig;
@@ -139,8 +112,7 @@ static int call_process_exited;
 EXFUN (Fgetenv_internal, 2);
 
 static Lisp_Object
-call_process_kill (fdpid)
-     Lisp_Object fdpid;
+call_process_kill (Lisp_Object fdpid)
 {
   emacs_close (XFASTINT (Fcar (fdpid)));
   EMACS_KILLPG (XFASTINT (Fcdr (fdpid)), SIGKILL);
@@ -149,18 +121,25 @@ call_process_kill (fdpid)
 }
 
 Lisp_Object
-call_process_cleanup (fdpid)
-     Lisp_Object fdpid;
+call_process_cleanup (Lisp_Object arg)
 {
+  Lisp_Object fdpid = Fcdr (arg);
+#if defined (MSDOS)
+  Lisp_Object file;
+#else
+  int pid;
+#endif
+
+  Fset_buffer (Fcar (arg));
+
 #if defined (MSDOS)
   /* for MSDOS fdpid is really (fd . tempfile)  */
-  register Lisp_Object file;
   file = Fcdr (fdpid);
   emacs_close (XFASTINT (Fcar (fdpid)));
   if (strcmp (SDATA (file), NULL_DEVICE) != 0)
     unlink (SDATA (file));
 #else /* not MSDOS */
-  register int pid = XFASTINT (Fcdr (fdpid));
+  pid = XFASTINT (Fcdr (fdpid));
 
   if (call_process_exited)
     {
@@ -211,9 +190,7 @@ and returns a numeric exit status or a signal description string.
 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
 
 usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
-     (nargs, args)
-     int nargs;
-     register Lisp_Object *args;
+  (int nargs, register Lisp_Object *args)
 {
   Lisp_Object infile, buffer, current_dir, path;
   int display_p;
@@ -226,9 +203,7 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
   int bufsize = CALLPROC_BUFFER_SIZE_MIN;
   int count = SPECPDL_INDEX ();
 
-  register const unsigned char **new_argv
-    = (const unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *));
-  struct buffer *old = current_buffer;
+  register const unsigned char **new_argv;
   /* File to use for stderr in the child.
      t means use same as standard output.  */
   Lisp_Object error_file;
@@ -276,21 +251,16 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
        if (!NILP (Vcoding_system_for_write))
          val = Vcoding_system_for_write;
        else if (! must_encode)
-         val = Qnil;
+         val = Qraw_text;
        else
          {
            args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
            args2[0] = Qcall_process;
            for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
            coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
-           if (CONSP (coding_systems))
-             val = XCDR (coding_systems);
-           else if (CONSP (Vdefault_process_coding_system))
-             val = XCDR (Vdefault_process_coding_system);
-           else
-             val = Qnil;
+           val = CONSP (coding_systems) ? XCDR (coding_systems) : Qnil;
          }
-       val = coding_inherit_eol_type (val, Qnil);
+       val = complement_process_encoding_system (val);
        setup_coding_system (Fcheck_coding_system (val), &argument_coding);
        coding_attrs = CODING_ID_ATTRS (argument_coding.id);
        if (NILP (CODING_ATTR_ASCII_COMPAT (coding_attrs)))
@@ -396,9 +366,9 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
     }
   /* Search for program; barf if not found.  */
   {
-    struct gcpro gcpro1;
+    struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
 
-    GCPRO1 (current_dir);
+    GCPRO4 (infile, buffer, current_dir, error_file);
     openp (Vexec_path, args[0], Vexec_suffixes, &path, make_number (X_OK));
     UNGCPRO;
   }
@@ -414,13 +384,14 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
       && SREF (path, 1) == ':')
     path = Fsubstring (path, make_number (2), Qnil);
 
-  new_argv[0] = SDATA (path);
+  new_argv = (const unsigned char **)
+    alloca (max (2, nargs - 2) * sizeof (char *));
   if (nargs > 4)
     {
       register int i;
-      struct gcpro gcpro1, gcpro2, gcpro3;
+      struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
 
-      GCPRO3 (infile, buffer, current_dir);
+      GCPRO5 (infile, buffer, current_dir, path, error_file);
       argument_coding.dst_multibyte = 0;
       for (i = 4; i < nargs; i++)
        {
@@ -428,13 +399,15 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
          if (CODING_REQUIRE_ENCODING (&argument_coding))
            /* We must encode this argument.  */
            args[i] = encode_coding_string (&argument_coding, args[i], 1);
-         new_argv[i - 3] = SDATA (args[i]);
        }
       UNGCPRO;
-      new_argv[nargs - 3] = 0;
+      for (i = 4; i < nargs; i++)
+       new_argv[i - 3] = SDATA (args[i]);
+      new_argv[i - 3] = 0;
     }
   else
     new_argv[1] = 0;
+  new_argv[0] = SDATA (path);
 
 #ifdef MSDOS /* MW, July 1993 */
   if ((outf = egetenv ("TMPDIR")))
@@ -572,7 +545,7 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
 #ifdef HAVE_SETSID
         setsid ();
 #endif
-#if defined (USG) && !defined (BSD_PGRPS)
+#if defined (USG)
         setpgrp ();
 #else
         setpgrp (pid, pid);
@@ -609,12 +582,6 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
     {
       if (fd[0] >= 0)
        emacs_close (fd[0]);
-#ifndef subprocesses
-      /* If Emacs has been built with asynchronous subprocess support,
-        we don't need to do this, I think because it will then have
-        the facilities for handling SIGCHLD.  */
-      wait_without_blocking ();
-#endif /* subprocesses */
       return Qnil;
     }
 
@@ -624,10 +591,13 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
 #if defined(MSDOS)
   /* MSDOS needs different cleanup information.  */
   record_unwind_protect (call_process_cleanup,
-                        Fcons (make_number (fd[0]), build_string (tempfile)));
+                        Fcons (Fcurrent_buffer (),
+                               Fcons (make_number (fd[0]),
+                                      build_string (tempfile))));
 #else
   record_unwind_protect (call_process_cleanup,
-                        Fcons (make_number (fd[0]), make_number (pid)));
+                        Fcons (Fcurrent_buffer (),
+                               Fcons (make_number (fd[0]), make_number (pid))));
 #endif /* not MSDOS */
 
 
@@ -680,9 +650,9 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
   QUIT;
 
   {
-    register int nread;
+    register EMACS_INT nread;
     int first = 1;
-    int total_read = 0;
+    EMACS_INT total_read = 0;
     int carryover = 0;
     int display_on_the_fly = display_p;
     struct coding_system saved_coding;
@@ -726,10 +696,18 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
            else
              {                 /* We have to decode the input.  */
                Lisp_Object curbuf;
+               int count1 = SPECPDL_INDEX ();
 
                XSETBUFFER (curbuf, current_buffer);
+               /* We cannot allow after-change-functions be run
+                  during decoding, because that might modify the
+                  buffer, while we rely on process_coding.produced to
+                  faithfully reflect inserted text until we
+                  TEMP_SET_PT_BOTH below.  */
+               specbind (Qinhibit_modification_hooks, Qt);
                decode_coding_c_string (&process_coding, buf, nread,
                                        curbuf);
+               unbind_to (count1, Qnil);
                if (display_on_the_fly
                    && CODING_REQUIRE_DETECTION (&saved_coding)
                    && ! CODING_REQUIRE_DETECTION (&process_coding))
@@ -759,23 +737,19 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
                                  PT_BYTE + process_coding.produced);
                carryover = process_coding.carryover_bytes;
                if (carryover > 0)
-                 /* As CARRYOVER should not be that large, we had
-                    better avoid overhead of bcopy.  */
-                 BCOPY_SHORT (process_coding.carryover, buf,
-                              process_coding.carryover_bytes);
+                 memcpy (buf, process_coding.carryover,
+                         process_coding.carryover_bytes);
              }
          }
 
        if (process_coding.mode & CODING_MODE_LAST_BLOCK)
          break;
 
-#if (CALLPROC_BUFFER_SIZE_MIN != CALLPROC_BUFFER_SIZE_MAX)
        /* Make the buffer bigger as we continue to read more data,
           but not past CALLPROC_BUFFER_SIZE_MAX.  */
        if (bufsize < CALLPROC_BUFFER_SIZE_MAX && total_read > 32 * bufsize)
          if ((bufsize *= 2) > CALLPROC_BUFFER_SIZE_MAX)
            bufsize = CALLPROC_BUFFER_SIZE_MAX;
-#endif
 
        if (display_p)
          {
@@ -798,16 +772,16 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
        coding-system used to decode the process output.  */
     if (inherit_process_coding_system)
       call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
-              make_number (total_read));
+            make_number (total_read));
   }
 
+#ifndef MSDOS
   /* Wait for it to terminate, unless it already has.  */
   wait_for_termination (pid);
+#endif
 
   immediate_quit = 0;
 
-  set_buffer_internal (old);
-
   /* Don't kill any children that the subprocess may have left behind
      when exiting.  */
   call_process_exited = 1;
@@ -816,7 +790,7 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
 
   if (synch_process_termsig)
     {
-      char *signame;
+      const char *signame;
 
       synchronize_system_messages_locale ();
       signame = strsignal (synch_process_termsig);
@@ -834,8 +808,7 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
 }
 \f
 static Lisp_Object
-delete_temp_file (name)
-     Lisp_Object name;
+delete_temp_file (Lisp_Object name)
 {
   /* Suppress jka-compr handling, etc.  */
   int count = SPECPDL_INDEX ();
@@ -868,9 +841,7 @@ and returns a numeric exit status or a signal description string.
 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
 
 usage: (call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &rest ARGS)  */)
-     (nargs, args)
-     int nargs;
-     register Lisp_Object *args;
+  (int nargs, register Lisp_Object *args)
 {
   struct gcpro gcpro1;
   Lisp_Object filename_string;
@@ -880,36 +851,32 @@ usage: (call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &r
   Lisp_Object coding_systems;
   Lisp_Object val, *args2;
   int i;
-#ifdef DOS_NT
   char *tempfile;
-  char *outf = '\0';
+  Lisp_Object tmpdir, pattern;
 
-  if ((outf = egetenv ("TMPDIR"))
-      || (outf = egetenv ("TMP"))
-      || (outf = egetenv ("TEMP")))
-    strcpy (tempfile = alloca (strlen (outf) + 20), outf);
+  if (STRINGP (Vtemporary_file_directory))
+    tmpdir = Vtemporary_file_directory;
   else
     {
-      tempfile = alloca (20);
-      *tempfile = '\0';
-    }
-  if (!IS_DIRECTORY_SEP (tempfile[strlen (tempfile) - 1]))
-    strcat (tempfile, "/");
-  if ('/' == DIRECTORY_SEP)
-    dostounix_filename (tempfile);
-  else
-    unixtodos_filename (tempfile);
-#ifdef WINDOWSNT
-  strcat (tempfile, "emXXXXXX");
-#else
-  strcat (tempfile, "detmp.XXX");
+#ifndef DOS_NT
+      if (getenv ("TMPDIR"))
+       tmpdir = build_string (getenv ("TMPDIR"));
+      else
+       tmpdir = build_string ("/tmp/");
+#else /* DOS_NT */
+      char *outf;
+      if ((outf = egetenv ("TMPDIR"))
+         || (outf = egetenv ("TMP"))
+         || (outf = egetenv ("TEMP")))
+       tmpdir = build_string (outf);
+      else
+       tmpdir = Ffile_name_as_directory (build_string ("c:/temp"));
 #endif
-#else /* not DOS_NT */
-  char *tempfile = (char *) alloca (SBYTES (Vtemp_file_name_pattern) + 1);
-  bcopy (SDATA (Vtemp_file_name_pattern), tempfile,
-        SBYTES (Vtemp_file_name_pattern) + 1);
-#endif /* not DOS_NT */
+    }
 
+  pattern = Fexpand_file_name (Vtemp_file_name_pattern, tmpdir);
+  tempfile = (char *) alloca (SBYTES (pattern) + 1);
+  memcpy (tempfile, SDATA (pattern), SBYTES (pattern) + 1);
   coding_systems = Qt;
 
 #ifdef HAVE_MKSTEMP
@@ -937,20 +904,16 @@ usage: (call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &r
   if (!NILP (Vcoding_system_for_write))
     val = Vcoding_system_for_write;
   else if (NILP (current_buffer->enable_multibyte_characters))
-    val = Qnil;
+    val = Qraw_text;
   else
     {
       args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
       args2[0] = Qcall_process_region;
       for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
       coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
-      if (CONSP (coding_systems))
-       val = XCDR (coding_systems);
-      else if (CONSP (Vdefault_process_coding_system))
-       val = XCDR (Vdefault_process_coding_system);
-      else
-       val = Qnil;
+      val = CONSP (coding_systems) ? XCDR (coding_systems) : Qnil;
     }
+  val = complement_process_encoding_system (val);
 
   {
     int count1 = SPECPDL_INDEX ();
@@ -987,7 +950,9 @@ usage: (call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &r
   RETURN_UNGCPRO (unbind_to (count, Fcall_process (nargs, args)));
 }
 \f
-static int relocate_fd ();
+#ifndef WINDOWSNT
+static int relocate_fd (int fd, int minfd);
+#endif
 
 static char **
 add_env (char **env, char **new_env, char *string)
@@ -1042,11 +1007,7 @@ add_env (char **env, char **new_env, char *string)
    executable directory by the parent.  */
 
 int
-child_setup (in, out, err, new_argv, set_pgrp, current_dir)
-     int in, out, err;
-     register char **new_argv;
-     int set_pgrp;
-     Lisp_Object current_dir;
+child_setup (int in, int out, int err, register char **new_argv, int set_pgrp, Lisp_Object current_dir)
 {
   char **env;
   char *pwd_var;
@@ -1057,19 +1018,9 @@ child_setup (in, out, err, new_argv, set_pgrp, current_dir)
 
   int pid = getpid ();
 
-#ifdef SET_EMACS_PRIORITY
-  {
-    extern EMACS_INT emacs_priority;
-
-    if (emacs_priority < 0)
-      nice (- emacs_priority);
-  }
-#endif
-
-#ifdef subprocesses
   /* Close Emacs's descriptors that this process should not have.  */
   close_process_descs ();
-#endif
+
   /* DOS_NT isn't in a vfork, so if we are in the middle of load-file,
      we will lose if we call close_load_descs here.  */
 #ifndef DOS_NT
@@ -1096,8 +1047,8 @@ child_setup (in, out, err, new_argv, set_pgrp, current_dir)
     pwd_var = (char *) alloca (i + 6);
 #endif
     temp = pwd_var + 4;
-    bcopy ("PWD=", pwd_var, 4);
-    bcopy (SDATA (current_dir), temp, i);
+    memcpy (pwd_var, "PWD=", 4);
+    memcpy (temp, SDATA (current_dir), i);
     if (!IS_DIRECTORY_SEP (temp[i - 1])) temp[i++] = DIRECTORY_SEP;
     temp[i] = 0;
 
@@ -1109,16 +1060,14 @@ child_setup (in, out, err, new_argv, set_pgrp, current_dir)
        at least check.  */
     if (chdir (temp) < 0)
       _exit (errno);
-#endif
-
-#ifdef DOS_NT
+#else /* DOS_NT */
     /* Get past the drive letter, so that d:/ is left alone.  */
     if (i > 2 && IS_DEVICE_SEP (temp[1]) && IS_DIRECTORY_SEP (temp[2]))
       {
        temp += 2;
        i -= 2;
       }
-#endif
+#endif /* DOS_NT */
 
     /* Strip trailing slashes for PWD, but leave "/" and "//" alone.  */
     while (i > 2 && IS_DIRECTORY_SEP (temp[i - 1]))
@@ -1132,7 +1081,7 @@ child_setup (in, out, err, new_argv, set_pgrp, current_dir)
     char **p, **q;
     register int new_length;
     Lisp_Object display = Qnil;
-    
+
     new_length = 0;
 
     for (tem = Vprocess_environment;
@@ -1168,7 +1117,7 @@ child_setup (in, out, err, new_argv, set_pgrp, current_dir)
        but with corrected value.  */
     if (egetenv ("PWD"))
       *new_env++ = pwd_var;
+
     if (STRINGP (display))
       {
        int vlen = strlen ("DISPLAY=") + strlen (SDATA (display)) + 1;
@@ -1198,10 +1147,18 @@ child_setup (in, out, err, new_argv, set_pgrp, current_dir)
       }
   }
 
-  
+
 #ifdef WINDOWSNT
   prepare_standard_handles (in, out, err, handles);
   set_process_dir (SDATA (current_dir));
+  /* Spawn the child.  (See ntproc.c:Spawnve).  */
+  cpid = spawnve (_P_NOWAIT, new_argv[0], new_argv, env);
+  reset_standard_handles (in, out, err, handles);
+  if (cpid == -1)
+    /* An error occurred while trying to spawn the process.  */
+    report_file_error ("Spawning child process", Qnil);
+  return cpid;
+
 #else  /* not WINDOWSNT */
   /* Make sure that in, out, and err are not actually already in
      descriptors zero, one, or two; this could happen if Emacs is
@@ -1236,38 +1193,22 @@ child_setup (in, out, err, new_argv, set_pgrp, current_dir)
   dup2 (out, 1);
   dup2 (err, 2);
   emacs_close (in);
-  emacs_close (out);
-  emacs_close (err);
-#endif /* not MSDOS */
-#endif /* not WINDOWSNT */
+  if (out != in)
+    emacs_close (out);
+  if (err != in && err != out)
+    emacs_close (err);
 
-#if defined(USG) && !defined(BSD_PGRPS)
+#if defined(USG)
 #ifndef SETPGRP_RELEASES_CTTY
   setpgrp ();                  /* No arguments but equivalent in this case */
 #endif
-#else
+#else /* not USG */
   setpgrp (pid, pid);
-#endif /* USG */
+#endif /* not USG */
+
   /* setpgrp_of_tty is incorrect here; it uses input_fd.  */
-  EMACS_SET_TTY_PGRP (0, &pid);
+  tcsetpgrp (0, pid);
 
-#ifdef MSDOS
-  pid = run_msdos_command (new_argv, pwd_var + 4, in, out, err, env);
-  xfree (pwd_var);
-  if (pid == -1)
-    /* An error occurred while trying to run the subprocess.  */
-    report_file_error ("Spawning child process", Qnil);
-  return pid;
-#else  /* not MSDOS */
-#ifdef WINDOWSNT
-  /* Spawn the child.  (See ntproc.c:Spawnve).  */
-  cpid = spawnve (_P_NOWAIT, new_argv[0], new_argv, env);
-  reset_standard_handles (in, out, err, handles);
-  if (cpid == -1)
-    /* An error occurred while trying to spawn the process.  */
-    report_file_error ("Spawning child process", Qnil);
-  return cpid;
-#else /* not WINDOWSNT */
   /* execvp does not accept an environment arg so the only way
      to pass this environment is to set environ.  Our caller
      is responsible for restoring the ambient value of environ.  */
@@ -1278,46 +1219,57 @@ child_setup (in, out, err, new_argv, set_pgrp, current_dir)
   emacs_write (1, new_argv[0], strlen (new_argv[0]));
   emacs_write (1, "\n", 1);
   _exit (1);
-#endif /* not WINDOWSNT */
-#endif /* not MSDOS */
+
+#else /* MSDOS */
+  pid = run_msdos_command (new_argv, pwd_var + 4, in, out, err, env);
+  xfree (pwd_var);
+  if (pid == -1)
+    /* An error occurred while trying to run the subprocess.  */
+    report_file_error ("Spawning child process", Qnil);
+  return pid;
+#endif  /* MSDOS */
+#endif  /* not WINDOWSNT */
 }
 
+#ifndef WINDOWSNT
 /* Move the file descriptor FD so that its number is not less than MINFD.
    If the file descriptor is moved at all, the original is freed.  */
 static int
-relocate_fd (fd, minfd)
-     int fd, minfd;
+relocate_fd (int fd, int minfd)
 {
   if (fd >= minfd)
     return fd;
   else
     {
-      int new = dup (fd);
+      int new;
+#ifdef F_DUPFD
+      new = fcntl (fd, F_DUPFD, minfd);
+#else
+      new = dup (fd);
+      if (new != -1)
+       /* Note that we hold the original FD open while we recurse,
+          to guarantee we'll get a new FD if we need it.  */
+       new = relocate_fd (new, minfd);
+#endif
       if (new == -1)
        {
-         char *message1 = "Error while setting up child: ";
-         char *errmessage = strerror (errno);
-         char *message2 = "\n";
+         const char *message1 = "Error while setting up child: ";
+         const char *errmessage = strerror (errno);
+         const char *message2 = "\n";
          emacs_write (2, message1, strlen (message1));
          emacs_write (2, errmessage, strlen (errmessage));
          emacs_write (2, message2, strlen (message2));
          _exit (1);
        }
-      /* Note that we hold the original FD open while we recurse,
-        to guarantee we'll get a new FD if we need it.  */
-      new = relocate_fd (new, minfd);
       emacs_close (fd);
       return new;
     }
 }
+#endif /* not WINDOWSNT */
 
 static int
-getenv_internal_1 (var, varlen, value, valuelen, env)
-     char *var;
-     int varlen;
-     char **value;
-     int *valuelen;
-     Lisp_Object env;
+getenv_internal_1 (const char *var, int varlen, char **value, int *valuelen,
+                  Lisp_Object env)
 {
   for (; CONSP (env); env = XCDR (env))
     {
@@ -1328,7 +1280,7 @@ getenv_internal_1 (var, varlen, value, valuelen, env)
          /* NT environment variables are case insensitive.  */
          && ! strnicmp (SDATA (entry), var, varlen)
 #else  /* not WINDOWSNT */
-         && ! bcmp (SDATA (entry), var, varlen)
+         && ! memcmp (SDATA (entry), var, varlen)
 #endif /* not WINDOWSNT */
          )
        {
@@ -1351,12 +1303,8 @@ getenv_internal_1 (var, varlen, value, valuelen, env)
 }
 
 static int
-getenv_internal (var, varlen, value, valuelen, frame)
-     char *var;
-     int varlen;
-     char **value;
-     int *valuelen;
-     Lisp_Object frame;
+getenv_internal (const char *var, int varlen, char **value, int *valuelen,
+                Lisp_Object frame)
 {
   /* Try to find VAR in Vprocess_environment first.  */
   if (getenv_internal_1 (var, varlen, value, valuelen,
@@ -1388,17 +1336,12 @@ DEFUN ("getenv-internal", Fgetenv_internal, Sgetenv_internal, 1, 2, 0,
 VARIABLE should be a string.  Value is nil if VARIABLE is undefined in
 the environment.  Otherwise, value is a string.
 
-This function searches `process-environment' for VARIABLE.  If it is
-not found there, then it continues the search in the environment list
-of the selected frame.
+This function searches `process-environment' for VARIABLE.
 
 If optional parameter ENV is a list, then search this list instead of
-`process-environment', and return t when encountering a negative entry.
-
-If it is a frame, then this function will ignore `process-environment' and
-will simply look up the variable in that frame's environment.  */)
-     (variable, env)
-     Lisp_Object variable, env;
+`process-environment', and return t when encountering a negative entry
+\(an entry for a variable with no value).  */)
+  (Lisp_Object variable, Lisp_Object env)
 {
   char *value;
   int valuelen;
@@ -1422,8 +1365,7 @@ will simply look up the variable in that frame's environment.  */)
 /* A version of getenv that consults the Lisp environment lists,
    easily callable from C.  */
 char *
-egetenv (var)
-     char *var;
+egetenv (const char *var)
 {
   char *value;
   int valuelen;
@@ -1438,7 +1380,7 @@ egetenv (var)
 /* This is run before init_cmdargs.  */
 
 void
-init_callproc_1 ()
+init_callproc_1 (void)
 {
   char *data_dir = egetenv ("EMACSDATA");
   char *doc_dir = egetenv ("EMACSDOC");
@@ -1460,7 +1402,7 @@ init_callproc_1 ()
 /* This is run after init_cmdargs, when Vinstallation_directory is valid.  */
 
 void
-init_callproc ()
+init_callproc (void)
 {
   char *data_dir = egetenv ("EMACSDATA");
 
@@ -1539,16 +1481,6 @@ init_callproc ()
   sh = (char *) getenv ("SHELL");
   Vshell_file_name = build_string (sh ? sh : "/bin/sh");
 
-  if (getenv ("TMPDIR"))
-    {
-      char *dir = getenv ("TMPDIR");
-      Vtemp_file_name_pattern
-       = Fexpand_file_name (build_string ("emacsXXXXXX"),
-                            build_string (dir));
-    }
-  else
-    Vtemp_file_name_pattern = build_string ("/tmp/emacsXXXXXX");
-
 #ifdef DOS_NT
   Vshared_game_score_directory = Qnil;
 #else
@@ -1559,16 +1491,15 @@ init_callproc ()
 }
 
 void
-set_initial_environment ()
+set_initial_environment (void)
 {
   register char **envp;
-#ifndef CANNOT_DUMP
-  if (initialized)
-    {
+#ifdef CANNOT_DUMP
+  Vprocess_environment = Qnil;
 #else
-    {
-      Vprocess_environment = Qnil;
+  if (initialized)
 #endif
+    {
       for (envp = environ; *envp; envp++)
        Vprocess_environment = Fcons (build_string (*envp),
                                      Vprocess_environment);
@@ -1579,13 +1510,22 @@ set_initial_environment ()
 }
 
 void
-syms_of_callproc ()
+syms_of_callproc (void)
 {
 #ifdef DOS_NT
-  Qbuffer_file_type = intern ("buffer-file-type");
+  Qbuffer_file_type = intern_c_string ("buffer-file-type");
   staticpro (&Qbuffer_file_type);
 #endif /* DOS_NT */
 
+#ifndef DOS_NT
+  Vtemp_file_name_pattern = build_string ("emacsXXXXXX");
+#elif defined (WINDOWSNT)
+  Vtemp_file_name_pattern = build_string ("emXXXXXX");
+#else
+  Vtemp_file_name_pattern = build_string ("detmp.XXX");
+#endif
+  staticpro (&Vtemp_file_name_pattern);
+
   DEFVAR_LISP ("shell-file-name", &Vshell_file_name,
               doc: /* *File name to load inferior shells from.
 Initialized from the SHELL environment variable, or to a system-dependent
@@ -1629,11 +1569,6 @@ If this variable is nil, then Emacs is unable to use a shared directory.  */);
   Vshared_game_score_directory = build_string (PATH_GAME);
 #endif
 
-  DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern,
-              doc: /* Pattern for making names for temporary files.
-This is used by `call-process-region'.  */);
-  /* This variable is initialized in init_callproc.  */
-
   DEFVAR_LISP ("initial-environment", &Vinitial_environment,
               doc: /* List of environment variables inherited from the parent process.
 Each element should be a string of the form ENVVARNAME=VALUE.
@@ -1668,5 +1603,3 @@ See `setenv' and `getenv'.  */);
   defsubr (&Scall_process_region);
 }
 
-/* arch-tag: 769b8045-1df7-4d2b-8968-e3fb49017f95
-   (do not change this comment) */