Implemented pipe on the windows side

This commit is contained in:
Snaipe 2015-03-25 00:22:33 +01:00
parent 70a3c91dc5
commit 130244d2ca
2 changed files with 39 additions and 4 deletions

View file

@ -1,7 +1,11 @@
#include "posix-compat.h"
#ifdef _WIN32
# define VC_EXTRALEAN
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# include <io.h>
# include <fcntl.h>
#else
# include <unistd.h>
# include <sys/wait.h>
@ -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)

View file

@ -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 <stdio.h>
# undef _POSIX_SOURCE
# ifdef TMP_POSIX
# undef _POSIX_SOURCE
# undef TMP_POSIX
# endif
# ifdef _WIN32
# define WEXITSTATUS(Status) (((Status) & 0xFF00) >> 8)