os: add basic OS functionality
Этот коммит содержится в:
родитель
f7b2a2c977
коммит
709a296150
6 изменённых файлов: 114 добавлений и 1 удалений
2
Makefile
2
Makefile
|
@ -82,7 +82,7 @@ clean:
|
|||
@rm -rf build
|
||||
|
||||
fmt:
|
||||
@go fmt . ./compiler ./interp ./loader ./ir ./src/device/arm ./src/examples/* ./src/machine ./src/runtime ./src/sync
|
||||
@go fmt . ./compiler ./interp ./loader ./ir ./src/device/arm ./src/examples/* ./src/machine ./src/os ./src/runtime ./src/sync
|
||||
@go fmt ./testdata/*.go
|
||||
|
||||
test:
|
||||
|
|
40
src/os/file.go
Обычный файл
40
src/os/file.go
Обычный файл
|
@ -0,0 +1,40 @@
|
|||
// Package os implements a subset of the Go "os" package. See
|
||||
// https://godoc.org/os for details.
|
||||
//
|
||||
// Note that the current implementation is blocking. This limitation should be
|
||||
// removed in a future version.
|
||||
package os
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Portable analogs of some common system call errors.
|
||||
var (
|
||||
ErrUnsupported = errors.New("operation not supported")
|
||||
)
|
||||
|
||||
// Stdin, Stdout, and Stderr are open Files pointing to the standard input,
|
||||
// standard output, and standard error file descriptors.
|
||||
var (
|
||||
Stdin = &File{0, "/dev/stdin"}
|
||||
Stdout = &File{1, "/dev/stdout"}
|
||||
Stderr = &File{2, "/dev/stderr"}
|
||||
)
|
||||
|
||||
// File represents an open file descriptor.
|
||||
type File struct {
|
||||
fd uintptr
|
||||
name string
|
||||
}
|
||||
|
||||
// NewFile returns a new File with the given file descriptor and name.
|
||||
func NewFile(fd uintptr, name string) *File {
|
||||
return &File{fd, name}
|
||||
}
|
||||
|
||||
// Fd returns the integer Unix file descriptor referencing the open file. The
|
||||
// file descriptor is valid only until f.Close is called.
|
||||
func (f *File) Fd() uintptr {
|
||||
return f.fd
|
||||
}
|
24
src/os/file_unix.go
Обычный файл
24
src/os/file_unix.go
Обычный файл
|
@ -0,0 +1,24 @@
|
|||
// +build linux
|
||||
|
||||
package os
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// Read reads up to len(b) bytes from the File. It returns the number of bytes
|
||||
// read and any error encountered. At end of file, Read returns 0, io.EOF.
|
||||
func (f *File) Read(b []byte) (n int, err error) {
|
||||
return syscall.Read(int(f.fd), b)
|
||||
}
|
||||
|
||||
// Write writes len(b) bytes to the File. It returns the number of bytes written
|
||||
// and an error, if any. Write returns a non-nil error when n != len(b).
|
||||
func (f *File) Write(b []byte) (n int, err error) {
|
||||
return syscall.Write(int(f.fd), b)
|
||||
}
|
||||
|
||||
// Close closes the File, rendering it unusable for I/O.
|
||||
func (f *File) Close() error {
|
||||
return syscall.Close(int(f.fd))
|
||||
}
|
34
src/os/file_wasm.go
Обычный файл
34
src/os/file_wasm.go
Обычный файл
|
@ -0,0 +1,34 @@
|
|||
// +build wasm
|
||||
|
||||
package os
|
||||
|
||||
import (
|
||||
_ "unsafe"
|
||||
)
|
||||
|
||||
// Read is unsupported on this system.
|
||||
func (f *File) Read(b []byte) (n int, err error) {
|
||||
return 0, ErrUnsupported
|
||||
}
|
||||
|
||||
// Write writes len(b) bytes to the output. It returns the number of bytes
|
||||
// written or an error if this file is not stdout or stderr.
|
||||
func (f *File) Write(b []byte) (n int, err error) {
|
||||
switch f.fd {
|
||||
case Stdout.fd, Stderr.fd:
|
||||
for _, c := range b {
|
||||
putchar(c)
|
||||
}
|
||||
return len(b), nil
|
||||
default:
|
||||
return 0, ErrUnsupported
|
||||
}
|
||||
}
|
||||
|
||||
// Close is unsupported on this system.
|
||||
func (f *File) Close() error {
|
||||
return ErrUnsupported
|
||||
}
|
||||
|
||||
//go:linkname putchar runtime.putchar
|
||||
func putchar(c byte)
|
12
testdata/stdlib.go
предоставленный
Обычный файл
12
testdata/stdlib.go
предоставленный
Обычный файл
|
@ -0,0 +1,12 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("stdin: ", os.Stdin.Fd())
|
||||
fmt.Println("stdout:", os.Stdout.Fd())
|
||||
fmt.Println("stderr:", os.Stderr.Fd())
|
||||
}
|
3
testdata/stdlib.txt
предоставленный
Обычный файл
3
testdata/stdlib.txt
предоставленный
Обычный файл
|
@ -0,0 +1,3 @@
|
|||
stdin: 0
|
||||
stdout: 1
|
||||
stderr: 2
|
Загрузка…
Создание таблицы
Сослаться в новой задаче