Newer
Older
// 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 os
import syscall "syscall"
import os "os"
Rob Pike
committed
// Auxiliary information if the FD describes a directory
type DirInfo struct { // TODO(r): 6g bug means this can't be private
buf []byte; // buffer for directory I/O
nbuf int64; // length of buf; return value from Getdirentries
bufp int64; // location of next record in buf.
}
Rob Pike
committed
dirinfo *DirInfo; // nil unless directory being read
func (fd *FD) Fd() int64 {
return fd.fd
}
func (fd *FD) Name() string {
return fd.name
}
func NewFD(fd int64, name string) *FD {
Rob Pike
committed
return &FD{fd, name, nil}
Stdin = NewFD(0, "/dev/stdin");
Stdout = NewFD(1, "/dev/stdout");
Stderr = NewFD(2, "/dev/stderr");
O_RDONLY = syscall.O_RDONLY;
O_WRONLY = syscall.O_WRONLY;
O_RDWR = syscall.O_RDWR;
O_APPEND = syscall.O_APPEND;
O_ASYNC = syscall.O_ASYNC;
O_CREAT = syscall.O_CREAT;
O_NOCTTY = syscall.O_NOCTTY;
O_NONBLOCK = syscall.O_NONBLOCK;
O_NDELAY = O_NONBLOCK;
O_SYNC = syscall.O_SYNC;
O_TRUNC = syscall.O_TRUNC;
)
func Open(name string, mode int, flags int) (fd *FD, err *Error) {
r, e := syscall.Open(name, int64(mode), int64(flags));
}
func (fd *FD) Close() *Error {
if fd == nil {
return EINVAL
}
r, e := syscall.Close(fd.fd);
fd.fd = -1; // so it can't be closed again
Rob Pike
committed
return 0, EINVAL
var r, e int64;
if len(b) > 0 { // because we access b[0]
Rob Pike
committed
if r < 0 {
r = 0
}
Rob Pike
committed
return 0, EINVAL
var r, e int64;
if len(b) > 0 { // because we access b[0]
Rob Pike
committed
if r < 0 {
r = 0
}
Rob Pike
committed
func (fd *FD) Seek(offset int64, whence int) (ret int64, err *Error) {
r, e := syscall.Seek(fd.fd, offset, int64(whence));
if e != 0 {
return -1, ErrnoToError(e)
}
if fd.dirinfo != nil && r != 0 {
return -1, ErrnoToError(syscall.EISDIR)
}
return r, nil
}
func (fd *FD) WriteString(s string) (ret int, err *Error) {
Rob Pike
committed
return 0, EINVAL
r, e := syscall.Write(fd.fd, syscall.StringBytePtr(s), int64(len(s)));
Rob Pike
committed
if r < 0 {
r = 0
}
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
func Stat(name string) (dir *Dir, err *Error) {
stat := new(syscall.Stat_t);
r, e := syscall.Stat(name, stat);
if e != 0 {
return nil, ErrnoToError(e)
}
return dirFromStat(name, new(Dir), stat), nil
}
func Fstat(fd *FD) (dir *Dir, err *Error) {
stat := new(syscall.Stat_t);
r, e := syscall.Fstat(fd.fd, stat);
if e != 0 {
return nil, ErrnoToError(e)
}
return dirFromStat(fd.name, new(Dir), stat), nil
}
func Lstat(name string) (dir *Dir, err *Error) {
stat := new(syscall.Stat_t);
r, e := syscall.Lstat(name, stat);
if e != 0 {
return nil, ErrnoToError(e)
}
return dirFromStat(name, new(Dir), stat), nil
}