pty.go 909 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // +build !windows
  2. package termios
  3. import (
  4. "fmt"
  5. "os"
  6. "golang.org/x/sys/unix"
  7. )
  8. func open_device(path string) (uintptr, error) {
  9. fd, err := unix.Open(path, unix.O_NOCTTY|unix.O_RDWR|unix.O_CLOEXEC, 0666)
  10. if err != nil {
  11. return 0, fmt.Errorf("unable to open %q: %v", path, err)
  12. }
  13. return uintptr(fd), nil
  14. }
  15. // Pty returns a UNIX 98 pseudoterminal device.
  16. // Pty returns a pair of fds representing the master and slave pair.
  17. func Pty() (*os.File, *os.File, error) {
  18. ptm, err := open_pty_master()
  19. if err != nil {
  20. return nil, nil, err
  21. }
  22. sname, err := Ptsname(ptm)
  23. if err != nil {
  24. return nil, nil, err
  25. }
  26. err = grantpt(ptm)
  27. if err != nil {
  28. return nil, nil, err
  29. }
  30. err = unlockpt(ptm)
  31. if err != nil {
  32. return nil, nil, err
  33. }
  34. pts, err := open_device(sname)
  35. if err != nil {
  36. return nil, nil, err
  37. }
  38. return os.NewFile(uintptr(ptm), "ptm"), os.NewFile(uintptr(pts), sname), nil
  39. }