pty.c 630 B

1234567891011121314151617181920212223242526272829303132333435
  1. #include <stdlib.h>
  2. #include <sys/ioctl.h>
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5. int posix_openpt(int flags)
  6. {
  7. return open("/dev/ptmx", flags);
  8. }
  9. int grantpt(int fd)
  10. {
  11. return 0;
  12. }
  13. int unlockpt(int fd)
  14. {
  15. int unlock = 0;
  16. return ioctl(fd, TIOCSPTLCK, &unlock);
  17. }
  18. char *ptsname(int fd)
  19. {
  20. static char buf[9 + sizeof(int)*3 + 1];
  21. char *s = buf+sizeof(buf)-1;
  22. int pty;
  23. if (ioctl (fd, TIOCGPTN, &pty))
  24. return NULL;
  25. if (pty) for (; pty; pty/=10) *--s = '0' + pty%10;
  26. else *--s = '0';
  27. s -= 9;
  28. s[0] = '/'; s[1] = 'd'; s[2] = 'e'; s[3] = 'v';
  29. s[4] = '/'; s[5] = 'p'; s[6] = 't'; s[7] = 's'; s[8] = '/';
  30. return s;
  31. }