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>
24 #include <sys/ioctl.h>
28 /* borrowed from detachtty's detachtty.c, in turn borrowed from APUE
29 * example code found at
30 * http://www.yendor.com/programming/unix/apue/pty/main.c
36 int set_noecho(int fd)
38 struct termios stermios;
40 if (tcgetattr(fd, &stermios) < 0) return 0;
42 stermios.c_lflag &= ~( ECHO | /* ECHOE | ECHOK | */ ECHONL);
43 stermios.c_oflag |= (ONLCR);
44 stermios.c_iflag &= ~(BRKINT);
45 stermios.c_iflag |= (ICANON|ICRNL);
47 stermios.c_cc[VERASE]=0177;
48 if (tcsetattr(fd, TCSANOW, &stermios) < 0) return 0;
52 int spawn(char *program, char *argv[], char *envp[], char *pty_name,
53 int stdin, int stdout, int stderr)
61 /* Put us in our own process group. */
64 #elif defined(SVR4) || defined(__linux__) || defined(__osf__)
70 /* If we are supposed to be part of some other pty, go for it. */
72 #if !defined(hpux) && !defined(SVR4)
73 fd = open("/dev/tty", O_RDWR, 0);
75 ioctl(fd, TIOCNOTTY, 0);
79 fd = open(pty_name, O_RDWR, 0);
86 /* Set up stdin, stdout, and stderr */
94 /* Close all other fds. */
96 for (fd = sysconf(_SC_OPEN_MAX)-1; fd >= 3; fd--)
99 for (fd = getdtablesize()-1; fd >= 3; fd--)
103 /* Exec the program. */
104 execve(program, argv, envp);
106 /* It didn't work, so try /bin/sh. */
109 execve("/bin/sh", argv-1, envp);
111 /* The exec didn't work, flame out. */