getpt.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <errno.h>
  17. #include <fcntl.h>
  18. #include <stdlib.h>
  19. #include <stdbool.h>
  20. #include <unistd.h>
  21. #include <paths.h>
  22. #include <sys/statfs.h>
  23. extern __typeof(statfs) __libc_statfs;
  24. libc_hidden_proto(__libc_statfs)
  25. libc_hidden_proto(open)
  26. libc_hidden_proto(close)
  27. #if !defined __ASSUME_DEVPTS__
  28. /* Constant that identifies the `devpts' filesystem. */
  29. # define DEVPTS_SUPER_MAGIC 0x1cd1
  30. /* Constant that identifies the `devfs' filesystem. */
  31. # define DEVFS_SUPER_MAGIC 0x1373
  32. #endif
  33. /* Path to the master pseudo terminal cloning device. */
  34. #define _PATH_DEVPTMX _PATH_DEV "ptmx"
  35. /* Directory containing the UNIX98 pseudo terminals. */
  36. #define _PATH_DEVPTS _PATH_DEV "pts"
  37. #if !defined __UNIX98PTY_ONLY__ && defined __UCLIBC_HAS_GETPT__
  38. /* Prototype for function that opens BSD-style master pseudo-terminals. */
  39. extern int __bsd_getpt (void) attribute_hidden;
  40. #endif
  41. /* Open a master pseudo terminal and return its file descriptor. */
  42. int
  43. posix_openpt (int flags)
  44. {
  45. #define have_no_dev_ptmx (1<<0)
  46. #define devpts_mounted (1<<1)
  47. #if !defined __UNIX98PTY_ONLY__
  48. static smallint _state;
  49. #endif
  50. int fd;
  51. #if !defined __UNIX98PTY_ONLY__
  52. if (!(_state & have_no_dev_ptmx))
  53. #endif
  54. {
  55. fd = open (_PATH_DEVPTMX, flags);
  56. if (fd != -1)
  57. {
  58. #if defined __ASSUME_DEVPTS__
  59. return fd;
  60. #else
  61. struct statfs fsbuf;
  62. /* Check that the /dev/pts filesystem is mounted
  63. or if /dev is a devfs filesystem (this implies /dev/pts). */
  64. if (
  65. #if !defined __UNIX98PTY_ONLY__
  66. (_state & devpts_mounted) ||
  67. #endif
  68. (__libc_statfs (_PATH_DEVPTS, &fsbuf) == 0
  69. && fsbuf.f_type == DEVPTS_SUPER_MAGIC)
  70. || (__libc_statfs (_PATH_DEV, &fsbuf) == 0
  71. && fsbuf.f_type == DEVFS_SUPER_MAGIC))
  72. {
  73. /* Everything is ok. */
  74. #if !defined __UNIX98PTY_ONLY__
  75. _state |= devpts_mounted;
  76. #endif
  77. return fd;
  78. }
  79. /* If /dev/pts is not mounted then the UNIX98 pseudo terminals
  80. are not usable. */
  81. close (fd);
  82. #if !defined __UNIX98PTY_ONLY__
  83. _state |= have_no_dev_ptmx;
  84. #endif
  85. #endif
  86. }
  87. else
  88. {
  89. #if !defined __UNIX98PTY_ONLY__
  90. if (errno == ENOENT || errno == ENODEV)
  91. _state |= have_no_dev_ptmx;
  92. else
  93. #endif
  94. return -1;
  95. }
  96. }
  97. return -1;
  98. }
  99. libc_hidden_def(posix_openpt)
  100. #undef have_no_dev_ptmx
  101. #undef devpts_mounted
  102. #if defined __USE_GNU && defined __UCLIBC_HAS_GETPT__
  103. int
  104. getpt (void)
  105. {
  106. int fd = posix_openpt(O_RDWR);
  107. #if !defined __UNIX98PTY_ONLY__
  108. if (fd == -1)
  109. fd = __bsd_getpt();
  110. #endif
  111. return fd;
  112. }
  113. #if !defined __UNIX98PTY_ONLY__
  114. # define PTYNAME1 "pqrstuvwxyzabcde";
  115. # define PTYNAME2 "0123456789abcdef";
  116. # define __getpt __bsd_getpt
  117. # include "bsd_getpt.c"
  118. #endif
  119. #endif /* GNU && __UCLIBC_HAS_GETPT__ */