xref: /haiku/src/tools/fs_shell/uio.cpp (revision 5115ca085884f7b604a3d607688f0ca20fb7cf57)
1 /*
2  * Copyright 2007, Ingo Weinhold, bonefish@cs.tu-berlin.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include "compatibility.h"
7 
8 #include "fssh_uio.h"
9 
10 #include <new>
11 
12 #include <errno.h>
13 #include <sys/uio.h>
14 
15 
16 static const fssh_size_t kMaxIOVecs = 1024;
17 
18 
19 bool
20 prepare_iovecs(const struct fssh_iovec *vecs, fssh_size_t count,
21 	struct iovec* systemVecs)
22 {
23 	if (count > kMaxIOVecs) {
24 		errno = B_BAD_VALUE;
25 		return false;
26 	}
27 
28 	for (fssh_size_t i = 0; i < count; i++) {
29 		systemVecs[i].iov_base = vecs[i].iov_base;
30 		systemVecs[i].iov_len = vecs[i].iov_len;
31 	}
32 
33 	return true;
34 }
35 
36 
37 fssh_ssize_t
38 fssh_readv(int fd, const struct fssh_iovec *vector, fssh_size_t count)
39 {
40 	struct iovec systemVecs[kMaxIOVecs];
41 	if (!prepare_iovecs(vector, count, systemVecs))
42 		return -1;
43 
44 	return readv(fd, systemVecs, count);
45 }
46 
47 
48 fssh_ssize_t
49 fssh_readv_pos(int fd, fssh_off_t pos, const struct fssh_iovec *vec,
50 	fssh_size_t count)
51 {
52 	struct iovec systemVecs[kMaxIOVecs];
53 	if (!prepare_iovecs(vec, count, systemVecs))
54 		return -1;
55 
56 	return readv_pos(fd, pos, systemVecs, count);
57 }
58 
59 
60 fssh_ssize_t
61 fssh_writev(int fd, const struct fssh_iovec *vector, fssh_size_t count)
62 {
63 	struct iovec systemVecs[kMaxIOVecs];
64 	if (!prepare_iovecs(vector, count, systemVecs))
65 		return -1;
66 
67 	return writev(fd, systemVecs, count);
68 }
69 
70 
71 fssh_ssize_t
72 fssh_writev_pos(int fd, fssh_off_t pos, const struct fssh_iovec *vec,
73 				fssh_size_t count)
74 {
75 	struct iovec systemVecs[kMaxIOVecs];
76 	if (!prepare_iovecs(vec, count, systemVecs))
77 		return -1;
78 
79 	return writev_pos(fd, pos, systemVecs, count);
80 }
81