openpty.c 3.7 KB

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