getpt.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Copyright (C) 1998, 1999 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 Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. 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. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If not,
  14. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. #include <errno.h>
  17. #include <fcntl.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <paths.h>
  21. #if !defined ASSUME_DEVPTS
  22. # include <sys/statfs.h>
  23. /* Constant that identifies the `devpts' filesystem. */
  24. # define DEVPTS_SUPER_MAGIC 0x1cd1
  25. /* Constant that identifies the `devfs' filesystem. */
  26. # define DEVFS_SUPER_MAGIC 0x1373
  27. #endif
  28. /* Path to the master pseudo terminal cloning device. */
  29. #define _PATH_DEVPTMX _PATH_DEV "ptmx"
  30. /* Directory containing the UNIX98 pseudo terminals. */
  31. #define _PATH_DEVPTS _PATH_DEV "pts"
  32. #if !defined UNIX98PTY_ONLY
  33. /* Prototype for function that opens BSD-style master pseudo-terminals. */
  34. int __bsd_getpt (void);
  35. #endif
  36. /* Open a master pseudo terminal and return its file descriptor. */
  37. int
  38. getpt (void)
  39. {
  40. #if !defined UNIX98PTY_ONLY
  41. static int have_no_dev_ptmx;
  42. #endif
  43. int fd;
  44. #if !defined UNIX98PTY_ONLY
  45. if (!have_no_dev_ptmx)
  46. #endif
  47. {
  48. fd = open (_PATH_DEVPTMX, O_RDWR);
  49. if (fd != -1)
  50. {
  51. #if defined ASSUME_DEVPTS
  52. return fd;
  53. #else
  54. struct statfs fsbuf;
  55. static int devpts_mounted;
  56. /* Check that the /dev/pts filesystem is mounted
  57. or if /dev is a devfs filesystem (this implies /dev/pts). */
  58. if (devpts_mounted
  59. || (statfs (_PATH_DEVPTS, &fsbuf) == 0
  60. && fsbuf.f_type == DEVPTS_SUPER_MAGIC)
  61. || (statfs (_PATH_DEV, &fsbuf) == 0
  62. && fsbuf.f_type == DEVFS_SUPER_MAGIC))
  63. {
  64. /* Everything is ok. */
  65. devpts_mounted = 1;
  66. return fd;
  67. }
  68. /* If /dev/pts is not mounted then the UNIX98 pseudo terminals
  69. are not usable. */
  70. close (fd);
  71. #if !defined UNIX98PTY_ONLY
  72. have_no_dev_ptmx = 1;
  73. #endif
  74. #endif
  75. }
  76. else
  77. {
  78. #if !defined UNIX98PTY_ONLY
  79. if (errno == ENOENT || errno == ENODEV)
  80. have_no_dev_ptmx = 1;
  81. else
  82. #endif
  83. return -1;
  84. }
  85. }
  86. #if !defined UNIX98PTY_ONLY
  87. return __bsd_getpt ();
  88. #endif
  89. }
  90. #if !defined UNIX98PTY_ONLY
  91. # define PTYNAME1 "pqrstuvwxyzabcde";
  92. # define PTYNAME2 "0123456789abcdef";
  93. # define __getpt __bsd_getpt
  94. # include "bsd_getpt.c"
  95. #endif