//go:build linux
// +build linux

package pty

import (
	
	
	
	
)

func open() (,  *os.File,  error) {
	,  := os.OpenFile("/dev/ptmx", os.O_RDWR, 0)
	if  != nil {
		return nil, nil, 
	}
	// In case of error after this point, make sure we close the ptmx fd.
	defer func() {
		if  != nil {
			_ = .Close() // Best effort.
		}
	}()

	,  := ptsname()
	if  != nil {
		return nil, nil, 
	}

	if  := unlockpt();  != nil {
		return nil, nil, 
	}

	,  := os.OpenFile(, os.O_RDWR|syscall.O_NOCTTY, 0) //nolint:gosec // Expected Open from a variable.
	if  != nil {
		return nil, nil, 
	}
	return , , nil
}

func ptsname( *os.File) (string, error) {
	var  _C_uint
	 := ioctl(, syscall.TIOCGPTN, uintptr(unsafe.Pointer(&))) //nolint:gosec // Expected unsafe pointer for Syscall call.
	if  != nil {
		return "", 
	}
	return "/dev/pts/" + strconv.Itoa(int()), nil
}

func unlockpt( *os.File) error {
	var  _C_int
	// use TIOCSPTLCK with a pointer to zero to clear the lock.
	return ioctl(, syscall.TIOCSPTLCK, uintptr(unsafe.Pointer(&))) //nolint:gosec // Expected unsafe pointer for Syscall call.
}