posix_openpt() — Open a pseudo-terminal device

Standards

Standards / Extensions C or C++ Dependencies
Single UNIX Specification, Version 3
both z/OS® V1.9

Format

#define _XOPEN_SOURCE 600 
#include <stdlib.h>
#include <fcntl.h>

int posix_openpt(int oflag);

General description

The posix_openpt() function establishes a connection between a master device for a pseudo-terminal and a file descriptor. The file descriptor is used by other I/O functions that refer to that pseudo-terminal.

The file status flags and file access modes of the open file description are set according to the value of oflag.

Values for oflag are constructed by a bitwise-inclusive OR of flags from the following list, defined in <fcntl.h>:

O_RDWR
Open for reading and writing.
O_NOCTTY
If set posix_openpt() will not cause the terminal device to become the controlling terminal for the process.

The behavior of other values for the oflag argument is unspecified.

Argument
Description
oflag
The value of the file status flags and file access modes |of the open file description.

Returned value

Upon successful completion, the posix_openpt() function opens a master pseudo-terminal device and returns a non-negative integer representing the lowest numbered unused file descriptor. Otherwise, -1 is returned and errno set to indicate the error.
Error Code
Description
EMFILE
{OPEN_MAX} file descriptors are currently open in the calling process.
ENFILE
The maximum allowable number of files is currently open in the system.
EINVAL
The value of oflag is not valid.
EAGAIN
Out of pseudo-terminal resources.

Example

CELEBP71
/* CELEBP71

   This example demonstrates how to use posix_openpt() to open a
   master psuedo-terminal device.

   Expected output:
   The master psuedo-terminal id is [first available descriptor]

*/
#define _XOPEN_SOURCE 600
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>

void main() {
   int fd;

   fd = posix_openpt(O_RDWR | O_NOCTTY);
   if (fd == -1)
      perror("Error opening a terminal.\n");
   else
      printf("The master psuedo-terminal id is %d\n",fd);
}

Related information