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