summaryrefslogtreecommitdiff
path: root/doc/progs/fd.go
diff options
context:
space:
mode:
Diffstat (limited to 'doc/progs/fd.go')
-rw-r--r--doc/progs/fd.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/doc/progs/fd.go b/doc/progs/fd.go
new file mode 100644
index 000000000..50d33624a
--- /dev/null
+++ b/doc/progs/fd.go
@@ -0,0 +1,58 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package fd
+
+import Syscall "syscall"
+
+export type FD struct {
+ fildes int64; // file descriptor number
+ name string; // file name at Open time
+}
+
+func NewFD(fd int64, name string) *FD {
+ if fd < 0 {
+ return nil
+ }
+ n := new(FD);
+ n.fildes = fd;
+ n.name = name;
+ return n
+}
+
+export var (
+ Stdin = NewFD(0, "/dev/stdin");
+ Stdout = NewFD(1, "/dev/stdout");
+ Stderr = NewFD(2, "/dev/stderr");
+)
+
+export func Open(name string, mode int64, perm int64) (fd *FD, errno int64) {
+ r, e := Syscall.open(name, mode, perm);
+ return NewFD(r, name), e
+}
+
+func (fd *FD) Close() int64 {
+ if fd == nil {
+ return Syscall.EINVAL
+ }
+ r, e := Syscall.close(fd.fildes);
+ fd.fildes = -1; // so it can't be closed again
+ return 0
+}
+
+func (fd *FD) Read(b *[]byte) (ret int64, errno int64) {
+ if fd == nil {
+ return -1, Syscall.EINVAL
+ }
+ r, e := Syscall.read(fd.fildes, &b[0], int64(len(b)));
+ return r, e
+}
+
+func (fd *FD) Write(b *[]byte) (ret int64, errno int64) {
+ if fd == nil {
+ return -1, Syscall.EINVAL
+ }
+ r, e := Syscall.write(fd.fildes, &b[0], int64(len(b)));
+ return r, e
+}