bsd_getpt.c 2.1 KB

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