openpty.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. /* Create pseudo tty master slave pair and set terminal attributes
  25. according to TERMP and WINP. Return handles for both ends in
  26. AMASTER and ASLAVE, and return the name of the slave end in NAME. */
  27. int
  28. openpty (int *amaster, int *aslave, char *name, const struct termios *termp,
  29. const struct winsize *winp)
  30. {
  31. #ifdef PATH_MAX
  32. char buf[PATH_MAX];
  33. #else
  34. char buf[512];
  35. #endif
  36. int master, slave;
  37. master = posix_openpt (O_RDWR);
  38. if (master == -1)
  39. return -1;
  40. if (grantpt (master))
  41. goto fail;
  42. if (unlockpt (master))
  43. goto fail;
  44. if (ptsname_r (master, buf, sizeof buf))
  45. goto fail;
  46. slave = open (buf, O_RDWR | O_NOCTTY);
  47. if (slave == -1)
  48. {
  49. goto fail;
  50. }
  51. /* XXX Should we ignore errors here? */
  52. if(termp)
  53. tcsetattr (slave, TCSAFLUSH, termp);
  54. if (winp)
  55. ioctl (slave, TIOCSWINSZ, winp);
  56. *amaster = master;
  57. *aslave = slave;
  58. if (name != NULL)
  59. strcpy (name, buf);
  60. return 0;
  61. fail:
  62. close (master);
  63. return -1;
  64. }
  65. libutil_hidden_def(openpty)