openpty.c 3.6 KB

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