view paper/src/fileread.c @ 45:675cd2c69450

add
author mir3636
date Mon, 11 Feb 2019 08:28:55 +0900
parents
children d3ed28a7964f
line wrap: on
line source

__code cbc_fileread1 (int r)
{   
    struct file *f = proc->cbc_arg.cbc_console_arg.f;
    __code (*next)(int ret) = cbc_ret;
    if (r > 0) 
        f->off += r;
    iunlock(f->ip);
    goto next(r);
}

__code cbc_fileread (struct file *f, char *addr, int n, __code (*next)(int ret))
{   
    if (f->readable == 0) {
        goto next(-1);
    }
    
    if (f->type == FD_PIPE) {
        goto cbc_piperead(f->pipe, addr, n, next);
        goto next(-1);
    }
    
    if (f->type == FD_INODE) {
        ilock(f->ip);
        proc->cbc_arg.cbc_console_arg.f = f;
        goto cbc_readi(f->ip, addr, f->off, n, cbc_fileread1);
    }
    
    goto cbc_panic("fileread");
}

// Read from file f.
int fileread (struct file *f, char *addr, int n)
{   
    int r;
    
    if (f->readable == 0) {
        return -1;
    }
    
    if (f->type == FD_PIPE) {
        return piperead(f->pipe, addr, n);
    }
    
    if (f->type == FD_INODE) {
        ilock(f->ip);

        if ((r = readi(f->ip, addr, f->off, n)) > 0) {
            f->off += r;
        }

        iunlock(f->ip);

        return r;
    }

    panic("fileread");
}