ptsname.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #define isatty __isatty
  17. #define _ISOC99_SOURCE
  18. #include <stdio.h>
  19. #include <errno.h>
  20. #include <paths.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/ioctl.h>
  24. #include <sys/stat.h>
  25. #include <sys/sysmacros.h>
  26. #include <termios.h>
  27. #include <unistd.h>
  28. #include <bits/uClibc_uintmaxtostr.h>
  29. #if !defined __UNIX98PTY_ONLY__
  30. /* Check if DEV corresponds to a master pseudo terminal device. */
  31. #define MASTER_P(Dev) \
  32. (major ((Dev)) == 2 \
  33. || (major ((Dev)) == 4 && minor ((Dev)) >= 128 && minor ((Dev)) < 192) \
  34. || (major ((Dev)) >= 128 && major ((Dev)) < 136))
  35. /* Check if DEV corresponds to a master pseudo terminal device. */
  36. #define SLAVE_P(Dev) \
  37. (major ((Dev)) == 3 \
  38. || (major ((Dev)) == 4 && minor ((Dev)) >= 192 && minor ((Dev)) < 256) \
  39. || (major ((Dev)) >= 136 && major ((Dev)) < 144))
  40. /* Note that major number 4 corresponds to the old BSD style pseudo
  41. terminal devices. As of Linux 2.1.115 these are no longer
  42. supported. They have been replaced by major numbers 2 (masters)
  43. and 3 (slaves). */
  44. /* The are declared in getpt.c. */
  45. extern const char _ptyname1[];
  46. extern const char _ptyname2[];
  47. #endif
  48. /* Directory where we can find the slave pty nodes. */
  49. #define _PATH_DEVPTS "/dev/pts/"
  50. /* Store at most BUFLEN characters of the pathname of the slave pseudo
  51. terminal associated with the master FD is open on in BUF.
  52. Return 0 on success, otherwise an error number. */
  53. int attribute_hidden __ptsname_r (int fd, char *buf, size_t buflen)
  54. {
  55. int save_errno = errno;
  56. #if !defined __UNIX98PTY_ONLY__
  57. struct stat st;
  58. #endif
  59. int ptyno;
  60. if (buf == NULL)
  61. {
  62. errno = EINVAL;
  63. return EINVAL;
  64. }
  65. #if !defined __UNIX98PTY_ONLY__
  66. if (!isatty (fd))
  67. {
  68. errno = ENOTTY;
  69. return ENOTTY;
  70. }
  71. #elif !defined TIOCGPTN
  72. # error "__UNIX98PTY_ONLY__ enabled but TIOCGPTN ioctl not supported by your kernel."
  73. #endif
  74. #ifdef TIOCGPTN
  75. if (__ioctl (fd, TIOCGPTN, &ptyno) == 0)
  76. {
  77. /* Buffer we use to print the number in. */
  78. char numbuf[__BUFLEN_INT10TOSTR];
  79. static const char devpts[] = _PATH_DEVPTS;
  80. char *p;
  81. p = _int10tostr(&numbuf[sizeof numbuf - 1], ptyno);
  82. if (buflen < sizeof devpts + &numbuf[sizeof numbuf - 1] - p)
  83. {
  84. errno = ERANGE;
  85. return ERANGE;
  86. }
  87. __strcpy (buf, devpts);
  88. __strcat (buf, p);
  89. /* Note: Don't bother with stat on the slave name and checking the
  90. driver's major device number - the ioctl above succeeded so
  91. we know the fd was a Unix'98 master and the /dev/pts/ prefix
  92. is set by definition. If the name isn't really a slave PTY,
  93. the system is misconfigured anyway - something else will fail
  94. later.
  95. */
  96. errno = save_errno;
  97. return 0;
  98. }
  99. #endif
  100. #if defined __UNIX98PTY_ONLY__
  101. else
  102. {
  103. /* If the ioctl fails it wasn't a Unix 98 master PTY */
  104. errno = ENOTTY;
  105. return ENOTTY;
  106. }
  107. #else
  108. # if !defined TIOCGPTN
  109. else if (errno == EINVAL)
  110. # endif
  111. {
  112. char *p;
  113. if (buflen < __strlen (_PATH_TTY) + 3)
  114. {
  115. errno = ERANGE;
  116. return ERANGE;
  117. }
  118. if (fstat (fd, &st) < 0)
  119. return errno;
  120. /* Check if FD really is a master pseudo terminal. */
  121. if (! MASTER_P (st.st_rdev))
  122. {
  123. errno = ENOTTY;
  124. return ENOTTY;
  125. }
  126. ptyno = minor (st.st_rdev);
  127. /* This is for the old BSD pseudo terminals. As of Linux
  128. 2.1.115 these are no longer supported. */
  129. if (major (st.st_rdev) == 4)
  130. ptyno -= 128;
  131. if (ptyno / 16 >= __strlen (_ptyname1))
  132. {
  133. errno = ENOTTY;
  134. return ENOTTY;
  135. }
  136. __strcpy (buf, _PATH_TTY);
  137. p = buf + __strlen (buf);
  138. p[0] = _ptyname1[ptyno / 16];
  139. p[1] = _ptyname2[ptyno % 16];
  140. p[2] = '\0';
  141. }
  142. if (stat(buf, &st) < 0)
  143. return errno;
  144. /* Check if the name we're about to return really corresponds to a
  145. slave pseudo terminal. */
  146. if (! S_ISCHR (st.st_mode) || ! SLAVE_P (st.st_rdev))
  147. {
  148. /* This really is a configuration problem. */
  149. errno = ENOTTY;
  150. return ENOTTY;
  151. }
  152. #endif
  153. errno = save_errno;
  154. return 0;
  155. }
  156. strong_alias(__ptsname_r,ptsname_r)
  157. /* Return the pathname of the pseudo terminal slave assoicated with
  158. the master FD is open on, or NULL on errors.
  159. The returned storage is good until the next call to this function. */
  160. char *
  161. ptsname (int fd)
  162. {
  163. static char buffer[sizeof (_PATH_DEVPTS) + 20];
  164. return __ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer;
  165. }