getpt.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Copyright (C) 1998-2019 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 <unistd.h>
  19. #include <paths.h>
  20. #include <sys/statfs.h>
  21. #include "linux_fsinfo.h"
  22. /* Path to the master pseudo terminal cloning device. */
  23. #define _PATH_DEVPTMX _PATH_DEV "ptmx"
  24. /* Directory containing the UNIX98 pseudo terminals. */
  25. #define _PATH_DEVPTS _PATH_DEV "pts"
  26. /* Prototype for function that opens BSD-style master pseudo-terminals. */
  27. extern int __bsd_getpt (void) attribute_hidden;
  28. /* Open a master pseudo terminal and return its file descriptor. */
  29. int
  30. __posix_openpt (int oflag)
  31. {
  32. static int have_no_dev_ptmx;
  33. int fd;
  34. if (!have_no_dev_ptmx)
  35. {
  36. fd = __open (_PATH_DEVPTMX, oflag);
  37. if (fd != -1)
  38. {
  39. struct statfs fsbuf;
  40. static int devpts_mounted;
  41. /* Check that the /dev/pts filesystem is mounted
  42. or if /dev is a devfs filesystem (this implies /dev/pts). */
  43. if (devpts_mounted
  44. || (__statfs (_PATH_DEVPTS, &fsbuf) == 0
  45. && fsbuf.f_type == DEVPTS_SUPER_MAGIC)
  46. || (__statfs (_PATH_DEV, &fsbuf) == 0
  47. && fsbuf.f_type == DEVFS_SUPER_MAGIC))
  48. {
  49. /* Everything is ok. */
  50. devpts_mounted = 1;
  51. return fd;
  52. }
  53. /* If /dev/pts is not mounted then the UNIX98 pseudo terminals
  54. are not usable. */
  55. __close (fd);
  56. have_no_dev_ptmx = 1;
  57. __set_errno (ENOENT);
  58. }
  59. else
  60. {
  61. if (errno == ENOENT || errno == ENODEV)
  62. have_no_dev_ptmx = 1;
  63. else
  64. return -1;
  65. }
  66. }
  67. else
  68. __set_errno (ENOENT);
  69. return -1;
  70. }
  71. weak_alias (__posix_openpt, posix_openpt)
  72. int
  73. __getpt (void)
  74. {
  75. int fd = __posix_openpt (O_RDWR);
  76. if (fd == -1)
  77. fd = __bsd_getpt ();
  78. return fd;
  79. }
  80. #define PTYNAME1 "pqrstuvwxyzabcde";
  81. #define PTYNAME2 "0123456789abcdef";
  82. #define __getpt __bsd_getpt
  83. #define HAVE_POSIX_OPENPT
  84. #include <sysdeps/unix/bsd/getpt.c>