| ... | ... | @@ -9,6 +9,30 @@ |
| 9 | 9 | #include "util.hpp" |
| 10 | 10 | #include "error.hpp" |
| 11 | 11 | |
| 12 | #if defined(_WIN32) |
| 13 | #define ZIG_OS_WINDOWS |
| 14 | |
| 15 | #if !defined(NOMINMAX) |
| 16 | #define NOMINMAX |
| 17 | #endif |
| 18 | |
| 19 | #if !defined(VC_EXTRALEAN) |
| 20 | #define VC_EXTRALEAN |
| 21 | #endif |
| 22 | |
| 23 | #if !defined(WIN32_LEAN_AND_MEAN) |
| 24 | #define WIN32_LEAN_AND_MEAN |
| 25 | #endif |
| 26 | |
| 27 | #if !defined(UNICODE) |
| 28 | #define UNICODE |
| 29 | #endif |
| 30 | |
| 31 | #include <windows.h> |
| 32 | #include <io.h> |
| 33 | #else |
| 34 | #define ZIG_OS_POSIX |
| 35 | |
| 12 | 36 | #include <unistd.h> |
| 13 | 37 | #include <errno.h> |
| 14 | 38 | #include <sys/types.h> |
| ... | ... | @@ -17,7 +41,10 @@ |
| 17 | 41 | #include <fcntl.h> |
| 18 | 42 | #include <limits.h> |
| 19 | 43 | |
| 20 | | void os_spawn_process(const char *exe, ZigList<const char *> &args, int *return_code) { |
| 44 | #endif |
| 45 | |
| 46 | |
| 47 | static void os_spawn_process_posix(const char *exe, ZigList<const char *> &args, int *return_code) { |
| 21 | 48 | pid_t pid = fork(); |
| 22 | 49 | if (pid == -1) |
| 23 | 50 | zig_panic("fork failed"); |
| ... | ... | @@ -37,24 +64,20 @@ void os_spawn_process(const char *exe, ZigList<const char *> &args, int *return_ |
| 37 | 64 | } |
| 38 | 65 | } |
| 39 | 66 | |
| 40 | | static int read_all_fd_stream(int fd, Buf *out_buf) { |
| 41 | | static const ssize_t buf_size = 0x2000; |
| 42 | | buf_resize(out_buf, buf_size); |
| 43 | | ssize_t actual_buf_len = 0; |
| 44 | | for (;;) { |
| 45 | | ssize_t amt_read = read(fd, buf_ptr(out_buf) + actual_buf_len, buf_size); |
| 46 | | if (amt_read < 0) { |
| 47 | | return ErrorFileSystem; |
| 48 | | } |
| 49 | | actual_buf_len += amt_read; |
| 50 | | if (amt_read == 0) { |
| 51 | | buf_resize(out_buf, actual_buf_len); |
| 52 | | return 0; |
| 53 | | } |
| 67 | #if defined(ZIG_OS_WINDOWS) |
| 68 | static void os_spawn_process_windows(const char *exe, ZigList<const char *> &args, int *return_code) { |
| 69 | zig_panic("TODO os_spawn_process_windows"); |
| 70 | } |
| 71 | #endif |
| 54 | 72 | |
| 55 | | buf_resize(out_buf, actual_buf_len + buf_size); |
| 56 | | } |
| 57 | | zig_unreachable(); |
| 73 | void os_spawn_process(const char *exe, ZigList<const char *> &args, int *return_code) { |
| 74 | #if defined(ZIG_OS_WINDOWS) |
| 75 | os_spawn_process_windows(exe, args, return_code); |
| 76 | #elif defined(ZIG_OS_POSIX) |
| 77 | os_spawn_process_posix(exe, args, return_code); |
| 78 | #else |
| 79 | #error "missing os_spawn_process implementation" |
| 80 | #endif |
| 58 | 81 | } |
| 59 | 82 | |
| 60 | 83 | void os_path_split(Buf *full_path, Buf *out_dirname, Buf *out_basename) { |
| ... | ... | @@ -101,7 +124,30 @@ int os_path_real(Buf *rel_path, Buf *out_abs_path) { |
| 101 | 124 | return ErrorNone; |
| 102 | 125 | } |
| 103 | 126 | |
| 104 | | void os_exec_process(const char *exe, ZigList<const char *> &args, |
| 127 | int os_fetch_file(FILE *f, Buf *out_buf) { |
| 128 | static const ssize_t buf_size = 0x2000; |
| 129 | buf_resize(out_buf, buf_size); |
| 130 | ssize_t actual_buf_len = 0; |
| 131 | for (;;) { |
| 132 | size_t amt_read = fread(buf_ptr(out_buf) + actual_buf_len, 1, buf_size, f); |
| 133 | actual_buf_len += amt_read; |
| 134 | if (amt_read != buf_size) { |
| 135 | if (feof(f)) { |
| 136 | buf_resize(out_buf, actual_buf_len); |
| 137 | return 0; |
| 138 | } else { |
| 139 | return ErrorFileSystem; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | buf_resize(out_buf, actual_buf_len + buf_size); |
| 144 | } |
| 145 | zig_unreachable(); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | #if defined(ZIG_OS_POSIX) |
| 150 | static void os_exec_process_posix(const char *exe, ZigList<const char *> &args, |
| 105 | 151 | int *return_code, Buf *out_stderr, Buf *out_stdout) |
| 106 | 152 | { |
| 107 | 153 | int stdin_pipe[2]; |
| ... | ... | @@ -146,27 +192,37 @@ void os_exec_process(const char *exe, ZigList<const char *> &args, |
| 146 | 192 | |
| 147 | 193 | waitpid(pid, return_code, 0); |
| 148 | 194 | |
| 149 | | read_all_fd_stream(stdout_pipe[0], out_stdout); |
| 150 | | read_all_fd_stream(stderr_pipe[0], out_stderr); |
| 195 | os_fetch_file(fdopen(stdout_pipe[0], "rb"), out_stdout); |
| 196 | os_fetch_file(fdopen(stderr_pipe[0], "rb"), out_stderr); |
| 151 | 197 | |
| 152 | 198 | } |
| 153 | 199 | } |
| 200 | #endif |
| 201 | |
| 202 | void os_exec_process(const char *exe, ZigList<const char *> &args, |
| 203 | int *return_code, Buf *out_stderr, Buf *out_stdout) |
| 204 | { |
| 205 | #if defined(ZIG_OS_WINDOWS) |
| 206 | return os_exec_process_windows(exe, args, return_code, out_stderr, out_stdout); |
| 207 | #elif defined(ZIG_OS_POSIX) |
| 208 | return os_exec_process_posix(exe, args, return_code, out_stderr, out_stdout); |
| 209 | #else |
| 210 | #error "missing os_exec_process implementation" |
| 211 | #endif |
| 212 | } |
| 154 | 213 | |
| 155 | 214 | void os_write_file(Buf *full_path, Buf *contents) { |
| 156 | | int fd; |
| 157 | | if ((fd = open(buf_ptr(full_path), O_CREAT|O_CLOEXEC|O_WRONLY|O_TRUNC, S_IRWXU)) == -1) |
| 215 | FILE *f = fopen(buf_ptr(full_path), "wb"); |
| 216 | if (!f) { |
| 158 | 217 | zig_panic("open failed"); |
| 159 | | ssize_t amt_written = write(fd, buf_ptr(contents), buf_len(contents)); |
| 218 | } |
| 219 | size_t amt_written = fwrite(buf_ptr(contents), 1, buf_len(contents), f); |
| 160 | 220 | if (amt_written != buf_len(contents)) |
| 161 | 221 | zig_panic("write failed: %s", strerror(errno)); |
| 162 | | if (close(fd) == -1) |
| 222 | if (fclose(f)) |
| 163 | 223 | zig_panic("close failed"); |
| 164 | 224 | } |
| 165 | 225 | |
| 166 | | int os_fetch_file(FILE *f, Buf *out_contents) { |
| 167 | | return read_all_fd_stream(fileno(f), out_contents); |
| 168 | | } |
| 169 | | |
| 170 | 226 | int os_fetch_file_path(Buf *full_path, Buf *out_contents) { |
| 171 | 227 | FILE *f = fopen(buf_ptr(full_path), "rb"); |
| 172 | 228 | if (!f) { |
| ... | ... | @@ -192,6 +248,9 @@ int os_fetch_file_path(Buf *full_path, Buf *out_contents) { |
| 192 | 248 | } |
| 193 | 249 | |
| 194 | 250 | int os_get_cwd(Buf *out_cwd) { |
| 251 | #if defined(ZIG_OS_WINDOWS) |
| 252 | zig_panic("TODO os_get_cwd for windows"); |
| 253 | #elif defined(ZIG_OS_POSIX) |
| 195 | 254 | int err = ERANGE; |
| 196 | 255 | buf_resize(out_cwd, 512); |
| 197 | 256 | while (err == ERANGE) { |
| ... | ... | @@ -202,10 +261,19 @@ int os_get_cwd(Buf *out_cwd) { |
| 202 | 261 | zig_panic("unable to get cwd: %s", strerror(err)); |
| 203 | 262 | |
| 204 | 263 | return 0; |
| 264 | #else |
| 265 | #error "missing os_get_cwd implementation" |
| 266 | #endif |
| 205 | 267 | } |
| 206 | 268 | |
| 207 | 269 | bool os_stderr_tty(void) { |
| 208 | | return isatty(STDERR_FILENO); |
| 270 | #if defined(ZIG_OS_WINDOWS) |
| 271 | return _isatty(STDERR_FILENO) != 0; |
| 272 | #elif defined(ZIG_OS_POSIX) |
| 273 | return isatty(STDERR_FILENO) != 0; |
| 274 | #else |
| 275 | #error "missing os_stderr_tty implementation" |
| 276 | #endif |
| 209 | 277 | } |
| 210 | 278 | |
| 211 | 279 | int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path) { |
| ... | ... | @@ -217,10 +285,15 @@ int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path) { |
| 217 | 285 | return ErrorFileSystem; |
| 218 | 286 | } |
| 219 | 287 | |
| 220 | | ssize_t amt_written = write(fd, buf_ptr(contents), buf_len(contents)); |
| 288 | FILE *f = fdopen(fd, "wb"); |
| 289 | if (!f) { |
| 290 | zig_panic("fdopen failed"); |
| 291 | } |
| 292 | |
| 293 | size_t amt_written = fwrite(buf_ptr(contents), 1, buf_len(contents), f); |
| 221 | 294 | if (amt_written != buf_len(contents)) |
| 222 | 295 | zig_panic("write failed: %s", strerror(errno)); |
| 223 | | if (close(fd) == -1) |
| 296 | if (fclose(f)) |
| 224 | 297 | zig_panic("close failed"); |
| 225 | 298 | |
| 226 | 299 | return 0; |