openpty.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. int
  75. openpty (int *amaster, int *aslave, char *name, struct termios *termp,
  76. struct winsize *winp)
  77. {
  78. #if 0
  79. #ifdef PATH_MAX
  80. char _buf[PATH_MAX];
  81. #else
  82. char _buf[512];
  83. #endif
  84. char *buf = _buf;
  85. #else
  86. #ifdef PATH_MAX
  87. char buf[PATH_MAX];
  88. #else
  89. char buf[512];
  90. #endif
  91. #endif
  92. int master, slave;
  93. master = posix_openpt (O_RDWR);
  94. if (master == -1)
  95. return -1;
  96. if (grantpt (master))
  97. goto fail;
  98. if (unlockpt (master))
  99. goto fail;
  100. #if 0
  101. if (pts_name (master, &buf, sizeof (_buf)))
  102. #else
  103. if (ptsname_r (master, buf, sizeof buf))
  104. #endif
  105. goto fail;
  106. slave = open (buf, O_RDWR | O_NOCTTY);
  107. if (slave == -1)
  108. {
  109. #if 0
  110. if (buf != _buf)
  111. free (buf);
  112. #endif
  113. goto fail;
  114. }
  115. /* XXX Should we ignore errors here? */
  116. if(termp)
  117. tcsetattr (slave, TCSAFLUSH, termp);
  118. if (winp)
  119. ioctl (slave, TIOCSWINSZ, winp);
  120. *amaster = master;
  121. *aslave = slave;
  122. if (name != NULL)
  123. strcpy (name, buf);
  124. #if 0
  125. if (buf != _buf)
  126. free (buf);
  127. #endif
  128. return 0;
  129. fail:
  130. close (master);
  131. return -1;
  132. }
  133. libutil_hidden_def(openpty)