1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
10 * The stuff in this file mostly comes from the "screen" program.
11 * Included with permission from Juergen Weigert.
12 * Copied from "pty.c". "putenv.c" was used for putenv() in misc2.c.
14 * It has been modified to work better with Vim.
15 * The parts that are not used in Vim have been deleted.
16 * See the "screen" sources for the complete stuff.
20 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
21 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
22 * Copyright (c) 1987 Oliver Laumann
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2, or (at your option)
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program (see the file COPYING); if not, write to the
36 * Free Software Foundation, Inc.,
37 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
40 /* RCS_ID("$Id: pty.c,v 1.11 2008/08/09 17:54:46 vimboss Exp $ FAU") */
47 # include <sys/termios.h>
50 #ifdef HAVE_SYS_IOCTL_H
51 # include <sys/ioctl.h>
55 #include <sys/types.h>
57 #define buf_T __system_buf_t__
64 # include <sys/conf.h>
75 # ifdef HAVE_TERMIOS_H
81 # include <sys/stream.h>
85 # include <sys/ptem.h>
88 #if !defined(sun) && !defined(VMS) && !defined(MACOS)
89 # include <sys/ioctl.h>
92 #if defined(sun) && defined(LOCKPTY) && !defined(TIOCEXCL)
93 # include <sys/ttold.h>
98 # include <sys/sioctl.h>
103 # include <sys/sysmacros.h>
106 #if defined(_INCLUDE_HPUX_SOURCE) && !defined(hpux)
111 * if no PTYRANGE[01] is in the config file, we pick a default
114 # define PTYRANGE0 "qprs"
117 # define PTYRANGE1 "0123456789abcdef"
120 /* SVR4 pseudo ttys don't seem to work with SCO-5 */
122 # undef HAVE_SVR4_PTYS
125 static void initmaster
__ARGS((int));
128 * Open all ptys with O_NOCTTY, just to be on the safe side
129 * (RISCos mips breaks otherwise)
141 tcflush(f
, TCIOFLUSH
);
144 (void)ioctl(f
, TIOCFLUSH
, (char *) 0);
148 (void)ioctl(f
, TIOCEXCL
, (char *) 0);
154 * This causes a hang on some systems, but is required for a properly working
155 * pty on others. Needs to be tuned...
163 #if defined(I_PUSH) && defined(HAVE_SVR4_PTYS) && !defined(sgi) && !defined(linux) && !defined(__osf__) && !defined(M_UNIX)
164 # if defined(HAVE_SYS_PTEM_H) || defined(hpux)
165 if (ioctl(fd
, I_PUSH
, "ptem") != 0)
168 if (ioctl(fd
, I_PUSH
, "ldterm") != 0)
171 if (ioctl(fd
, I_PUSH
, "ttcompat") != 0)
179 #if defined(OSX) && !defined(PTY_DONE)
186 static char TtyName
[32];
188 if ((f
= open_controlling_pty(TtyName
)) < 0)
196 #if (defined(sequent) || defined(_SEQUENT_)) && defined(HAVE_GETPSEUDOTTY) \
197 && !defined(PTY_DONE)
205 /* used for opening a new pty-pair: */
206 static char PtyName
[32];
207 static char TtyName
[32];
209 if ((f
= getpseudotty(&s
, &m
)) < 0)
214 strncpy(PtyName
, m
, sizeof(PtyName
));
215 strncpy(TtyName
, s
, sizeof(TtyName
));
222 #if defined(__sgi) && !defined(PTY_DONE)
230 RETSIGTYPE (*sigcld
)__ARGS(SIGPROTOARG
);
233 * SIGCHLD set to SIG_DFL for _getpty() because it may fork() and
234 * exec() /usr/adm/mkpts
236 sigcld
= signal(SIGCHLD
, SIG_DFL
);
237 name
= _getpty(&f
, O_RDWR
| O_NONBLOCK
| O_EXTRA
, 0600, 0);
238 signal(SIGCHLD
, sigcld
);
248 #if defined(MIPS) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
256 /* used for opening a new pty-pair: */
257 static char TtyName
[32];
259 if ((f
= open("/dev/ptc", O_RDWR
| O_NOCTTY
| O_NONBLOCK
| O_EXTRA
, 0)) < 0)
261 if (mch_fstat(f
, &buf
) < 0)
266 sprintf(TtyName
, "/dev/ttyq%d", minor(buf
.st_rdev
));
273 #if defined(HAVE_SVR4_PTYS) && !defined(PTY_DONE) && !defined(hpux) && !defined(MACOS_X)
275 /* NOTE: Even though HPUX can have /dev/ptmx, the code below doesn't work!
276 * Same for Mac OS X Leopard. */
284 int unlockpt
__ARGS((int)), grantpt
__ARGS((int));
285 RETSIGTYPE (*sigcld
)__ARGS(SIGPROTOARG
);
286 /* used for opening a new pty-pair: */
287 static char TtyName
[32];
289 if ((f
= open("/dev/ptmx", O_RDWR
| O_NOCTTY
| O_EXTRA
, 0)) == -1)
293 * SIGCHLD set to SIG_DFL for grantpt() because it fork()s and
296 sigcld
= signal(SIGCHLD
, SIG_DFL
);
297 if ((m
= ptsname(f
)) == NULL
|| grantpt(f
) || unlockpt(f
))
299 signal(SIGCHLD
, sigcld
);
303 signal(SIGCHLD
, sigcld
);
304 strncpy(TtyName
, m
, sizeof(TtyName
));
311 #if defined(_AIX) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
323 /* used for opening a new pty-pair: */
324 static char TtyName
[32];
326 /* a dumb looking loop replaced by mycrofts code: */
327 if ((f
= open("/dev/ptc", O_RDWR
| O_NOCTTY
| O_EXTRA
)) < 0)
329 strncpy(TtyName
, ttyname(f
), sizeof(TtyName
));
330 if (geteuid() != ROOT_UID
&& mch_access(TtyName
, R_OK
| W_OK
))
339 if ((aixhack
= open(TtyName
, O_RDWR
| O_NOCTTY
| O_EXTRA
, 0)) < 0)
353 static char PtyProto
[] = "/dev/ptym/ptyXY";
354 static char TtyProto
[] = "/dev/pty/ttyXY";
357 static char PtyProto
[] = "/dev/pt/XY";
358 static char TtyProto
[] = "/dev/tt/XY";
360 static char PtyProto
[] = "/dev/ptyXY";
361 static char TtyProto
[] = "/dev/ttyXY";
371 /* used for opening a new pty-pair: */
372 static char PtyName
[32];
373 static char TtyName
[32];
375 strcpy(PtyName
, PtyProto
);
376 strcpy(TtyName
, TtyProto
);
377 for (p
= PtyName
; *p
!= 'X'; p
++)
379 for (q
= TtyName
; *q
!= 'X'; q
++)
381 for (l
= PTYRANGE0
; (*p
= *l
) != '\0'; l
++)
383 for (d
= PTYRANGE1
; (p
[1] = *d
) != '\0'; d
++)
385 #if !defined(MACOS) || defined(USE_CARBONIZED)
386 if ((f
= open(PtyName
, O_RDWR
| O_NOCTTY
| O_EXTRA
, 0)) == -1)
388 if ((f
= open(PtyName
, O_RDWR
| O_NOCTTY
| O_EXTRA
)) == -1)
394 if (geteuid() != ROOT_UID
&& mch_access(TtyName
, R_OK
| W_OK
))
400 #if defined(sun) && defined(TIOCGPGRP) && !defined(SUNOS3)
401 /* Hack to ensure that the slave side of the pty is
402 * unused. May not work in anything other than SunOS4.1
407 /* tcgetpgrp does not work (uses TIOCGETPGRP)! */
408 if (ioctl(f
, TIOCGPGRP
, (char *)&pgrp
) != -1 || errno
!= EIO
)