diff --git a/include/stdlib.h b/include/stdlib.h index 81e939670d..745f4286b5 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -223,6 +223,7 @@ int posix_memalign(FAR void **, size_t, size_t); /* Pseudo-Terminals */ #ifdef CONFIG_PSEUDOTERM +int posix_openpt(int oflag); FAR char *ptsname(int fd); int ptsname_r(int fd, FAR char *buf, size_t buflen); int unlockpt(int fd); diff --git a/libs/libc/stdlib/lib_openpty.c b/libs/libc/stdlib/lib_openpty.c index e0332105c8..9b8e9bfe45 100644 --- a/libs/libc/stdlib/lib_openpty.c +++ b/libs/libc/stdlib/lib_openpty.c @@ -32,13 +32,43 @@ #include /**************************************************************************** - * Private Functions + * Public Functions ****************************************************************************/ -static int openmaster(void) +/**************************************************************************** + * Name: posix_openpt + * + * Description: + * The posix_openpt() function establish 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 + * 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 : + * + * O_RDWR + * Open for reading and writing. + * O_NOCTTY + * If set posix_openpt() shall not cause the terminal device to become + * the controlling terminal for the process. + * + * The behavior of other values for the oflag argument is unspecified. + * + * Returned Value: + * Upon successful completion, the posix_openpt() function shall open + * a master pseudo-terminal device and return a non-negative integer + * representing the lowest numbered unused file descriptor. Otherwise, + * -1 shall be returned and errno set to indicate the error. + * + ****************************************************************************/ + +int posix_openpt(int oflag) { #ifdef CONFIG_PSEUDOTERM_SUSV1 - return open("dev/ptmx", O_RDWR); + return open("dev/ptmx", oflag); #else int minor; @@ -48,13 +78,13 @@ static int openmaster(void) int fd; snprintf(devname, 16, "/dev/pty%d", minor); - fd = open(devname, O_RDWR); + fd = open(devname, oflag); if (fd < 0) { /* Fail, register and try again */ pty_register(minor); - fd = open(devname, O_RDWR); + fd = open(devname, oflag); } if (fd >= 0) @@ -67,10 +97,6 @@ static int openmaster(void) #endif } -/**************************************************************************** - * Public Functions - ****************************************************************************/ - /**************************************************************************** * Name: openpty * @@ -83,8 +109,8 @@ static int openmaster(void) * the slave will be set to the values in win. * * Returned Value: - * If a call to openpty() is not successful, -1 is returned and - * errno is set to indicate the error. Otherwise, return 0. + * If a call to openpty() is not successful, -1 is returned and + * errno is set to indicate the error. Otherwise, return 0. * ****************************************************************************/ @@ -96,7 +122,7 @@ int openpty(FAR int *master, FAR int *slave, FAR char *name, /* Open the pseudo terminal master */ - ret = openmaster(); + ret = posix_openpt(O_RDWR); if (ret < 0) { return ret;