forked from abligh/pty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptyopen.c
72 lines (65 loc) · 1.34 KB
/
ptyopen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "apue.h"
#include <errno.h>
#include <fcntl.h>
#if defined(SOLARIS)
#include <stropts.h>
#endif
int
ptym_open (char *pts_name, int pts_namesz)
{
char *ptr;
int fdm, err;
if ((fdm = posix_openpt (O_RDWR)) < 0)
return (-1);
if (grantpt (fdm) < 0) /* grant access to slave */
goto errout;
if (unlockpt (fdm) < 0) /* clear slave's lock flag */
goto errout;
if ((ptr = ptsname (fdm)) == NULL) /* get slave's name */
goto errout;
/*
* Return name of slave. Null terminate to handle
* case where strlen(ptr) > pts_namesz.
*/
strncpy (pts_name, ptr, pts_namesz);
pts_name[pts_namesz - 1] = '\0';
return (fdm); /* return fd of master */
errout:
err = errno;
close (fdm);
errno = err;
return (-1);
}
int
ptys_open (char *pts_name)
{
int fds;
#if defined(SOLARIS)
int err, setup;
#endif
if ((fds = open (pts_name, O_RDWR)) < 0)
return (-1);
#if defined(SOLARIS)
/*
* Check if stream is already set up by autopush facility.
*/
if ((setup = ioctl (fds, I_FIND, "ldterm")) < 0)
goto errout;
if (setup == 0)
{
if (ioctl (fds, I_PUSH, "ptem") < 0)
goto errout;
if (ioctl (fds, I_PUSH, "ldterm") < 0)
goto errout;
if (ioctl (fds, I_PUSH, "ttcompat") < 0)
{
errout:
err = errno;
close (fds);
errno = err;
return (-1);
}
}
#endif
return (fds);
}