openpty.c 679 B

123456789101112131415161718192021222324252627282930313233
  1. #include <stdlib.h>
  2. #include <fcntl.h>
  3. #include <unistd.h>
  4. #include <pty.h>
  5. #include <stdio.h>
  6. /* Nonstandard, but vastly superior to the standard functions */
  7. int openpty(int *m, int *s, char *name, const struct termios *tio, const struct winsize *ws)
  8. {
  9. int n=0;
  10. char buf[20];
  11. *m = open("/dev/ptmx", O_RDWR|O_NOCTTY);
  12. if (*m < 0) return -1;
  13. if (ioctl(*m, TIOCSPTLCK, &n) || ioctl (*m, TIOCGPTN, &n)) {
  14. close(*m);
  15. return -1;
  16. }
  17. if (!name) name = buf;
  18. snprintf(name, sizeof buf, "/dev/pts/%d", n);
  19. if ((*s = open(name, O_RDWR|O_NOCTTY)) < 0) {
  20. close(*m);
  21. return -1;
  22. }
  23. if (tio) tcsetattr(*s, TCSANOW, tio);
  24. if (ws) ioctl(*s, TIOCSWINSZ, ws);
  25. return 0;
  26. }