OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [os/] [file.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2009 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
// Package os provides a platform-independent interface to operating system
6
// functionality.  The design is Unix-like.
7
// The os interface is intended to be uniform across all operating systems.
8
// Features not generally available appear in the system-specific package syscall.
9
package os
10
 
11
import (
12
        "io"
13
        "syscall"
14
)
15
 
16
// Name returns the name of the file as presented to Open.
17
func (f *File) Name() string { return f.name }
18
 
19
// Stdin, Stdout, and Stderr are open Files pointing to the standard input,
20
// standard output, and standard error file descriptors.
21
var (
22
        Stdin  = NewFile(syscall.Stdin, "/dev/stdin")
23
        Stdout = NewFile(syscall.Stdout, "/dev/stdout")
24
        Stderr = NewFile(syscall.Stderr, "/dev/stderr")
25
)
26
 
27
// Flags to Open wrapping those of the underlying system. Not all flags
28
// may be implemented on a given system.
29
const (
30
        O_RDONLY   int = syscall.O_RDONLY   // open the file read-only.
31
        O_WRONLY   int = syscall.O_WRONLY   // open the file write-only.
32
        O_RDWR     int = syscall.O_RDWR     // open the file read-write.
33
        O_APPEND   int = syscall.O_APPEND   // append data to the file when writing.
34
        O_ASYNC    int = syscall.O_ASYNC    // generate a signal when I/O is available.
35
        O_CREATE   int = syscall.O_CREAT    // create a new file if none exists.
36
        O_EXCL     int = syscall.O_EXCL     // used with O_CREATE, file must not exist
37
        O_NOCTTY   int = syscall.O_NOCTTY   // do not make file the controlling tty.
38
        O_NONBLOCK int = syscall.O_NONBLOCK // open in non-blocking mode.
39
        O_NDELAY   int = O_NONBLOCK         // synonym for O_NONBLOCK
40
        O_SYNC     int = syscall.O_SYNC     // open for synchronous I/O.
41
        O_TRUNC    int = syscall.O_TRUNC    // if possible, truncate file when opened.
42
)
43
 
44
// Seek whence values.
45
const (
46
        SEEK_SET int = 0 // seek relative to the origin of the file
47
        SEEK_CUR int = 1 // seek relative to the current offset
48
        SEEK_END int = 2 // seek relative to the end
49
)
50
 
51
// Read reads up to len(b) bytes from the File.
52
// It returns the number of bytes read and an error, if any.
53
// EOF is signaled by a zero count with err set to io.EOF.
54
func (f *File) Read(b []byte) (n int, err error) {
55
        if f == nil {
56
                return 0, EINVAL
57
        }
58
        n, e := f.read(b)
59
        if n < 0 {
60
                n = 0
61
        }
62
        if n == 0 && len(b) > 0 && e == nil {
63
                return 0, io.EOF
64
        }
65
        if e != nil {
66
                err = &PathError{"read", f.name, e}
67
        }
68
        return n, err
69
}
70
 
71
// ReadAt reads len(b) bytes from the File starting at byte offset off.
72
// It returns the number of bytes read and the error, if any.
73
// ReadAt always returns a non-nil error when n < len(b).
74
// At end of file, that error is io.EOF.
75
func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
76
        if f == nil {
77
                return 0, EINVAL
78
        }
79
        for len(b) > 0 {
80
                m, e := f.pread(b, off)
81
                if m == 0 && e == nil {
82
                        return n, io.EOF
83
                }
84
                if e != nil {
85
                        err = &PathError{"read", f.name, e}
86
                        break
87
                }
88
                n += m
89
                b = b[m:]
90
                off += int64(m)
91
        }
92
        return
93
}
94
 
95
// Write writes len(b) bytes to the File.
96
// It returns the number of bytes written and an error, if any.
97
// Write returns a non-nil error when n != len(b).
98
func (f *File) Write(b []byte) (n int, err error) {
99
        if f == nil {
100
                return 0, EINVAL
101
        }
102
        n, e := f.write(b)
103
        if n < 0 {
104
                n = 0
105
        }
106
 
107
        epipecheck(f, e)
108
 
109
        if e != nil {
110
                err = &PathError{"write", f.name, e}
111
        }
112
        return n, err
113
}
114
 
115
// WriteAt writes len(b) bytes to the File starting at byte offset off.
116
// It returns the number of bytes written and an error, if any.
117
// WriteAt returns a non-nil error when n != len(b).
118
func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
119
        if f == nil {
120
                return 0, EINVAL
121
        }
122
        for len(b) > 0 {
123
                m, e := f.pwrite(b, off)
124
                if e != nil {
125
                        err = &PathError{"write", f.name, e}
126
                        break
127
                }
128
                n += m
129
                b = b[m:]
130
                off += int64(m)
131
        }
132
        return
133
}
134
 
135
// Seek sets the offset for the next Read or Write on file to offset, interpreted
136
// according to whence: 0 means relative to the origin of the file, 1 means
137
// relative to the current offset, and 2 means relative to the end.
138
// It returns the new offset and an error, if any.
139
func (f *File) Seek(offset int64, whence int) (ret int64, err error) {
140
        r, e := f.seek(offset, whence)
141
        if e == nil && f.dirinfo != nil && r != 0 {
142
                e = syscall.EISDIR
143
        }
144
        if e != nil {
145
                return 0, &PathError{"seek", f.name, e}
146
        }
147
        return r, nil
148
}
149
 
150
// WriteString is like Write, but writes the contents of string s rather than
151
// an array of bytes.
152
func (f *File) WriteString(s string) (ret int, err error) {
153
        if f == nil {
154
                return 0, EINVAL
155
        }
156
        return f.Write([]byte(s))
157
}
158
 
159
// Mkdir creates a new directory with the specified name and permission bits.
160
// It returns an error, if any.
161
func Mkdir(name string, perm FileMode) error {
162
        e := syscall.Mkdir(name, syscallMode(perm))
163
        if e != nil {
164
                return &PathError{"mkdir", name, e}
165
        }
166
        return nil
167
}
168
 
169
// Chdir changes the current working directory to the named directory.
170
func Chdir(dir string) error {
171
        if e := syscall.Chdir(dir); e != nil {
172
                return &PathError{"chdir", dir, e}
173
        }
174
        return nil
175
}
176
 
177
// Chdir changes the current working directory to the file,
178
// which must be a directory.
179
func (f *File) Chdir() error {
180
        if e := syscall.Fchdir(f.fd); e != nil {
181
                return &PathError{"chdir", f.name, e}
182
        }
183
        return nil
184
}
185
 
186
// Open opens the named file for reading.  If successful, methods on
187
// the returned file can be used for reading; the associated file
188
// descriptor has mode O_RDONLY.
189
// It returns the File and an error, if any.
190
func Open(name string) (file *File, err error) {
191
        return OpenFile(name, O_RDONLY, 0)
192
}
193
 
194
// Create creates the named file mode 0666 (before umask), truncating
195
// it if it already exists.  If successful, methods on the returned
196
// File can be used for I/O; the associated file descriptor has mode
197
// O_RDWR.
198
// It returns the File and an error, if any.
199
func Create(name string) (file *File, err error) {
200
        return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666)
201
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.