bsd_getpt.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Copyright (C) 1998, 1999, 2000, 2001, 2002 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 <string.h>
  19. #include <unistd.h>
  20. /* Prefix for master pseudo terminal nodes. */
  21. #define _PATH_PTY "/dev/pty"
  22. /* Letters indicating a series of pseudo terminals. */
  23. #ifndef PTYNAME1
  24. #define PTYNAME1 "pqrsPQRS"
  25. #endif
  26. const char __libc_ptyname1[] attribute_hidden = PTYNAME1;
  27. /* Letters indicating the position within a series. */
  28. #ifndef PTYNAME2
  29. #define PTYNAME2 "0123456789abcdefghijklmnopqrstuv";
  30. #endif
  31. const char __libc_ptyname2[] attribute_hidden = PTYNAME2;
  32. /* Open a master pseudo terminal and return its file descriptor. */
  33. int
  34. __getpt (void)
  35. {
  36. char buf[sizeof (_PATH_PTY) + 2];
  37. const char *p, *q;
  38. char *s;
  39. s = __mempcpy (buf, _PATH_PTY, sizeof (_PATH_PTY) - 1);
  40. /* s[0] and s[1] will be filled in the loop. */
  41. s[2] = '\0';
  42. for (p = __libc_ptyname1; *p != '\0'; ++p)
  43. {
  44. s[0] = *p;
  45. for (q = __libc_ptyname2; *q != '\0'; ++q)
  46. {
  47. int fd;
  48. s[1] = *q;
  49. fd = __open (buf, O_RDWR);
  50. if (fd != -1)
  51. return fd;
  52. if (errno == ENOENT)
  53. return -1;
  54. }
  55. }
  56. __set_errno (ENOENT);
  57. return -1;
  58. }