ptsname.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* Copyright (C) 1998 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 <paths.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <sys/ioctl.h>
  21. #include <sys/stat.h>
  22. #include <sys/sysmacros.h>
  23. #include <termios.h>
  24. #include <unistd.h>
  25. /* Check if DEV corresponds to a master pseudo terminal device. */
  26. #define MASTER_P(Dev) \
  27. (major ((Dev)) == 2 \
  28. || (major ((Dev)) == 4 && minor ((Dev)) >= 128 && minor ((Dev)) < 192) \
  29. || (major ((Dev)) >= 128 && major ((Dev)) < 136))
  30. /* Check if DEV corresponds to a master pseudo terminal device. */
  31. #define SLAVE_P(Dev) \
  32. (major ((Dev)) == 3 \
  33. || (major ((Dev)) == 4 && minor ((Dev)) >= 192 && minor ((Dev)) < 256) \
  34. || (major ((Dev)) >= 136 && major ((Dev)) < 144))
  35. /* Note that major number 4 corresponds to the old BSD style pseudo
  36. terminal devices. As of Linux 2.1.115 these are no longer
  37. supported. They have been replaced by major numbers 2 (masters)
  38. and 3 (slaves). */
  39. /* Directory where we can find the slave pty nodes. */
  40. #define _PATH_DEVPTS "/dev/pts/"
  41. /* The are declared in getpt.c. */
  42. extern const char _ptyname1[];
  43. extern const char _ptyname2[];
  44. /* Static buffer for `ptsname'. */
  45. static char buffer[sizeof (_PATH_DEVPTS) + 20];
  46. /*
  47. extern char *
  48. _itoa_word (unsigned long value, char *buflim,
  49. unsigned int base, int upper_case);
  50. */
  51. /* Store at most BUFLEN characters of the pathname of the slave pseudo
  52. terminal associated with the master FD is open on in BUF.
  53. Return 0 on success, otherwise an error number. */
  54. int ptsname_r (int fd, char *buf, size_t buflen)
  55. {
  56. int save_errno = errno;
  57. struct stat st;
  58. int ptyno;
  59. if (buf == NULL)
  60. {
  61. errno = EINVAL;
  62. return EINVAL;
  63. }
  64. if (!isatty (fd))
  65. {
  66. errno = ENOTTY;
  67. return ENOTTY;
  68. }
  69. #if 0
  70. #ifdef TIOCGPTN
  71. if (ioctl (fd, TIOCGPTN, &ptyno) == 0)
  72. {
  73. /* Buffer we use to print the number in. For a maximum size for
  74. `int' of 8 bytes we never need more than 20 digits. */
  75. char numbuf[21];
  76. const char *devpts = _PATH_DEVPTS;
  77. const size_t devptslen = strlen (devpts);
  78. char *p;
  79. numbuf[20] = '\0';
  80. p = _itoa_word (ptyno, &numbuf[20], 10, 0);
  81. if (buflen < devptslen + strlen (p) + 1)
  82. {
  83. errno = ERANGE;
  84. return ERANGE;
  85. }
  86. strcpy (buf, devpts);
  87. strcat (buf, p);
  88. }
  89. else if (errno == EINVAL)
  90. #endif
  91. #endif
  92. {
  93. char *p;
  94. if (buflen < strlen (_PATH_TTY) + 3)
  95. {
  96. errno = ERANGE;
  97. return ERANGE;
  98. }
  99. if (fstat (fd, &st) < 0)
  100. return errno;
  101. /* Check if FD really is a master pseudo terminal. */
  102. if (! MASTER_P (st.st_rdev))
  103. {
  104. errno = ENOTTY;
  105. return ENOTTY;
  106. }
  107. ptyno = minor (st.st_rdev);
  108. /* This is for the old BSD pseudo terminals. As of Linux
  109. 2.1.115 these are no longer supported. */
  110. if (major (st.st_rdev) == 4)
  111. ptyno -= 128;
  112. if (ptyno / 16 >= strlen (_ptyname1))
  113. {
  114. errno = ENOTTY;
  115. return ENOTTY;
  116. }
  117. strcpy (buf, _PATH_TTY);
  118. p = buf + strlen (buf);
  119. p[0] = _ptyname1[ptyno / 16];
  120. p[1] = _ptyname2[ptyno % 16];
  121. p[2] = '\0';
  122. }
  123. if (__xstat (_STAT_VER, buf, &st) < 0)
  124. return errno;
  125. /* Check if the name we're about to return really corresponds to a
  126. slave pseudo terminal. */
  127. if (! S_ISCHR (st.st_mode) || ! SLAVE_P (st.st_rdev))
  128. {
  129. /* This really is a configuration problem. */
  130. errno = ENOTTY;
  131. return ENOTTY;
  132. }
  133. errno = save_errno;
  134. return 0;
  135. }
  136. /* Return the pathname of the pseudo terminal slave assoicated with
  137. the master FD is open on, or NULL on errors.
  138. The returned storage is good until the next call to this function. */
  139. char *
  140. ptsname (int fd)
  141. {
  142. return ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer;
  143. }