1posix_openpt(3C) Standard C Library Functions posix_openpt(3C)
2
3
4
6 posix_openpt - open a pseudo terminal device
7
9 #include <stdlib.h>
10 #include <fcntl.h>
11
12 int posix_openpt(int oflag);
13
14
16 The posix_openpt() function establishes a connection between a master
17 device for a pseudo-terminal and a file descriptor. The file descriptor
18 is used by other I/O functions that refer to that pseudo-terminal.
19
20
21 The file status flags and file access modes of the open file descrip‐
22 tion are set according to the value of oflag.
23
24
25 Values for oflag are constructed by a bitwise-inclusive OR of flags
26 from the following list, defined in <fcntl.h>.
27
28 O_RDWR Open for reading and writing.
29
30
31 O_NOCTTY If set, posix_openpt() does not cause the terminal device
32 to become the controlling terminal for the process.
33
34
35
36 The behavior of other values for the oflag argument is unspecified.
37
39 Upon successful completion, the posix_openpt() function opens a master
40 pseudo-terminal device and returns a non-negative integer representing
41 the lowest numbered unused file descriptor. Otherwise, -1 is returned
42 and errno is set to indicate the error.
43
45 The posix_openpt() function will fail if:
46
47 EMFILE {OPEN_MAX} file descriptors are currently open in the calling
48 process.
49
50
51 ENFILE The maximum allowable number of files is currently open in
52 the system.
53
54
55
56 The posix_openpt() function may fail if:
57
58 EINVAL The value of oflag is not valid.
59
60
61 EAGAIN Out of pseudo-terminal resources.
62
63
64 ENOSR Out of STREAMS resources.
65
66
68 Example 1 Open a pseudo-terminal.
69
70
71 The following example opens a pseudo-terminal and returns the name of
72 the slave device and a file descriptor.
73
74
75 #include fcntl.h>
76 #include stdio.h>
77
78 int masterfd, slavefd;
79 char *slavedevice;
80
81 masterfd = posix_openpt(O_RDWR|O_NOCTTY);
82
83 if (masterfd == -1
84 || grantpt (masterfd) == -1
85 || unlockpt (masterfd) == -1
86 || (slavedevice = ptsname (masterfd)) == NULL)
87 return -1;
88
89 printf("slave device is: %s\n", slavedevice);
90
91 slavefd = open(slave, O_RDWR|O_NOCTTY);
92 if (slavefd < 0)
93 return -1;
94
95
97 This function provides a method for portably obtaining a file descrip‐
98 tor of a master terminal device for a pseudo-terminal. The grantpt(3C)
99 and ptsname(3C) functions can be used to manipulate mode and ownership
100 permissions and to obtain the name of the slave device, respectively.
101
103 See attributes(5) for descriptions of the following attributes:
104
105
106
107
108 ┌─────────────────────────────┬─────────────────────────────┐
109 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
110 ├─────────────────────────────┼─────────────────────────────┤
111 │Interface Stability │Standard │
112 ├─────────────────────────────┼─────────────────────────────┤
113 │MT-Level │MT-Safe │
114 └─────────────────────────────┴─────────────────────────────┘
115
117 open(2), grantpt(3C), ptsname(3C), unlockpt(3C), attributes(5), stan‐
118 dards(5)
119
120
121
122SunOS 5.11 1 Dec 2003 posix_openpt(3C)