bsd_getpt.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Copyright (C) 1998, 1999 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 Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. 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. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If not,
  14. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 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 _ptyname1[] = PTYNAME1;
  27. /* Letters indicating the position within a series. */
  28. #ifndef PTYNAME2
  29. #define PTYNAME2 "0123456789abcdefghijklmnopqrstuv";
  30. #endif
  31. const char _ptyname2[] = 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. memcpy (buf, _PATH_PTY, sizeof (_PATH_PTY) - 1);
  40. s = buf + strlen (buf);
  41. /* s[0] and s[1] will be filled in the loop. */
  42. s[2] = '\0';
  43. for (p = _ptyname1; *p != '\0'; ++p)
  44. {
  45. s[0] = *p;
  46. for (q = _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. errno = ENOENT;
  58. return -1;
  59. }