diff --git a/src/posix-compat.c b/src/posix-compat.c index 46e26e4..7d7f0e4 100644 --- a/src/posix-compat.c +++ b/src/posix-compat.c @@ -1,7 +1,11 @@ #include "posix-compat.h" #ifdef _WIN32 +# define VC_EXTRALEAN +# define WIN32_LEAN_AND_MEAN # include +# include +# include #else # include # include @@ -21,7 +25,7 @@ struct proc_handle { struct pipe_handle { #ifdef _WIN32 - // TODO + HANDLE fhs[2]; #else int fds[2]; #endif @@ -49,22 +53,47 @@ void wait_process(s_proc_handle *handle, int *status) { } FILE *pipe_in(s_pipe_handle *p) { +#ifdef _WIN32 + CloseHandle(p->fhs[1]); + int fd = _open_osfhandle(p->fhs[0], _O_RDONLY); + if (fd == -1) + return NULL; + FILE *in = _fdopen(, "r"); +#else close(p->fds[1]); FILE *in = fdopen(p->fds[0], "r"); +#endif + if (!in) + return NULL; + setvbuf(in, NULL, _IONBF, 0); return in; } FILE *pipe_out(s_pipe_handle *p) { +#ifdef _WIN32 + CloseHandle(p->fhs[0]); + int fd = _open_osfhandle(p->fhs[1], _O_WRONLY); + if (fd == -1) + return NULL; + FILE *out = _fdopen(fd, "w"); +#else close(p->fds[0]); FILE *out = fdopen(p->fds[1], "w"); +#endif + if (!out) + return NULL; + setvbuf(out, NULL, _IONBF, 0); return out; } s_pipe_handle *stdpipe() { #ifdef _WIN32 - // TODO + HANDLE fhs[2]; + if (!CreatePipe(fhs, fhs + 1, NULL, 0)) + return NULL; + return unique_ptr(s_pipe_handle, {{ fhs[0], fhs[1] }}); #else int fds[2] = { -1, -1 }; if (pipe(fds) == -1) diff --git a/src/posix-compat.h b/src/posix-compat.h index 83567f8..8f53627 100644 --- a/src/posix-compat.h +++ b/src/posix-compat.h @@ -1,9 +1,15 @@ #ifndef POSIX_COMPAT_H_ # define POSIX_COMPAT_H_ -# define _POSIX_SOURCE 1 +# if !defined(_POSIX_SOURCE) +# define _POSIX_SOURCE 1 +# define TMP_POSIX +# endif # include -# undef _POSIX_SOURCE +# ifdef TMP_POSIX +# undef _POSIX_SOURCE +# undef TMP_POSIX +# endif # ifdef _WIN32 # define WEXITSTATUS(Status) (((Status) & 0xFF00) >> 8)