github.com/DQNEO/babygo@v0.0.3/src/os/os.go (about)

     1  package os
     2  
     3  import "syscall"
     4  
     5  const SYS_EXIT int = 60
     6  
     7  var Args []string
     8  
     9  var Stdin *File
    10  var Stdout *File
    11  var Stderr *File
    12  
    13  type File struct {
    14  	fd int
    15  }
    16  
    17  const O_CREATE_WRITE int = 524866 // O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC
    18  
    19  func Create(name string) (*File, interface{}) {
    20  	var fd int
    21  	fd, _ = syscall.Open(name, O_CREATE_WRITE, 438)
    22  	if fd < 0 {
    23  		panic("unable to create file " + name)
    24  	}
    25  
    26  	f := new(File)
    27  	f.fd = fd
    28  	return f, nil
    29  }
    30  
    31  func (f *File) Close() {
    32  	syscall.Close(f.fd)
    33  }
    34  
    35  func (f *File) Write(p []byte) (int, interface{}) {
    36  	syscall.Write(f.fd, p)
    37  	return 0, nil
    38  }
    39  
    40  func init() {
    41  	Args = runtime_args()
    42  	Stdin = &File{
    43  		fd: 0,
    44  	}
    45  	Stdout = &File{
    46  		fd: 1,
    47  	}
    48  	Stderr = &File{
    49  		fd: 2,
    50  	}
    51  }
    52  
    53  func Getenv(key string) string {
    54  	v := runtime_getenv(key)
    55  	return v
    56  }
    57  
    58  func Exit(status int) {
    59  	syscall.Syscall(uintptr(SYS_EXIT), uintptr(status), 0, 0)
    60  }
    61  
    62  func runtime_args() []string
    63  func runtime_getenv(key string) string