aboutsummaryrefslogtreecommitdiff
path: root/src/base/bufio/write.c
blob: 8b64055d4feb1bcbba98f293259b4da8ef40598b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "internal.h"

intptr
bio·write(io·Header *io, intptr len, void *buf)
{
    char *b;
    intptr c, o, nw, n;

    b = buf;
    c = len;
    o = io->olen;

    while(c > 0){
        n = -o;
        if(n > c)
            n = c;
        if(n == 0){
            if(io->state != io·BufWtr)
                return io·BufEof;
            switch(sys·write(io->fd, io->cap, io->b, &nw)){
            case 0:
                if(nw != io->cap) goto error;
                io->pos += nw;
                o = -io->cap;
                continue;
            case sys·ErrorInterrupt:
                io->state = io·BufNil;
                /* fallthrough */
            default: error:
                return io·BufEof;
            }
        }
        mem·move(io->e+o, b, n);
        o += n;
        c -= n;
        b += n;
    }
    io->olen = o;
    return len-c;
}