getpt.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #include <sys/statfs.h>
  22. /* Constant that identifies the `devpts' filesystem. */
  23. #define DEVPTS_SUPER_MAGIC 0x1cd1
  24. /* Constant that identifies the `devfs' filesystem. */
  25. #define DEVFS_SUPER_MAGIC 0x1373
  26. /* Path to the master pseudo terminal cloning device. */
  27. #define _PATH_DEVPTMX _PATH_DEV "ptmx"
  28. /* Directory containing the UNIX98 pseudo terminals. */
  29. #define _PATH_DEVPTS _PATH_DEV "pts"
  30. /* Prototype for function that opens BSD-style master pseudo-terminals. */
  31. int __bsd_getpt (void);
  32. /* Open a master pseudo terminal and return its file descriptor. */
  33. int
  34. getpt (void)
  35. {
  36. static int have_no_dev_ptmx;
  37. int fd;
  38. if (!have_no_dev_ptmx)
  39. {
  40. fd = open (_PATH_DEVPTMX, O_RDWR);
  41. if (fd != -1)
  42. {
  43. struct statfs fsbuf;
  44. static int devpts_mounted;
  45. /* Check that the /dev/pts filesystem is mounted
  46. or if /dev is a devfs filesystem (this implies /dev/pts). */
  47. if (devpts_mounted
  48. || (statfs (_PATH_DEVPTS, &fsbuf) == 0
  49. && fsbuf.f_type == DEVPTS_SUPER_MAGIC)
  50. || (statfs (_PATH_DEV, &fsbuf) == 0
  51. && fsbuf.f_type == DEVFS_SUPER_MAGIC))
  52. {
  53. /* Everything is ok. */
  54. devpts_mounted = 1;
  55. return fd;
  56. }
  57. /* If /dev/pts is not mounted then the UNIX98 pseudo terminals
  58. are not usable. */
  59. close (fd);
  60. have_no_dev_ptmx = 1;
  61. }
  62. else
  63. {
  64. if (errno == ENOENT || errno == ENODEV)
  65. have_no_dev_ptmx = 1;
  66. else
  67. return -1;
  68. }
  69. }
  70. return __bsd_getpt ();
  71. }
  72. #define PTYNAME1 "pqrstuvwxyzabcde";
  73. #define PTYNAME2 "0123456789abcdef";
  74. #define __getpt __bsd_getpt
  75. #include "bsd_getpt.c"