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