forked from cs61/cs61-f24-psets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio61.cc
204 lines (166 loc) · 5.07 KB
/
io61.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "io61.hh"
#include <sys/types.h>
#include <sys/stat.h>
#include <climits>
#include <cerrno>
// io61.cc
// YOUR CODE HERE!
// io61_file
// Data structure for io61 file wrappers. Add your own stuff.
struct io61_file {
int fd = -1; // file descriptor
int mode; // open mode (O_RDONLY or O_WRONLY)
};
// io61_fdopen(fd, mode)
// Returns a new io61_file for file descriptor `fd`. `mode` is either
// O_RDONLY for a read-only file or O_WRONLY for a write-only file.
// You need not support read/write files.
io61_file* io61_fdopen(int fd, int mode) {
assert(fd >= 0);
io61_file* f = new io61_file;
f->fd = fd;
f->mode = mode;
return f;
}
// io61_close(f)
// Closes the io61_file `f` and releases all its resources.
int io61_close(io61_file* f) {
io61_flush(f);
int r = close(f->fd);
delete f;
return r;
}
// io61_readc(f)
// Reads a single (unsigned) byte from `f` and returns it. Returns EOF,
// which equals -1, on end of file or error.
int io61_readc(io61_file* f) {
unsigned char ch;
ssize_t nr = read(f->fd, &ch, 1);
if (nr == 1) {
return ch;
} else if (nr == 0) {
errno = 0; // clear `errno` to indicate EOF
return -1;
} else {
assert(nr == -1 && errno > 0);
return -1;
}
}
// io61_read(f, buf, sz)
// Reads up to `sz` bytes from `f` into `buf`. Returns the number of
// bytes read on success. Returns 0 if end-of-file is encountered before
// any bytes are read, and -1 if an error is encountered before any
// bytes are read.
//
// Note that the return value might be positive, but less than `sz`,
// if end-of-file or error is encountered before all `sz` bytes are read.
// This is called a “short read.”
ssize_t io61_read(io61_file* f, unsigned char* buf, size_t sz) {
size_t nread = 0;
while (nread != sz) {
int ch = io61_readc(f);
if (ch == EOF) {
break;
}
buf[nread] = ch;
++nread;
}
if (nread != 0 || sz == 0 || errno == 0) {
return nread;
} else {
return -1;
}
}
// io61_writec(f)
// Write a single character `c` to `f` (converted to unsigned char).
// Returns 0 on success and -1 on error.
int io61_writec(io61_file* f, int c) {
unsigned char ch = c;
ssize_t nw = write(f->fd, &ch, 1);
if (nw == 1) {
return 0;
} else {
return -1;
}
}
// io61_write(f, buf, sz)
// Writes `sz` characters from `buf` to `f`. Returns `sz` on success.
// Can write fewer than `sz` characters when there is an error, such as
// a drive running out of space. In this case io61_write returns the
// number of characters written, or -1 if no characters were written
// before the error occurred.
ssize_t io61_write(io61_file* f, const unsigned char* buf, size_t sz) {
size_t nwritten = 0;
while (nwritten != sz) {
if (io61_writec(f, buf[nwritten]) == -1) {
break;
}
++nwritten;
}
if (nwritten != 0 || sz == 0) {
return nwritten;
} else {
return -1;
}
}
// io61_flush(f)
// If `f` was opened write-only, `io61_flush(f)` forces a write of any
// cached data written to `f`. Returns 0 on success; returns -1 if an error
// is encountered before all cached data was written.
//
// If `f` was opened read-only, `io61_flush(f)` returns 0. It may also
// drop any data cached for reading.
int io61_flush(io61_file* f) {
(void) f;
return 0;
}
// io61_seek(f, off)
// Changes the file pointer for file `f` to `off` bytes into the file.
// Returns 0 on success and -1 on failure.
int io61_seek(io61_file* f, off_t off) {
off_t r = lseek(f->fd, (off_t) off, SEEK_SET);
// Ignore the returned offset unless it’s an error.
if (r == -1) {
return -1;
} else {
return 0;
}
}
// You shouldn't need to change these functions.
// io61_open_check(filename, mode)
// Opens the file corresponding to `filename` and returns its io61_file.
// If `!filename`, returns either the standard input or the
// standard output, depending on `mode`. Exits with an error message if
// `filename != nullptr` and the named file cannot be opened.
io61_file* io61_open_check(const char* filename, int mode) {
int fd;
if (filename) {
fd = open(filename, mode, 0666);
} else if ((mode & O_ACCMODE) == O_RDONLY) {
fd = STDIN_FILENO;
} else {
fd = STDOUT_FILENO;
}
if (fd < 0) {
fprintf(stderr, "%s: %s\n", filename, strerror(errno));
exit(1);
}
return io61_fdopen(fd, mode & O_ACCMODE);
}
// io61_fileno(f)
// Returns the file descriptor associated with `f`.
int io61_fileno(io61_file* f) {
return f->fd;
}
// io61_filesize(f)
// Returns the size of `f` in bytes. Returns -1 if `f` does not have a
// well-defined size (for instance, if it is a pipe).
off_t io61_filesize(io61_file* f) {
struct stat s;
int r = fstat(f->fd, &s);
if (r >= 0 && S_ISREG(s.st_mode)) {
return s.st_size;
} else {
return -1;
}
}