Files
tinygo/src/os/file_other.go
T
Ayke van Laethem 77ec9b6369 all: update build constraints to Go 1.17
Do it all at once in preparation for Go 1.18 support.

To make this commit, I've simply modified the `fmt-check` Makefile
target to rewrite files instead of listing the differences. So this is a
fully mechanical change, it should not have introduced any errors.
2022-02-04 07:49:46 +01:00

86 lines
2.1 KiB
Go

//go:build baremetal || (wasm && !wasi)
// +build baremetal wasm,!wasi
package os
import (
_ "unsafe"
)
// Stdin, Stdout, and Stderr are open Files pointing to the standard input,
// standard output, and standard error file descriptors.
var (
Stdin = NewFile(stdioFileHandle(0), "/dev/stdin")
Stdout = NewFile(stdioFileHandle(1), "/dev/stdout")
Stderr = NewFile(stdioFileHandle(2), "/dev/stderr")
)
// isOS indicates whether we're running on a real operating system with
// filesystem support.
const isOS = false
// stdioFileHandle represents one of stdin, stdout, or stderr depending on the
// number. It implements the FileHandle interface.
type stdioFileHandle uint8
// file is the real representation of *File.
// The extra level of indirection ensures that no clients of os
// can overwrite this data, which could cause the finalizer
// to close the wrong file descriptor.
type file struct {
handle FileHandle
name string
}
func NewFile(fd FileHandle, name string) *File {
return &File{&file{fd, name}}
}
// Read is unsupported on this system.
func (f stdioFileHandle) Read(b []byte) (n int, err error) {
return 0, ErrUnsupported
}
func (f stdioFileHandle) ReadAt(b []byte, off int64) (n int, err error) {
return 0, ErrNotImplemented
}
// 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 stdioFileHandle) Write(b []byte) (n int, err error) {
switch f {
case 1, 2: // stdout, stderr
for _, c := range b {
putchar(c)
}
return len(b), nil
default:
return 0, ErrUnsupported
}
}
// Close is unsupported on this system.
func (f stdioFileHandle) Close() error {
return ErrUnsupported
}
// Seek wraps syscall.Seek.
func (f stdioFileHandle) Seek(offset int64, whence int) (int64, error) {
return -1, ErrUnsupported
}
//go:linkname putchar runtime.putchar
func putchar(c byte)
func Pipe() (r *File, w *File, err error) {
return nil, nil, ErrNotImplemented
}
func Readlink(name string) (string, error) {
return "", ErrNotImplemented
}
func tempDir() string {
return "/tmp"
}