openpty.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 <limits.h>
  19. #include <pty.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <termios.h>
  23. #include <unistd.h>
  24. #include <sys/types.h>
  25. extern int getpt (void);
  26. extern int grantpt (int fd);
  27. extern int ptsname_r (int fd, char *buf, size_t buflen);
  28. extern int unlockpt (int fd);
  29. /* BCS: the following function is, IMO, overkill */
  30. #if 0
  31. /* Return the result of ptsname_r in the buffer pointed to by PTS,
  32. which should be of length BUF_LEN. If it is too long to fit in
  33. this buffer, a sufficiently long buffer is allocated using malloc,
  34. and returned in PTS. 0 is returned upon success, -1 otherwise. */
  35. static int
  36. pts_name (int fd, char **pts, size_t buf_len)
  37. {
  38. int rv;
  39. char *buf = *pts;
  40. for (;;)
  41. {
  42. char *new_buf;
  43. if (buf_len)
  44. {
  45. rv = ptsname_r (fd, buf, buf_len);
  46. if (rv != 0 || memchr (buf, '\0', buf_len))
  47. /* We either got an error, or we succeeded and the
  48. returned name fit in the buffer. */
  49. break;
  50. /* Try again with a longer buffer. */
  51. buf_len += buf_len; /* Double it */
  52. }
  53. else
  54. /* No initial buffer; start out by mallocing one. */
  55. buf_len = 128; /* First time guess. */
  56. if (buf != *pts)
  57. /* We've already malloced another buffer at least once. */
  58. new_buf = realloc (buf, buf_len);
  59. else
  60. new_buf = malloc (buf_len);
  61. if (! new_buf)
  62. {
  63. rv = -1;
  64. errno = ENOMEM;
  65. break;
  66. }
  67. buf = new_buf;
  68. }
  69. if (rv == 0)
  70. *pts = buf; /* Return buffer to the user. */
  71. else if (buf != *pts)
  72. free (buf); /* Free what we malloced when returning an error. */
  73. return rv;
  74. }
  75. #endif
  76. /* Create pseudo tty master slave pair and set terminal attributes
  77. according to TERMP and WINP. Return handles for both ends in
  78. AMASTER and ASLAVE, and return the name of the slave end in NAME. */
  79. int attribute_hidden
  80. __openpty (int *amaster, int *aslave, char *name, struct termios *termp,
  81. struct winsize *winp)
  82. {
  83. #if 0
  84. #ifdef PATH_MAX
  85. char _buf[PATH_MAX];
  86. #else
  87. char _buf[512];
  88. #endif
  89. char *buf = _buf;
  90. #else
  91. #ifdef PATH_MAX
  92. char buf[PATH_MAX];
  93. #else
  94. char buf[512];
  95. #endif
  96. #endif
  97. int master, slave;
  98. master = getpt ();
  99. if (master == -1)
  100. return -1;
  101. if (grantpt (master))
  102. goto fail;
  103. if (unlockpt (master))
  104. goto fail;
  105. #if 0
  106. if (pts_name (master, &buf, sizeof (_buf)))
  107. #else
  108. if (ptsname_r (master, buf, sizeof buf))
  109. #endif
  110. goto fail;
  111. slave = open (buf, O_RDWR | O_NOCTTY);
  112. if (slave == -1)
  113. {
  114. #if 0
  115. if (buf != _buf)
  116. free (buf);
  117. #endif
  118. goto fail;
  119. }
  120. /* XXX Should we ignore errors here? */
  121. if(termp)
  122. tcsetattr (slave, TCSAFLUSH, termp);
  123. if (winp)
  124. ioctl (slave, TIOCSWINSZ, winp);
  125. *amaster = master;
  126. *aslave = slave;
  127. if (name != NULL)
  128. strcpy (name, buf);
  129. #if 0
  130. if (buf != _buf)
  131. free (buf);
  132. #endif
  133. return 0;
  134. fail:
  135. close (master);
  136. return -1;
  137. }
  138. strong_alias(__openpty,openpty)