github.com/bir3/gocompiler@v0.9.2202/src/internal/testpty/pty_cgo.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build cgo && (aix || dragonfly || freebsd || (linux && !android) || netbsd || openbsd)
     6  
     7  package testpty
     8  
     9  /*
    10  #define _XOPEN_SOURCE 600
    11  #include <fcntl.h>
    12  #include <stdlib.h>
    13  #include <unistd.h>
    14  */
    15  import "C"
    16  
    17  import "os"
    18  
    19  func open() (pty *os.File, processTTY string, err error) {
    20  	m, err := C.posix_openpt(C.O_RDWR)
    21  	if m < 0 {
    22  		return nil, "", ptyError("posix_openpt", err)
    23  	}
    24  	if res, err := C.grantpt(m); res < 0 {
    25  		C.close(m)
    26  		return nil, "", ptyError("grantpt", err)
    27  	}
    28  	if res, err := C.unlockpt(m); res < 0 {
    29  		C.close(m)
    30  		return nil, "", ptyError("unlockpt", err)
    31  	}
    32  	processTTY = C.GoString(C.ptsname(m))
    33  	return os.NewFile(uintptr(m), "pty"), processTTY, nil
    34  }