file_other.mx raw

   1  //go:build baremetal || (moxie.wasm && !wasip1 && !wasip2) || nintendoswitch
   2  
   3  package os
   4  
   5  import (
   6  	_ "unsafe"
   7  )
   8  
   9  // Stdin, Stdout, and Stderr are open Files pointing to the standard input,
  10  // standard output, and standard error file descriptors.
  11  var (
  12  	Stdin  = NewFile(0, "/dev/stdin")
  13  	Stdout = NewFile(1, "/dev/stdout")
  14  	Stderr = NewFile(2, "/dev/stderr")
  15  )
  16  
  17  const DevNull = "/dev/null"
  18  
  19  // isOS indicates whether we're running on a real operating system with
  20  // filesystem support.
  21  const isOS = false
  22  
  23  // stdioFileHandle represents one of stdin, stdout, or stderr depending on the
  24  // number. It implements the FileHandle interface.
  25  type stdioFileHandle uint8
  26  
  27  // file is the real representation of *File.
  28  // The extra level of indirection ensures that no clients of os
  29  // can overwrite this data, which could cause the finalizer
  30  // to close the wrong file descriptor.
  31  type file struct {
  32  	handle     FileHandle
  33  	name       string
  34  	appendMode bool
  35  }
  36  
  37  func (f *file) close() error {
  38  	return f.handle.Close()
  39  }
  40  
  41  func NewFile(fd uintptr, name string) *File {
  42  	return &File{&file{handle: stdioFileHandle(fd), name: name}}
  43  }
  44  
  45  // Chdir changes the current working directory to the named directory.
  46  // If there is an error, it will be of type *PathError.
  47  func Chdir(dir string) error {
  48  	return ErrNotImplemented
  49  }
  50  
  51  // Rename renames (moves) oldpath to newpath.
  52  // If newpath already exists and is not a directory, Rename replaces it.
  53  // OS-specific restrictions may apply when oldpath and newpath are in different directories.
  54  // If there is an error, it will be of type *LinkError.
  55  func Rename(oldpath, newpath string) error {
  56  	return ErrNotImplemented
  57  }
  58  
  59  // Read reads up to len(b) bytes from machine.Serial.
  60  // It returns the number of bytes read and any error encountered.
  61  func (f stdioFileHandle) Read(b []byte) (n int, err error) {
  62  	if len(b) == 0 {
  63  		return 0, nil
  64  	}
  65  
  66  	size := buffered()
  67  	for size == 0 {
  68  		gosched()
  69  		size = buffered()
  70  	}
  71  
  72  	if size > len(b) {
  73  		size = len(b)
  74  	}
  75  	for i := 0; i < size; i++ {
  76  		b[i] = getchar()
  77  	}
  78  	return size, nil
  79  }
  80  
  81  func (f stdioFileHandle) ReadAt(b []byte, off int64) (n int, err error) {
  82  	return 0, ErrNotImplemented
  83  }
  84  
  85  func (f stdioFileHandle) WriteAt(b []byte, off int64) (n int, err error) {
  86  	return 0, ErrNotImplemented
  87  }
  88  
  89  // Write writes len(b) bytes to the output. It returns the number of bytes
  90  // written or an error if this file is not stdout or stderr.
  91  func (f stdioFileHandle) Write(b []byte) (n int, err error) {
  92  	switch f {
  93  	case 1, 2: // stdout, stderr
  94  		for _, c := range b {
  95  			putchar(c)
  96  		}
  97  		return len(b), nil
  98  	default:
  99  		return 0, ErrUnsupported
 100  	}
 101  }
 102  
 103  // Close is unsupported on this system.
 104  func (f stdioFileHandle) Close() error {
 105  	return ErrUnsupported
 106  }
 107  
 108  // Seek wraps syscall.Seek.
 109  func (f stdioFileHandle) Seek(offset int64, whence int) (int64, error) {
 110  	return -1, ErrUnsupported
 111  }
 112  
 113  func (f stdioFileHandle) Sync() error {
 114  	return ErrUnsupported
 115  }
 116  
 117  func (f stdioFileHandle) Fd() uintptr {
 118  	return uintptr(f)
 119  }
 120  
 121  //go:linkname putchar runtime.putchar
 122  func putchar(c byte)
 123  
 124  //go:linkname getchar runtime.getchar
 125  func getchar() byte
 126  
 127  //go:linkname buffered runtime.buffered
 128  func buffered() int
 129  
 130  //go:linkname gosched runtime.Gosched
 131  func gosched() int
 132  
 133  func Pipe() (r *File, w *File, err error) {
 134  	return nil, nil, ErrNotImplemented
 135  }
 136  
 137  func Symlink(oldname, newname string) error {
 138  	return ErrNotImplemented
 139  }
 140  
 141  func Readlink(name string) (string, error) {
 142  	return "", ErrNotImplemented
 143  }
 144  
 145  func tempDir() string {
 146  	return "/tmp"
 147  }
 148  
 149  // Truncate is unsupported on this system.
 150  func Truncate(filename string, size int64) (err error) {
 151  	return ErrUnsupported
 152  }
 153  
 154  // Truncate is unsupported on this system.
 155  func (f *File) Truncate(size int64) (err error) {
 156  	if f.handle == nil {
 157  		return ErrClosed
 158  	}
 159  
 160  	return Truncate(f.name, size)
 161  }
 162  
 163  func (f *File) chmod(mode FileMode) error {
 164  	return ErrUnsupported
 165  }
 166  
 167  func (f *File) chdir() error {
 168  	return ErrNotImplemented
 169  }
 170