openpty.c 3.7 KB

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