Skip to content

Commit 709a296

Browse files
aykevldeadprogram
authored andcommitted
os: add basic OS functionality
1 parent f7b2a2c commit 709a296

File tree

6 files changed

+114
-1
lines changed

6 files changed

+114
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ clean:
8282
@rm -rf build
8383

8484
fmt:
85-
@go fmt . ./compiler ./interp ./loader ./ir ./src/device/arm ./src/examples/* ./src/machine ./src/runtime ./src/sync
85+
@go fmt . ./compiler ./interp ./loader ./ir ./src/device/arm ./src/examples/* ./src/machine ./src/os ./src/runtime ./src/sync
8686
@go fmt ./testdata/*.go
8787

8888
test:

src/os/file.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Package os implements a subset of the Go "os" package. See
2+
// https://godoc.org/os for details.
3+
//
4+
// Note that the current implementation is blocking. This limitation should be
5+
// removed in a future version.
6+
package os
7+
8+
import (
9+
"errors"
10+
)
11+
12+
// Portable analogs of some common system call errors.
13+
var (
14+
ErrUnsupported = errors.New("operation not supported")
15+
)
16+
17+
// Stdin, Stdout, and Stderr are open Files pointing to the standard input,
18+
// standard output, and standard error file descriptors.
19+
var (
20+
Stdin = &File{0, "/dev/stdin"}
21+
Stdout = &File{1, "/dev/stdout"}
22+
Stderr = &File{2, "/dev/stderr"}
23+
)
24+
25+
// File represents an open file descriptor.
26+
type File struct {
27+
fd uintptr
28+
name string
29+
}
30+
31+
// NewFile returns a new File with the given file descriptor and name.
32+
func NewFile(fd uintptr, name string) *File {
33+
return &File{fd, name}
34+
}
35+
36+
// Fd returns the integer Unix file descriptor referencing the open file. The
37+
// file descriptor is valid only until f.Close is called.
38+
func (f *File) Fd() uintptr {
39+
return f.fd
40+
}

src/os/file_unix.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// +build linux
2+
3+
package os
4+
5+
import (
6+
"syscall"
7+
)
8+
9+
// Read reads up to len(b) bytes from the File. It returns the number of bytes
10+
// read and any error encountered. At end of file, Read returns 0, io.EOF.
11+
func (f *File) Read(b []byte) (n int, err error) {
12+
return syscall.Read(int(f.fd), b)
13+
}
14+
15+
// Write writes len(b) bytes to the File. It returns the number of bytes written
16+
// and an error, if any. Write returns a non-nil error when n != len(b).
17+
func (f *File) Write(b []byte) (n int, err error) {
18+
return syscall.Write(int(f.fd), b)
19+
}
20+
21+
// Close closes the File, rendering it unusable for I/O.
22+
func (f *File) Close() error {
23+
return syscall.Close(int(f.fd))
24+
}

src/os/file_wasm.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// +build wasm
2+
3+
package os
4+
5+
import (
6+
_ "unsafe"
7+
)
8+
9+
// Read is unsupported on this system.
10+
func (f *File) Read(b []byte) (n int, err error) {
11+
return 0, ErrUnsupported
12+
}
13+
14+
// Write writes len(b) bytes to the output. It returns the number of bytes
15+
// written or an error if this file is not stdout or stderr.
16+
func (f *File) Write(b []byte) (n int, err error) {
17+
switch f.fd {
18+
case Stdout.fd, Stderr.fd:
19+
for _, c := range b {
20+
putchar(c)
21+
}
22+
return len(b), nil
23+
default:
24+
return 0, ErrUnsupported
25+
}
26+
}
27+
28+
// Close is unsupported on this system.
29+
func (f *File) Close() error {
30+
return ErrUnsupported
31+
}
32+
33+
//go:linkname putchar runtime.putchar
34+
func putchar(c byte)

testdata/stdlib.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
func main() {
9+
fmt.Println("stdin: ", os.Stdin.Fd())
10+
fmt.Println("stdout:", os.Stdout.Fd())
11+
fmt.Println("stderr:", os.Stderr.Fd())
12+
}

testdata/stdlib.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
stdin: 0
2+
stdout: 1
3+
stderr: 2

0 commit comments

Comments
 (0)