2 * support for the Lisp function RUN-PROGRAM and friends
6 * This software is part of the SBCL system. See the README file for
9 * This software is derived from the CMU CL system, which was
10 * written at Carnegie Mellon University and released into the
11 * public domain. The software is in the public domain and is
12 * provided with absolutely no warranty. See the COPYING and CREDITS
13 * files for more information.
18 #include <sys/types.h>
21 #include <sys/ioctl.h>
22 #if defined(SVR4) || defined(__linux__)
26 #include <sys/ioctl.h>
30 /* borrowed from detachtty's detachtty.c, in turn borrowed from APUE
31 * example code found at
32 * http://www.yendor.com/programming/unix/apue/pty/main.c
38 int set_noecho(int fd
)
40 struct termios stermios
;
42 if (tcgetattr(fd
, &stermios
) < 0) return 0;
44 stermios
.c_lflag
&= ~( ECHO
| /* ECHOE | ECHOK | */ ECHONL
);
45 stermios
.c_oflag
|= (ONLCR
);
46 stermios
.c_iflag
&= ~(BRKINT
);
47 stermios
.c_iflag
|= (ICANON
|ICRNL
);
49 stermios
.c_cc
[VERASE
]=0177;
50 if (tcsetattr(fd
, TCSANOW
, &stermios
) < 0) return 0;
54 int spawn(char *program
, char *argv
[], char *envp
[], char *pty_name
,
55 int stdin
, int stdout
, int stderr
)
63 /* Put us in our own process group. */
66 #elif defined(SVR4) || defined(__linux__)
72 /* If we are supposed to be part of some other pty, go for it. */
74 #if !defined(hpux) && !defined(SVR4)
75 fd
= open("/dev/tty", O_RDWR
, 0);
77 ioctl(fd
, TIOCNOTTY
, 0);
81 fd
= open(pty_name
, O_RDWR
, 0);
88 /* Set up stdin, stdout, and stderr */
96 /* Close all other fds. */
98 for (fd
= sysconf(_SC_OPEN_MAX
)-1; fd
>= 3; fd
--)
101 for (fd
= getdtablesize()-1; fd
>= 3; fd
--)
105 /* Exec the program. */
106 execve(program
, argv
, envp
);
108 /* It didn't work, so try /bin/sh. */
111 execve("/bin/sh", argv
-1, envp
);
113 /* The exec didn't work, flame out. */