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