xref: /netbsd-src/external/mit/libuv/dist/docs/code/uvcat/main.c (revision 5f2f42719cd62ff11fd913b40b7ce19f07c4fd25)
1*0e552da7Schristos #include <assert.h>
2*0e552da7Schristos #include <stdio.h>
3*0e552da7Schristos #include <fcntl.h>
4*0e552da7Schristos #include <uv.h>
5*0e552da7Schristos 
6*0e552da7Schristos void on_read(uv_fs_t *req);
7*0e552da7Schristos 
8*0e552da7Schristos uv_fs_t open_req;
9*0e552da7Schristos uv_fs_t read_req;
10*0e552da7Schristos uv_fs_t write_req;
11*0e552da7Schristos 
12*0e552da7Schristos static char buffer[1024];
13*0e552da7Schristos 
14*0e552da7Schristos static uv_buf_t iov;
15*0e552da7Schristos 
on_write(uv_fs_t * req)16*0e552da7Schristos void on_write(uv_fs_t *req) {
17*0e552da7Schristos     if (req->result < 0) {
18*0e552da7Schristos         fprintf(stderr, "Write error: %s\n", uv_strerror((int)req->result));
19*0e552da7Schristos     }
20*0e552da7Schristos     else {
21*0e552da7Schristos         uv_fs_read(uv_default_loop(), &read_req, open_req.result, &iov, 1, -1, on_read);
22*0e552da7Schristos     }
23*0e552da7Schristos }
24*0e552da7Schristos 
on_read(uv_fs_t * req)25*0e552da7Schristos void on_read(uv_fs_t *req) {
26*0e552da7Schristos     if (req->result < 0) {
27*0e552da7Schristos         fprintf(stderr, "Read error: %s\n", uv_strerror(req->result));
28*0e552da7Schristos     }
29*0e552da7Schristos     else if (req->result == 0) {
30*0e552da7Schristos         uv_fs_t close_req;
31*0e552da7Schristos         // synchronous
32*0e552da7Schristos         uv_fs_close(uv_default_loop(), &close_req, open_req.result, NULL);
33*0e552da7Schristos     }
34*0e552da7Schristos     else if (req->result > 0) {
35*0e552da7Schristos         iov.len = req->result;
36*0e552da7Schristos         uv_fs_write(uv_default_loop(), &write_req, 1, &iov, 1, -1, on_write);
37*0e552da7Schristos     }
38*0e552da7Schristos }
39*0e552da7Schristos 
on_open(uv_fs_t * req)40*0e552da7Schristos void on_open(uv_fs_t *req) {
41*0e552da7Schristos     // The request passed to the callback is the same as the one the call setup
42*0e552da7Schristos     // function was passed.
43*0e552da7Schristos     assert(req == &open_req);
44*0e552da7Schristos     if (req->result >= 0) {
45*0e552da7Schristos         iov = uv_buf_init(buffer, sizeof(buffer));
46*0e552da7Schristos         uv_fs_read(uv_default_loop(), &read_req, req->result,
47*0e552da7Schristos                    &iov, 1, -1, on_read);
48*0e552da7Schristos     }
49*0e552da7Schristos     else {
50*0e552da7Schristos         fprintf(stderr, "error opening file: %s\n", uv_strerror((int)req->result));
51*0e552da7Schristos     }
52*0e552da7Schristos }
53*0e552da7Schristos 
main(int argc,char ** argv)54*0e552da7Schristos int main(int argc, char **argv) {
55*0e552da7Schristos     uv_fs_open(uv_default_loop(), &open_req, argv[1], O_RDONLY, 0, on_open);
56*0e552da7Schristos     uv_run(uv_default_loop(), UV_RUN_DEFAULT);
57*0e552da7Schristos 
58*0e552da7Schristos     uv_fs_req_cleanup(&open_req);
59*0e552da7Schristos     uv_fs_req_cleanup(&read_req);
60*0e552da7Schristos     uv_fs_req_cleanup(&write_req);
61*0e552da7Schristos     return 0;
62*0e552da7Schristos }
63