1 1.1 christos #include <stdio.h> 2 1.1 christos #include <fcntl.h> 3 1.1 christos #include <string.h> 4 1.1 christos #include <stdlib.h> 5 1.1 christos 6 1.1 christos #include <uv.h> 7 1.1 christos 8 1.1 christos typedef struct { 9 1.1 christos uv_write_t req; 10 1.1 christos uv_buf_t buf; 11 1.1 christos } write_req_t; 12 1.1 christos 13 1.1 christos uv_loop_t *loop; 14 1.1 christos uv_pipe_t stdin_pipe; 15 1.1 christos uv_pipe_t stdout_pipe; 16 1.1 christos uv_pipe_t file_pipe; 17 1.1 christos 18 1.1 christos void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) { 19 1.1 christos *buf = uv_buf_init((char*) malloc(suggested_size), suggested_size); 20 1.1 christos } 21 1.1 christos 22 1.1 christos void free_write_req(uv_write_t *req) { 23 1.1 christos write_req_t *wr = (write_req_t*) req; 24 1.1 christos free(wr->buf.base); 25 1.1 christos free(wr); 26 1.1 christos } 27 1.1 christos 28 1.1 christos void on_stdout_write(uv_write_t *req, int status) { 29 1.1 christos free_write_req(req); 30 1.1 christos } 31 1.1 christos 32 1.1 christos void on_file_write(uv_write_t *req, int status) { 33 1.1 christos free_write_req(req); 34 1.1 christos } 35 1.1 christos 36 1.1 christos void write_data(uv_stream_t *dest, size_t size, uv_buf_t buf, uv_write_cb cb) { 37 1.1 christos write_req_t *req = (write_req_t*) malloc(sizeof(write_req_t)); 38 1.1 christos req->buf = uv_buf_init((char*) malloc(size), size); 39 1.1 christos memcpy(req->buf.base, buf.base, size); 40 1.1 christos uv_write((uv_write_t*) req, (uv_stream_t*)dest, &req->buf, 1, cb); 41 1.1 christos } 42 1.1 christos 43 1.1 christos void read_stdin(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) { 44 1.1 christos if (nread < 0){ 45 1.1 christos if (nread == UV_EOF){ 46 1.1 christos // end of file 47 1.1 christos uv_close((uv_handle_t *)&stdin_pipe, NULL); 48 1.1 christos uv_close((uv_handle_t *)&stdout_pipe, NULL); 49 1.1 christos uv_close((uv_handle_t *)&file_pipe, NULL); 50 1.1 christos } 51 1.1 christos } else if (nread > 0) { 52 1.1 christos write_data((uv_stream_t *)&stdout_pipe, nread, *buf, on_stdout_write); 53 1.1 christos write_data((uv_stream_t *)&file_pipe, nread, *buf, on_file_write); 54 1.1 christos } 55 1.1 christos 56 1.1 christos // OK to free buffer as write_data copies it. 57 1.1 christos if (buf->base) 58 1.1 christos free(buf->base); 59 1.1 christos } 60 1.1 christos 61 1.1 christos int main(int argc, char **argv) { 62 1.1 christos loop = uv_default_loop(); 63 1.1 christos 64 1.1 christos uv_pipe_init(loop, &stdin_pipe, 0); 65 1.1 christos uv_pipe_open(&stdin_pipe, 0); 66 1.1 christos 67 1.1 christos uv_pipe_init(loop, &stdout_pipe, 0); 68 1.1 christos uv_pipe_open(&stdout_pipe, 1); 69 1.1 christos 70 1.1 christos uv_fs_t file_req; 71 1.1 christos int fd = uv_fs_open(loop, &file_req, argv[1], O_CREAT | O_RDWR, 0644, NULL); 72 1.1 christos uv_pipe_init(loop, &file_pipe, 0); 73 1.1 christos uv_pipe_open(&file_pipe, fd); 74 1.1 christos 75 1.1 christos uv_read_start((uv_stream_t*)&stdin_pipe, alloc_buffer, read_stdin); 76 1.1 christos 77 1.1 christos uv_run(loop, UV_RUN_DEFAULT); 78 1.1 christos return 0; 79 1.1 christos } 80