Merge pull request #26 from yoheiueda/use-syscall-consts
Use syscall.TIOCGPTN and syscall.TIOCSPTLCK for ioctl
Guillaume J. Charmes
9 years ago
0 | |
package pty
|
1 | |
|
2 | |
// from <asm-generic/ioctl.h>
|
3 | |
const (
|
4 | |
_IOC_NRBITS = 8
|
5 | |
_IOC_TYPEBITS = 8
|
6 | |
|
7 | |
_IOC_SIZEBITS = 14
|
8 | |
_IOC_DIRBITS = 2
|
9 | |
|
10 | |
_IOC_NRSHIFT = 0
|
11 | |
_IOC_TYPESHIFT = _IOC_NRSHIFT + _IOC_NRBITS
|
12 | |
_IOC_SIZESHIFT = _IOC_TYPESHIFT + _IOC_TYPEBITS
|
13 | |
_IOC_DIRSHIFT = _IOC_SIZESHIFT + _IOC_SIZEBITS
|
14 | |
|
15 | |
_IOC_NONE uint = 0
|
16 | |
_IOC_WRITE uint = 1
|
17 | |
_IOC_READ uint = 2
|
18 | |
)
|
19 | |
|
20 | |
func _IOC(dir uint, ioctl_type byte, nr byte, size uintptr) uintptr {
|
21 | |
return (uintptr(dir)<<_IOC_DIRSHIFT |
|
22 | |
uintptr(ioctl_type)<<_IOC_TYPESHIFT |
|
23 | |
uintptr(nr)<<_IOC_NRSHIFT |
|
24 | |
size<<_IOC_SIZESHIFT)
|
25 | |
}
|
26 | |
|
27 | |
func _IO(ioctl_type byte, nr byte) uintptr {
|
28 | |
return _IOC(_IOC_NONE, ioctl_type, nr, 0)
|
29 | |
}
|
30 | |
|
31 | |
func _IOR(ioctl_type byte, nr byte, size uintptr) uintptr {
|
32 | |
return _IOC(_IOC_READ, ioctl_type, nr, size)
|
33 | |
}
|
34 | |
|
35 | |
func _IOW(ioctl_type byte, nr byte, size uintptr) uintptr {
|
36 | |
return _IOC(_IOC_WRITE, ioctl_type, nr, size)
|
37 | |
}
|
38 | |
|
39 | |
func _IOWR(ioctl_type byte, nr byte, size uintptr) uintptr {
|
40 | |
return _IOC(_IOC_READ|_IOC_WRITE, ioctl_type, nr, size)
|
41 | |
}
|
4 | 4 |
"strconv"
|
5 | 5 |
"syscall"
|
6 | 6 |
"unsafe"
|
7 | |
)
|
8 | |
|
9 | |
var (
|
10 | |
ioctl_TIOCGPTN = _IOR('T', 0x30, unsafe.Sizeof(_C_uint(0))) /* Get Pty Number (of pty-mux device) */
|
11 | |
ioctl_TIOCSPTLCK = _IOW('T', 0x31, unsafe.Sizeof(_C_int(0))) /* Lock/unlock Pty */
|
12 | 7 |
)
|
13 | 8 |
|
14 | 9 |
func open() (pty, tty *os.File, err error) {
|
|
36 | 31 |
|
37 | 32 |
func ptsname(f *os.File) (string, error) {
|
38 | 33 |
var n _C_uint
|
39 | |
err := ioctl(f.Fd(), ioctl_TIOCGPTN, uintptr(unsafe.Pointer(&n)))
|
|
34 |
err := ioctl(f.Fd(), syscall.TIOCGPTN, uintptr(unsafe.Pointer(&n)))
|
40 | 35 |
if err != nil {
|
41 | 36 |
return "", err
|
42 | 37 |
}
|
|
46 | 41 |
func unlockpt(f *os.File) error {
|
47 | 42 |
var u _C_int
|
48 | 43 |
// use TIOCSPTLCK with a zero valued arg to clear the slave pty lock
|
49 | |
return ioctl(f.Fd(), ioctl_TIOCSPTLCK, uintptr(unsafe.Pointer(&u)))
|
|
44 |
return ioctl(f.Fd(), syscall.TIOCSPTLCK, uintptr(unsafe.Pointer(&u)))
|
50 | 45 |
}
|