getpt.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <stdlib.h>
  18. #include <stdbool.h>
  19. #include <unistd.h>
  20. #include <paths.h>
  21. #include <sys/statfs.h>
  22. extern __typeof(statfs) __libc_statfs;
  23. #if !defined __ASSUME_DEVPTS__
  24. /* Constant that identifies the `devpts' filesystem. */
  25. # define DEVPTS_SUPER_MAGIC 0x1cd1
  26. /* Constant that identifies the `devfs' filesystem. */
  27. # define DEVFS_SUPER_MAGIC 0x1373
  28. #endif
  29. /* Path to the master pseudo terminal cloning device. */
  30. #define _PATH_DEVPTMX _PATH_DEV "ptmx"
  31. /* Directory containing the UNIX98 pseudo terminals. */
  32. #define _PATH_DEVPTS _PATH_DEV "pts"
  33. #if !defined __UNIX98PTY_ONLY__ && defined __UCLIBC_HAS_GETPT__
  34. /* Prototype for function that opens BSD-style master pseudo-terminals. */
  35. static __inline__ int __bsd_getpt (void);
  36. #endif
  37. /* Open a master pseudo terminal and return its file descriptor. */
  38. static int
  39. __posix_openpt (int flags)
  40. {
  41. #define have_no_dev_ptmx (1<<0)
  42. #define devpts_mounted (1<<1)
  43. #if !defined __UNIX98PTY_ONLY__
  44. static smallint _state;
  45. #endif
  46. int fd;
  47. #if !defined __UNIX98PTY_ONLY__
  48. if (!(_state & have_no_dev_ptmx))
  49. #endif
  50. {
  51. fd = open (_PATH_DEVPTMX, flags);
  52. if (fd != -1)
  53. {
  54. #if defined __ASSUME_DEVPTS__
  55. return fd;
  56. #else
  57. struct statfs fsbuf;
  58. /* Check that the /dev/pts filesystem is mounted
  59. or if /dev is a devfs filesystem (this implies /dev/pts). */
  60. if (
  61. #if !defined __UNIX98PTY_ONLY__
  62. (_state & devpts_mounted) ||
  63. #endif
  64. (__libc_statfs (_PATH_DEVPTS, &fsbuf) == 0
  65. && fsbuf.f_type == DEVPTS_SUPER_MAGIC)
  66. || (__libc_statfs (_PATH_DEV, &fsbuf) == 0
  67. && fsbuf.f_type == DEVFS_SUPER_MAGIC))
  68. {
  69. /* Everything is ok. */
  70. #if !defined __UNIX98PTY_ONLY__
  71. _state |= devpts_mounted;
  72. #endif
  73. return fd;
  74. }
  75. /* If /dev/pts is not mounted then the UNIX98 pseudo terminals
  76. are not usable. */
  77. close (fd);
  78. #if !defined __UNIX98PTY_ONLY__
  79. _state |= have_no_dev_ptmx;
  80. #endif
  81. #endif
  82. }
  83. else
  84. {
  85. #if !defined __UNIX98PTY_ONLY__
  86. if (errno == ENOENT || errno == ENODEV)
  87. _state |= have_no_dev_ptmx;
  88. else
  89. #endif
  90. return -1;
  91. }
  92. }
  93. #if !defined __UNIX98PTY_ONLY__ && defined __UCLIBC_HAS_GETPT__
  94. /* If we have no ptmx then ignore flags and use the fallback. */
  95. if (_state & have_no_dev_ptmx)
  96. return __bsd_getpt();
  97. #endif
  98. return -1;
  99. }
  100. strong_alias(__posix_openpt,posix_openpt)
  101. #undef have_no_dev_ptmx
  102. #undef devpts_mounted
  103. #if defined __USE_GNU && defined __UCLIBC_HAS_GETPT__
  104. int getpt (void)
  105. {
  106. return __posix_openpt(O_RDWR);
  107. }
  108. #if !defined __UNIX98PTY_ONLY__ && defined __UCLIBC_HAS_GETPT__
  109. # define PTYNAME1 "pqrstuvwxyzabcde";
  110. # define PTYNAME2 "0123456789abcdef";
  111. # define __getpt __bsd_getpt
  112. # include "bsd_getpt.c"
  113. #endif
  114. #endif /* GNU && __UCLIBC_HAS_GETPT__ */