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