1 #include <unistd.h>
2 #include <string.h>
3 #include <sys/uio.h>
4
5 int
readv(int fd,struct iovec * v,int ent)6 readv(int fd, struct iovec *v, int ent)
7 {
8 int x, i, n, len;
9 char buf[10*1024];
10
11 for(len = i = 0; i < ent; i++)
12 len += v[i].iov_len;
13 if(len > sizeof(buf))
14 len = sizeof(buf);
15
16 len = read(fd, buf, len);
17 if(len <= 0)
18 return len;
19
20 for(n = i = 0; n < len && i < ent; i++){
21 x = len - n;
22 if(x > v[i].iov_len)
23 x = v[i].iov_len;
24 memmove(v[i].iov_base, buf + n, x);
25 n += x;
26 }
27
28 return len;
29 }
30