+ if (pwd && chdir(pwd) < 0) {
+ failure_code = 3;
+ } else {
+ if (envp) {
+ environ = envp;
+ }
+ /* Exec the program. */
+ if (search)
+ execvp(program, argv);
+ else
+ execv(program, argv);
+ }
+
+ /* When exec or chdir fails and channel is available, send the errno value. */
+ if (-1 != channel[1]) {
+ int our_errno = errno;
+ int bytes = sizeof(int);
+ int n;
+ char *p = (char*)&our_errno;
+ while ((bytes > 0) &&
+ (n = write(channel[1], p, bytes))) {
+ if (-1 == n) {
+ if (EINTR == errno) {
+ continue;
+ } else {
+ break;
+ }
+ } else {
+ bytes -= n;
+ p += n;
+ }
+ }
+ close(channel[1]);
+ }
+ _exit(failure_code);
+}
+#else /* !LISP_FEATURE_WIN32 */
+
+# include <windows.h>
+# include <process.h>
+# include <stdio.h>
+# include <stdlib.h>
+# include <fcntl.h>
+# include <io.h>
+
+#define READ_HANDLE 0
+#define WRITE_HANDLE 1
+
+/* These functions do not attempt to deal with wchar_t variations. */
+
+/* Get the value of _environ maintained by MSVCRT */
+char **msvcrt_environ ( void ) {
+ return ( _environ );
+}
+
+/* Set up in, out, err pipes and spawn a program, waiting or otherwise. */
+HANDLE spawn (
+ const char *program,
+ const char *const *argv,
+ int in,
+ int out,
+ int err,
+ int search,
+ char *envp,
+ char *ptyname,
+ int wait,
+ char *pwd
+ )
+{
+ int stdout_backup, stdin_backup, stderr_backup, wait_mode;
+ HANDLE hProcess;
+ HANDLE hReturn;
+
+ /* Duplicate and save the original stdin/out/err handles. */
+ stdout_backup = _dup ( _fileno ( stdout ) );
+ stdin_backup = _dup ( _fileno ( stdin ) );
+ stderr_backup = _dup ( _fileno ( stderr ) );
+
+ /* If we are not using stdin/out/err
+ * then duplicate the new pipes to current stdin/out/err handles.
+ *
+ * Default std fds are used if in, out or err parameters
+ * are -1. */
+
+ hReturn = (HANDLE)-1;
+ hProcess = (HANDLE)-1;
+ if ( ( out >= 0 ) && ( out != _fileno ( stdout ) ) ) {
+ if ( _dup2 ( out, _fileno ( stdout ) ) != 0 ) goto error_exit;
+ }
+ if ( ( in >= 0 ) && ( in != _fileno ( stdin ) ) ) {
+ if ( _dup2 ( in, _fileno ( stdin ) ) != 0 ) goto error_exit_out;
+ }
+ if ( ( err >= 0 ) && ( err != _fileno ( stderr ) ) ) {
+ if ( _dup2 ( err, _fileno ( stderr ) ) != 0 ) goto error_exit_in;
+ }
+
+ /* Set the wait mode. */
+ if ( 0 == wait ) {
+ wait_mode = P_NOWAIT;
+ } else {
+ wait_mode = P_WAIT;
+ }
+
+ /* Change working directory if supplied. */
+ if (pwd) {
+ if (chdir(pwd) < 0) {
+ goto error_exit;
+ }
+ }