123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- #include <errno.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <unistd.h>
- #include <paths.h>
- #include <sys/statfs.h>
- extern __typeof(statfs) __libc_statfs;
- #if !defined __ASSUME_DEVPTS__
- # define DEVPTS_SUPER_MAGIC 0x1cd1
- # define DEVFS_SUPER_MAGIC 0x1373
- #endif
- #define _PATH_DEVPTMX _PATH_DEV "ptmx"
- #define _PATH_DEVPTS _PATH_DEV "pts"
- #if !defined __UNIX98PTY_ONLY__ && defined __UCLIBC_HAS_GETPT__
- static __inline__ int __bsd_getpt (void);
- #endif
- static int
- __posix_openpt (int flags)
- {
- #define have_no_dev_ptmx (1<<0)
- #define devpts_mounted (1<<1)
- #if !defined __UNIX98PTY_ONLY__
- static smallint _state;
- #endif
- int fd;
- #if !defined __UNIX98PTY_ONLY__
- if (!(_state & have_no_dev_ptmx))
- #endif
- {
- fd = open (_PATH_DEVPTMX, flags);
- if (fd != -1)
- {
- #if defined __ASSUME_DEVPTS__
- return fd;
- #else
- struct statfs fsbuf;
-
- if (
- #if !defined __UNIX98PTY_ONLY__
- (_state & devpts_mounted) ||
- #endif
- (__libc_statfs (_PATH_DEVPTS, &fsbuf) == 0
- && fsbuf.f_type == DEVPTS_SUPER_MAGIC)
- || (__libc_statfs (_PATH_DEV, &fsbuf) == 0
- && fsbuf.f_type == DEVFS_SUPER_MAGIC))
- {
-
- #if !defined __UNIX98PTY_ONLY__
- _state |= devpts_mounted;
- #endif
- return fd;
- }
-
- close (fd);
- #if !defined __UNIX98PTY_ONLY__
- _state |= have_no_dev_ptmx;
- #endif
- #endif
- }
- else
- {
- #if !defined __UNIX98PTY_ONLY__
- if (errno == ENOENT || errno == ENODEV)
- _state |= have_no_dev_ptmx;
- else
- #endif
- return -1;
- }
- }
- #if !defined __UNIX98PTY_ONLY__ && defined __UCLIBC_HAS_GETPT__
-
- if (_state & have_no_dev_ptmx)
- return __bsd_getpt();
- #endif
- return -1;
- }
- strong_alias(__posix_openpt,posix_openpt)
- #undef have_no_dev_ptmx
- #undef devpts_mounted
- #if defined __USE_GNU && defined __UCLIBC_HAS_GETPT__
- int getpt (void)
- {
- return __posix_openpt(O_RDWR);
- }
- #if !defined __UNIX98PTY_ONLY__ && defined __UCLIBC_HAS_GETPT__
- # define PTYNAME1 "pqrstuvwxyzabcde";
- # define PTYNAME2 "0123456789abcdef";
- # define __getpt __bsd_getpt
- # include "bsd_getpt.c"
- #endif
- #endif
|