getpt.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #define _GNU_SOURCE
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <paths.h>
  22. libc_hidden_proto(open)
  23. libc_hidden_proto(close)
  24. #if !defined __ASSUME_DEVPTS__
  25. # include <sys/statfs.h>
  26. /* Constant that identifies the `devpts' filesystem. */
  27. # define DEVPTS_SUPER_MAGIC 0x1cd1
  28. /* Constant that identifies the `devfs' filesystem. */
  29. # define DEVFS_SUPER_MAGIC 0x1373
  30. #endif
  31. /* Path to the master pseudo terminal cloning device. */
  32. #define _PATH_DEVPTMX _PATH_DEV "ptmx"
  33. /* Directory containing the UNIX98 pseudo terminals. */
  34. #define _PATH_DEVPTS _PATH_DEV "pts"
  35. #if !defined __UNIX98PTY_ONLY__
  36. /* Prototype for function that opens BSD-style master pseudo-terminals. */
  37. extern int __bsd_getpt (void) attribute_hidden;
  38. #endif
  39. /* Open a master pseudo terminal and return its file descriptor. */
  40. int
  41. getpt (void)
  42. {
  43. #if !defined __UNIX98PTY_ONLY__
  44. static int have_no_dev_ptmx;
  45. #endif
  46. int fd;
  47. #if !defined __UNIX98PTY_ONLY__
  48. if (!have_no_dev_ptmx)
  49. #endif
  50. {
  51. fd = open (_PATH_DEVPTMX, O_RDWR);
  52. if (fd != -1)
  53. {
  54. #if defined __ASSUME_DEVPTS__
  55. return fd;
  56. #else
  57. struct statfs fsbuf;
  58. static int devpts_mounted;
  59. /* Check that the /dev/pts filesystem is mounted
  60. or if /dev is a devfs filesystem (this implies /dev/pts). */
  61. if (devpts_mounted
  62. || (statfs (_PATH_DEVPTS, &fsbuf) == 0
  63. && fsbuf.f_type == DEVPTS_SUPER_MAGIC)
  64. || (statfs (_PATH_DEV, &fsbuf) == 0
  65. && fsbuf.f_type == DEVFS_SUPER_MAGIC))
  66. {
  67. /* Everything is ok. */
  68. devpts_mounted = 1;
  69. return fd;
  70. }
  71. /* If /dev/pts is not mounted then the UNIX98 pseudo terminals
  72. are not usable. */
  73. close (fd);
  74. #if !defined __UNIX98PTY_ONLY__
  75. have_no_dev_ptmx = 1;
  76. #endif
  77. #endif
  78. }
  79. else
  80. {
  81. #if !defined __UNIX98PTY_ONLY__
  82. if (errno == ENOENT || errno == ENODEV)
  83. have_no_dev_ptmx = 1;
  84. else
  85. #endif
  86. return -1;
  87. }
  88. }
  89. #if !defined __UNIX98PTY_ONLY__
  90. return __bsd_getpt ();
  91. #endif
  92. }
  93. #if !defined __UNIX98PTY_ONLY__
  94. # define PTYNAME1 "pqrstuvwxyzabcde";
  95. # define PTYNAME2 "0123456789abcdef";
  96. # define __getpt __bsd_getpt
  97. # include "bsd_getpt.c"
  98. #endif