authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-04 14:33:40-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-04 14:33:40-07:00
log139e5ca08f3b15cf900be7096d7be0db4e0eced8
treefe23f2fe67035711b93b72f4c1dae9a726a9f14f
parentcd6283e8c442e6a456a325a4c974103a2a0d9e40

fix reading source from stdin


1 files changed, 7 insertions(+), 44 deletions(-)

src/os.cpp+7-44
......@@ -38,22 +38,24 @@ void os_spawn_process(const char *exe, ZigList<const char *> &args, bool detache
3838 zig_panic("execvp failed: %s", strerror(errno));
3939}
4040
41static void read_all_fd_stream(int fd, Buf *out_buf) {
41static int read_all_fd_stream(int fd, Buf *out_buf) {
4242 static const ssize_t buf_size = 0x2000;
4343 buf_resize(out_buf, buf_size);
4444 ssize_t actual_buf_len = 0;
4545 for (;;) {
4646 ssize_t amt_read = read(fd, buf_ptr(out_buf), buf_len(out_buf));
47 if (amt_read < 0)
48 zig_panic("fd read error");
47 if (amt_read < 0) {
48 return ErrorFileSystem;
49 }
4950 actual_buf_len += amt_read;
5051 if (amt_read == 0) {
5152 buf_resize(out_buf, actual_buf_len);
52 return;
53 return 0;
5354 }
5455
5556 buf_resize(out_buf, actual_buf_len + buf_size);
5657 }
58 zig_unreachable();
5759}
5860
5961void os_path_split(Buf *full_path, Buf *out_dirname, Buf *out_basename) {
......@@ -142,46 +144,7 @@ void os_write_file(Buf *full_path, Buf *contents) {
142144}
143145
144146int os_fetch_file(FILE *f, Buf *out_contents) {
145 int fd = fileno(f);
146 struct stat st;
147 if (fstat(fd, &st)) {
148 switch (errno) {
149 case EACCES:
150 return ErrorAccess;
151 case ENOENT:
152 return ErrorFileNotFound;
153 case ENOMEM:
154 return ErrorSystemResources;
155 case EINTR:
156 return ErrorInterrupted;
157 case EINVAL:
158 zig_unreachable();
159 default:
160 return ErrorFileSystem;
161 }
162 }
163 off_t big_size = st.st_size;
164 if (big_size > INT_MAX) {
165 return ErrorFileTooBig;
166 }
167 int size = (int)big_size;
168
169 buf_resize(out_contents, size);
170 ssize_t ret = read(fd, buf_ptr(out_contents), size);
171
172 if (ret != size) {
173 switch (errno) {
174 case EINTR:
175 return ErrorInterrupted;
176 case EINVAL:
177 case EISDIR:
178 zig_unreachable();
179 default:
180 return ErrorFileSystem;
181 }
182 }
183
184 return 0;
147 return read_all_fd_stream(fileno(f), out_contents);
185148}
186149
187150int os_fetch_file_path(Buf *full_path, Buf *out_contents) {