openpty.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. /* Return the result of ptsname_r in the buffer pointed to by PTS,
  30. which should be of length BUF_LEN. If it is too long to fit in
  31. this buffer, a sufficiently long buffer is allocated using malloc,
  32. and returned in PTS. 0 is returned upon success, -1 otherwise. */
  33. static int
  34. pts_name (int fd, char **pts, size_t buf_len)
  35. {
  36. int rv;
  37. char *buf = *pts;
  38. for (;;)
  39. {
  40. char *new_buf;
  41. if (buf_len)
  42. {
  43. rv = ptsname_r (fd, buf, buf_len);
  44. if (rv != 0 || memchr (buf, '\0', buf_len))
  45. /* We either got an error, or we succeeded and the
  46. returned name fit in the buffer. */
  47. break;
  48. /* Try again with a longer buffer. */
  49. buf_len += buf_len; /* Double it */
  50. }
  51. else
  52. /* No initial buffer; start out by mallocing one. */
  53. buf_len = 128; /* First time guess. */
  54. if (buf != *pts)
  55. /* We've already malloced another buffer at least once. */
  56. new_buf = realloc (buf, buf_len);
  57. else
  58. new_buf = malloc (buf_len);
  59. if (! new_buf)
  60. {
  61. rv = -1;
  62. errno = ENOMEM;
  63. break;
  64. }
  65. buf = new_buf;
  66. }
  67. if (rv == 0)
  68. *pts = buf; /* Return buffer to the user. */
  69. else if (buf != *pts)
  70. free (buf); /* Free what we malloced when returning an error. */
  71. return rv;
  72. }
  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. int
  77. openpty (int *amaster, int *aslave, char *name, struct termios *termp,
  78. struct winsize *winp)
  79. {
  80. #ifdef PATH_MAX
  81. char _buf[PATH_MAX];
  82. #else
  83. char _buf[512];
  84. #endif
  85. char *buf = _buf;
  86. int master, slave;
  87. master = getpt ();
  88. if (master == -1)
  89. return -1;
  90. if (grantpt (master))
  91. goto fail;
  92. if (unlockpt (master))
  93. goto fail;
  94. if (pts_name (master, &buf, sizeof (_buf)))
  95. goto fail;
  96. slave = open (buf, O_RDWR | O_NOCTTY);
  97. if (slave == -1)
  98. {
  99. if (buf != _buf)
  100. free (buf);
  101. goto fail;
  102. }
  103. /* XXX Should we ignore errors here? */
  104. if(termp)
  105. tcsetattr (slave, TCSAFLUSH, termp);
  106. if (winp)
  107. ioctl (slave, TIOCSWINSZ, winp);
  108. *amaster = master;
  109. *aslave = slave;
  110. if (name != NULL)
  111. strcpy (name, buf);
  112. if (buf != _buf)
  113. free (buf);
  114. return 0;
  115. fail:
  116. close (master);
  117. return -1;
  118. }