Release v.5.0.0
[screen.git] / src / pty.c
blob513940335ca2f1022bcb2b44dbb9a9daf4bcaa39
1 /* Copyright (c) 2008, 2009
2 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
3 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
4 * Micah Cowan (micah@cowan.name)
5 * Sadrul Habib Chowdhury (sadrul@users.sourceforge.net)
6 * Copyright (c) 1993-2002, 2003, 2005, 2006, 2007
7 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
8 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
9 * Copyright (c) 1987 Oliver Laumann
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3, or (at your option)
14 * any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program (see the file COPYING); if not, see
23 * https://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 ****************************************************************
29 #include "config.h"
31 #include "pty.h"
33 #include <sys/ioctl.h>
35 #include "screen.h"
37 #if defined(HAVE_PTY_H)
38 # include <pty.h>
39 #endif
40 #if defined(HAVE_UTIL_H)
41 # include <util.h>
42 #endif
43 #if defined(HAVE_LIBUTIL_H)
44 # include <libutil.h>
45 #endif
47 int pty_preopen = 0;
49 /***************************************************************/
51 int OpenPTY(char **ttyn)
53 static char TtyName[32];
54 int f, s;
56 if (openpty(&f, &s, TtyName, NULL, NULL) != 0)
57 return -1;
58 close(s);
60 tcflush(f, TCIOFLUSH);
61 #if defined(LOCKPTY) && defined(TIOCEXCL) && defined(TIOCNXCL)
62 (void)ioctl(f, TIOCEXCL, NULL);
63 #endif
65 pty_preopen = 1;
66 *ttyn = TtyName;
67 return f;
70 int ClosePTY(int fd)
72 #if defined(LOCKPTY) && defined(TIOCEXCL) && defined(TIOCNXCL)
73 (void)ioctl(fd, TIOCNXCL, NULL);
74 #endif
76 return close(fd);