POSIX_OPENPT(2)

πŸ’– POSIX_OPENPT(2) πŸ’–

okay bestie, so this one lives in the System Calls Manual section of the FreeBSD/Debian man pages, and it's literally the cutest way to spin up a pseudo-terminal. grab a strawberry drink πŸ“ and let's get into it.


✨ NAME ✨

posix_openpt β€” open a pseudo-terminal device πŸŽ€

πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc) β€” yeah she's built in, no extra linking drama πŸ’…

🌸 SYNOPSIS 🌸

#include <fcntl.h>
#include <stdlib.h>

int
posix_openpt(int oflag);

πŸ‘‘ DESCRIPTION πŸ‘‘

So the way that posix_openpt() works is: it allocates a brand new pseudo-terminal and establishes a connection with its master device. slay. and a slave device shall be created in /dev/pts. πŸ’–

after the pseudo-terminal has been allocated, the slave device should have the proper permissions before it can be used β€” check grantpt(3) for that part, bestie. and you can figure out the name of the slave device by calling ptsname(3). no cap.

The file status flags and file access modes of the open file description shall be 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. both. iconic.
  • O_NOCTTY β€” if set, posix_openpt() shall not cause the terminal device to become the controlling terminal for the process. it's giving "i'm just here, not trying to run your life."
  • O_CLOEXEC β€” set the close-on-exec flag for the new file descriptor. so it cleans itself up on exec. we love a self-sufficient queen 🧚
⚠️ the posix_openpt() function shall fail when oflag contains other values. do not pass her random flags, she will not be okay with it. πŸ’€

🫢 RETURN VALUES 🫢

Upon successful completion, the posix_openpt() function shall allocate a new pseudo-terminal device and return a non-negative integer representing a file descriptor, which is connected to its master device. πŸŽ‰

Otherwise, -1 shall be returned and errno set to indicate the error. sad girl hours, but it's honest.

πŸ”₯ ERRORS πŸ”₯

The posix_openpt() function shall fail if:

  • [ENFILE] β€” the system file table is full. 😭
  • [EINVAL] β€” the value of oflag is not valid.
  • [EAGAIN] β€” out of pseudo-terminal resources. no ptys left, bestie, try again later.

🌈 SEE ALSO 🌈

ptsname(3), pts(4), tty(4) β€” go read these next, they're the supporting cast and they ate. ⭐

πŸ’œ STANDARDS πŸ’œ

The posix_openpt() function conforms to IEEE Std 1003.1-2001 ("POSIX.1"). and heads up: the ability to use O_CLOEXEC is an extension to the standard. so it's the bonus track, not the main album. 🎡

πŸ¦‹ HISTORY πŸ¦‹

The posix_openpt() function appeared in FreeBSD 5.0. and in FreeBSD 8.0, this function was changed to a system call. glow-up era. ✨

πŸ™ˆ NOTES πŸ™ˆ

The flag O_NOCTTY is included for compatibility; in FreeBSD, opening a terminal does not cause it to become a process's controlling terminal. so it's lowkey decorative there, but it's there for your cross-platform bestie needs. 🫢

πŸ‘‘ AUTHORS πŸ‘‘

Ed Schouten <ed@FreeBSD.org> β€” thank you king πŸ’…


and that's the whole page, fr fr. this one's from the Debian crew, dated September 21, 2016. go open some pseudo-terminals, bestie, you're literally the main character of the tty world now πŸŽ€πŸ’–βœ¨

πŸ“¦ where this page comes from