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...@@ -38,22 +38,24 @@ void os_spawn_process(const char *exe, ZigList<const char *> &args, bool detache
38 zig_panic("execvp failed: %s", strerror(errno));38 zig_panic("execvp failed: %s", strerror(errno));
39}39}
4040
41static void read_all_fd_stream(int fd, Buf *out_buf) {41static int read_all_fd_stream(int fd, Buf *out_buf) {
42 static const ssize_t buf_size = 0x2000;42 static const ssize_t buf_size = 0x2000;
43 buf_resize(out_buf, buf_size);43 buf_resize(out_buf, buf_size);
44 ssize_t actual_buf_len = 0;44 ssize_t actual_buf_len = 0;
45 for (;;) {45 for (;;) {
46 ssize_t amt_read = read(fd, buf_ptr(out_buf), buf_len(out_buf));46 ssize_t amt_read = read(fd, buf_ptr(out_buf), buf_len(out_buf));
47 if (amt_read < 0)47 if (amt_read < 0) {
48 zig_panic("fd read error");48 return ErrorFileSystem;
49 }
49 actual_buf_len += amt_read;50 actual_buf_len += amt_read;
50 if (amt_read == 0) {51 if (amt_read == 0) {
51 buf_resize(out_buf, actual_buf_len);52 buf_resize(out_buf, actual_buf_len);
52 return;53 return 0;
53 }54 }
5455
55 buf_resize(out_buf, actual_buf_len + buf_size);56 buf_resize(out_buf, actual_buf_len + buf_size);
56 }57 }
58 zig_unreachable();
57}59}
5860
59void os_path_split(Buf *full_path, Buf *out_dirname, Buf *out_basename) {61void 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) {...@@ -142,46 +144,7 @@ void os_write_file(Buf *full_path, Buf *contents) {
142}144}
143145
144int os_fetch_file(FILE *f, Buf *out_contents) {146int os_fetch_file(FILE *f, Buf *out_contents) {
145 int fd = fileno(f);147 return read_all_fd_stream(fileno(f), out_contents);
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;
185}148}
186149
187int os_fetch_file_path(Buf *full_path, Buf *out_contents) {150int os_fetch_file_path(Buf *full_path, Buf *out_contents) {