1 .\"
   2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
   3 .\" permission to reproduce portions of its copyrighted documentation.
   4 .\" Original documentation from The Open Group can be obtained online at
   5 .\" http://www.opengroup.org/bookstore/.
   6 .\"
   7 .\" The Institute of Electrical and Electronics Engineers and The Open
   8 .\" Group, have given us permission to reprint portions of their
   9 .\" documentation.
  10 .\"
  11 .\" In the following statement, the phrase ``this text'' refers to portions
  12 .\" of the system documentation.
  13 .\"
  14 .\" Portions of this text are reprinted and reproduced in electronic form
  15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
  16 .\" Standard for Information Technology -- Portable Operating System
  17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
  18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
  19 .\" Engineers, Inc and The Open Group.  In the event of any discrepancy
  20 .\" between these versions and the original IEEE and The Open Group
  21 .\" Standard, the original IEEE and The Open Group Standard is the referee
  22 .\" document.  The original Standard can be obtained online at
  23 .\" http://www.opengroup.org/unix/online.html.
  24 .\"
  25 .\" This notice shall appear on any product containing this material.
  26 .\"
  27 .\" The contents of this file are subject to the terms of the
  28 .\" Common Development and Distribution License (the "License").
  29 .\" You may not use this file except in compliance with the License.
  30 .\"
  31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  32 .\" or http://www.opensolaris.org/os/licensing.
  33 .\" See the License for the specific language governing permissions
  34 .\" and limitations under the License.
  35 .\"
  36 .\" When distributing Covered Code, include this CDDL HEADER in each
  37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  38 .\" If applicable, add the following below this CDDL HEADER, with the
  39 .\" fields enclosed by brackets "[]" replaced with your own identifying
  40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
  41 .\"
  42 .\"
  43 .\" Copyright (c) 2001, The IEEE and The Open Group.  All Rights Reserved.
  44 .\" Portions Copyright (c) 2003, Sun Microsystems, Inc. All Rights Reserved.
  45 .\"
  46 .TH POSIX_OPENPT 3C "June 18, 2021"
  47 .SH NAME
  48 posix_openpt \- open a pseudo terminal device
  49 .SH SYNOPSIS
  50 .nf
  51 #include <stdlib.h>
  52 #include <fcntl.h>
  53 
  54 \fBint\fR \fBposix_openpt\fR(\fBint\fR \fIoflag\fR);
  55 .fi
  56 
  57 .SH DESCRIPTION
  58 The \fBposix_openpt()\fR function establishes a connection between a master
  59 device for a pseudo-terminal and a file descriptor. The file descriptor is used
  60 by other I/O functions that refer to that pseudo-terminal.
  61 .sp
  62 .LP
  63 The file status flags and file access modes of the open file description are
  64 set according to the value of \fIoflag\fR.
  65 .sp
  66 .LP
  67 Values for \fIoflag\fR are constructed by a bitwise-inclusive OR of flags from
  68 the following list, defined in <\fBfcntl.h\fR>.
  69 .sp
  70 .ne 2
  71 .na
  72 \fB\fBO_RDWR\fR\fR
  73 .ad
  74 .RS 12n
  75 Open for reading and writing.
  76 .RE
  77 
  78 .sp
  79 .ne 2
  80 .na
  81 \fB\fBO_NOCTTY\fR\fR
  82 .ad
  83 .RS 12n
  84 If set, \fBposix_openpt()\fR does not cause the terminal device to become the
  85 controlling terminal for the process.
  86 .RE
  87 
  88 .sp
  89 .LP
  90 The behavior of other values for the \fIoflag\fR argument is unspecified.
  91 .SH RETURN VALUES
  92 Upon successful completion, the \fBposix_openpt()\fR function opens a master
  93 pseudo-terminal device and returns a non-negative integer representing the
  94 lowest numbered unused file descriptor. Otherwise, -1 is returned and
  95 \fBerrno\fR is set to indicate the error.
  96 .SH ERRORS
  97 The \fBposix_openpt()\fR function will fail if:
  98 .sp
  99 .ne 2
 100 .na
 101 \fB\fBEMFILE\fR\fR
 102 .ad
 103 .RS 10n
 104 {\fBOPEN_MAX\fR} file descriptors are currently open in the calling process.
 105 .RE
 106 
 107 .sp
 108 .ne 2
 109 .na
 110 \fB\fBENFILE\fR\fR
 111 .ad
 112 .RS 10n
 113 The maximum allowable number of files is currently open in the system.
 114 .RE
 115 
 116 .sp
 117 .LP
 118 The \fBposix_openpt()\fR function may fail if:
 119 .sp
 120 .ne 2
 121 .na
 122 \fB\fBEINVAL\fR\fR
 123 .ad
 124 .RS 10n
 125 The value of \fIoflag\fR is not valid.
 126 .RE
 127 
 128 .sp
 129 .ne 2
 130 .na
 131 \fB\fBEAGAIN\fR\fR
 132 .ad
 133 .RS 10n
 134 Out of pseudo-terminal resources.
 135 .RE
 136 
 137 .sp
 138 .ne 2
 139 .na
 140 \fB\fBENOSR\fR\fR
 141 .ad
 142 .RS 10n
 143 Out of STREAMS resources.
 144 .RE
 145 
 146 .SH EXAMPLES
 147 \fBExample 1 \fROpen a pseudo-terminal.
 148 .sp
 149 .LP
 150 The following example opens a pseudo-terminal and returns the name of the slave
 151 device and a file descriptor.
 152 
 153 .sp
 154 .in +2
 155 .nf
 156 #include <fcntl.h>
 157 #include <stdio.h>
 158 
 159 int masterfd, slavefd;
 160 char *slavedevice;
 161 
 162 masterfd = posix_openpt(O_RDWR|O_NOCTTY);
 163 
 164 if (masterfd == -1
 165       || grantpt (masterfd) == -1
 166       || unlockpt (masterfd) == -1
 167       || (slavedevice = ptsname (masterfd)) == NULL)
 168       return -1;
 169 
 170 printf("slave device is: %s\en", slavedevice);
 171 
 172 slavefd = open(slave, O_RDWR|O_NOCTTY);
 173 if (slavefd < 0)
 174       return -1;
 175 .fi
 176 .in -2
 177 
 178 .SH USAGE
 179 This function provides a method for portably obtaining a file descriptor of a
 180 master terminal device for a pseudo-terminal. The \fBgrantpt\fR(3C) and
 181 \fBptsname\fR(3C) functions can be used to manipulate mode and ownership
 182 permissions and to obtain the name of the slave device, respectively.
 183 .SH ATTRIBUTES
 184 See \fBattributes\fR(5) for descriptions of the following attributes:
 185 .sp
 186 
 187 .sp
 188 .TS
 189 box;
 190 c | c
 191 l | l .
 192 ATTRIBUTE TYPE  ATTRIBUTE VALUE
 193 _
 194 Interface Stability     Standard
 195 _
 196 MT-Level        MT-Safe
 197 .TE
 198 
 199 .SH SEE ALSO
 200 \fBopen\fR(2), \fBgrantpt\fR(3C), \fBptsname\fR(3C), \fBunlockpt\fR(3C),
 201 \fBattributes\fR(5), \fBstandards\fR(5)