bsd_getpt.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #if defined __USE_BSD
  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. static __inline__ 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. }
  59. #endif /* __USE_BSD */