Created
October 6, 2014 21:13
-
-
Save splinterofchaos/fc1f27bbd5aa38ef32c3 to your computer and use it in GitHub Desktop.
Uv stdout and stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main() | |
printf "yo!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a.out : err.c | |
gcc err.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <uv.h> | |
#include <string.h> | |
#include <stdio.h> | |
#include <assert.h> | |
char output[1024]; | |
int used = 0; | |
static void on_alloc(uv_handle_t* handle, | |
size_t suggested_size, | |
uv_buf_t* buf) { | |
buf->base = output; | |
buf->len = 1024; | |
} | |
static void on_read(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) { | |
if (nread > 0) { | |
output[nread] = 0; | |
puts(output); | |
} else if (nread < 0) { | |
assert(nread == UV_EOF); | |
uv_close((uv_handle_t*)tcp, NULL); | |
} | |
} | |
static void exit_cb(uv_process_t* process, | |
int64_t exit_status, | |
int term_signal) { | |
uv_close((uv_handle_t*)process, NULL); | |
} | |
int main() | |
{ | |
uv_process_t process; | |
uv_pipe_t out; | |
uv_process_options_t options = {0}; | |
uv_stdio_container_t stdio[3]; | |
char *args[] = { "make", 0 }; | |
//options.file = "echo"; | |
options.file = "make"; | |
options.args = args; | |
options.exit_cb = exit_cb; | |
int r; | |
r = uv_pipe_init(uv_default_loop(), &out, 0); | |
assert(!r); | |
options.stdio = stdio; | |
options.stdio[0].flags = UV_IGNORE; | |
options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE; | |
options.stdio[1].data.stream = (uv_stream_t*)&out; | |
options.stdio_count = 2; | |
r = uv_spawn(uv_default_loop(), &process, &options); | |
assert(!r); | |
r = uv_read_start((uv_stream_t*)&out, on_alloc, on_read); | |
assert(!r); | |
r = uv_run(uv_default_loop(), UV_RUN_DEFAULT); | |
assert(!r); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment