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