Sha256: 074ab14d54a46367da792ce698f0cbd87f9a3b161c91008da360d83297bd74c9

Contents?: true

Size: 1.62 KB

Versions: 15

Compression:

Stored size: 1.62 KB

Contents

#include <assert.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <uv.h>

void on_read(uv_fs_t *req);

uv_fs_t open_req;
uv_fs_t read_req;
uv_fs_t write_req;

static char buffer[1024];

static uv_buf_t iov;

void on_write(uv_fs_t *req) {
    if (req->result < 0) {
        fprintf(stderr, "Write error: %s\n", uv_strerror((int)req->result));
    }
    else {
        uv_fs_read(uv_default_loop(), &read_req, open_req.result, &iov, 1, -1, on_read);
    }
}

void on_read(uv_fs_t *req) {
    if (req->result < 0) {
        fprintf(stderr, "Read error: %s\n", uv_strerror(req->result));
    }
    else if (req->result == 0) {
        uv_fs_t close_req;
        // synchronous
        uv_fs_close(uv_default_loop(), &close_req, open_req.result, NULL);
    }
    else if (req->result > 0) {
        iov.len = req->result;
        uv_fs_write(uv_default_loop(), &write_req, 1, &iov, 1, -1, on_write);
    }
}

void on_open(uv_fs_t *req) {
    // The request passed to the callback is the same as the one the call setup
    // function was passed.
    assert(req == &open_req);
    if (req->result >= 0) {
        iov = uv_buf_init(buffer, sizeof(buffer));
        uv_fs_read(uv_default_loop(), &read_req, req->result,
                   &iov, 1, -1, on_read);
    }
    else {
        fprintf(stderr, "error opening file: %s\n", uv_strerror((int)req->result));
    }
}

int main(int argc, char **argv) {
    uv_fs_open(uv_default_loop(), &open_req, argv[1], O_RDONLY, 0, on_open);
    uv_run(uv_default_loop(), UV_RUN_DEFAULT);

    uv_fs_req_cleanup(&open_req);
    uv_fs_req_cleanup(&read_req);
    uv_fs_req_cleanup(&write_req);
    return 0;
}

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
mt-libuv-4.1.04 ext/libuv/docs/code/uvcat/main.c
mt-libuv-4.1.03 ext/libuv/docs/code/uvcat/main.c
mt-libuv-4.1.02 ext/libuv/docs/code/uvcat/main.c
libuv-4.0.9 ext/libuv/docs/code/uvcat/main.c
libuv-4.0.2 ext/libuv/docs/code/uvcat/main.c
libuv-4.0.1 ext/libuv/docs/code/uvcat/main.c
libuv-4.0.0 ext/libuv/docs/code/uvcat/main.c
libuv-3.3.0 ext/libuv/docs/code/uvcat/main.c
libuv-3.2.4 ext/libuv/docs/code/uvcat/main.c
libuv-3.2.3 ext/libuv/docs/code/uvcat/main.c
libuv-3.2.2 ext/libuv/docs/code/uvcat/main.c
libuv-3.2.1 ext/libuv/docs/code/uvcat/main.c
libuv-3.2.0 ext/libuv/docs/code/uvcat/main.c
libuv-3.1.9 ext/libuv/docs/code/uvcat/main.c
libuv-3.1.8 ext/libuv/docs/code/uvcat/main.c