2
* pty_getpty: open a PTY master.
4
* Copyright 1995, 1996 by the Massachusetts Institute of Technology.
7
* Permission to use, copy, modify, and distribute this software and
8
* its documentation for any purpose and without fee is hereby
9
* granted, provided that the above copyright notice appear in all
10
* copies and that both that copyright notice and this permission
11
* notice appear in supporting documentation, and that the name of
12
* M.I.T. not be used in advertising or publicity pertaining to
13
* distribution of the software without specific, written prior
14
* permission. Furthermore if you modify this software you must label
15
* your software as modified software and not distribute it in such a
16
* fashion that it might be confused with the original M.I.T. software.
17
* M.I.T. makes no representations about the suitability
18
* of this software for any purpose. It is provided "as is" without
19
* express or implied warranty.
28
ptyint_getpty_ext(int *fd, char *slave, int slavelength, int do_grantpt)
30
#if !defined(HAVE__GETPTY) && !defined(HAVE_OPENPTY)
39
char *slaveret; /*Temporary to hold pointer to slave*/
40
#endif /*HAVE__GETPTY*/
45
if(openpty(fd, &slavefd, slave, (struct termios *) 0,
46
(struct winsize *) 0)) return 1;
49
#else /*HAVE_OPENPTY*/
51
/* This code is included for Irix; as of version 5.3, Irix has /dev/ptmx,
52
* but it fails to work properly; even after calling unlockpt,
53
* root gets permission denied opening the pty.
54
* The code to support _getpty should be removed if Irix gets working
55
* streams ptys in favor of maintaining the least needed code
58
if ((slaveret = _getpty(fd, O_RDWR|O_NDELAY, 0600, 0)) == 0) {
60
return PTY_GETPTY_NOPTY;
62
if (strlen(slaveret) > slavelength - 1) {
65
return PTY_GETPTY_SLAVE_TOOLONG;
67
else strcpy(slave, slaveret);
69
#else /*HAVE__GETPTY*/
71
*fd = open("/dev/ptym/clone", O_RDWR|O_NDELAY); /* HPUX*/
73
if (*fd < 0) *fd = open("/dev/ptmx",O_RDWR|O_NDELAY); /*Solaris*/
75
if (*fd < 0) *fd = open("/dev/ptc", O_RDWR|O_NDELAY); /* AIX */
76
if (*fd < 0) *fd = open("/dev/pty", O_RDWR|O_NDELAY); /* sysvimp */
80
#if defined(HAVE_GRANTPT)&&defined(HAVE_STREAMS)
82
if (grantpt(*fd) || unlockpt(*fd)) return PTY_GETPTY_STREAMS;
91
/* XXX If we don't have either what do we do */
95
if (strlen(p) > slavelength - 1) {
98
return PTY_GETPTY_SLAVE_TOOLONG;
104
if (fstat(*fd, &stb) < 0) {
106
return PTY_GETPTY_FSTAT;
108
ptynum = (int)(stb.st_rdev&0xFF);
109
sprintf(slavebuf, "/dev/ttyp%x", ptynum);
110
if (strlen(slavebuf) > slavelength - 1) {
113
return PTY_GETPTY_SLAVE_TOOLONG;
115
strncpy(slave, slavebuf, slavelength);
118
for (cp = "pqrstuvwxyzPQRST";*cp; cp++) {
119
sprintf(slavebuf,"/dev/ptyXX");
120
slavebuf[sizeof("/dev/pty") - 1] = *cp;
121
slavebuf[sizeof("/dev/ptyp") - 1] = '0';
122
if (stat(slavebuf, &stb) < 0)
124
for (i = 0; i < 16; i++) {
125
slavebuf[sizeof("/dev/ptyp") - 1] = "0123456789abcdef"[i];
126
*fd = open(slavebuf, O_RDWR);
127
if (*fd < 0) continue;
130
slavebuf[sizeof("/dev/") - 1] = 't';
131
if (strlen(slavebuf) > slavelength -1) {
134
return PTY_GETPTY_SLAVE_TOOLONG;
136
strncpy(slave, slavebuf, slavelength);
140
return PTY_GETPTY_NOPTY;
142
#endif /*HAVE__GETPTY*/
143
#endif /* HAVE_OPENPTY */
147
pty_getpty(int *fd, char *slave, int slavelength)
149
return ptyint_getpty_ext(fd, slave, slavelength, 1);