Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / support / sys-uio.c
1 /*
2  * <sys/uio.h> wrapper functions.
3  *
4  * Authors:
5  *   Steffen Kiess (s-kiess@web.de)
6  *
7  * Copyright (C) 2012 Steffen Kiess
8  */
9
10 #ifndef _GNU_SOURCE
11 #define _GNU_SOURCE
12 #endif /* ndef _GNU_SOURCE */
13
14 #include "sys-uio.h"
15
16 #include <sys/uio.h>
17
18 #include "map.h"
19 #include "mph.h"
20
21 G_BEGIN_DECLS
22
23 struct iovec*
24 _mph_from_iovec_array (struct Mono_Posix_Iovec *iov, gint32 iovcnt)
25 {
26         struct iovec* v;
27         gint32 i;
28
29         if (iovcnt < 0) {
30                 errno = EINVAL;
31                 return NULL;
32         }
33
34         v = malloc (iovcnt * sizeof (struct iovec));
35         if (!v) {
36                 return NULL;
37         }
38
39         for (i = 0; i < iovcnt; i++) {
40                 if (Mono_Posix_FromIovec (&iov[i], &v[i]) != 0) {
41                         free (v);
42                         return NULL;
43                 }
44         }
45
46         return v;
47 }
48
49 #ifdef HAVE_READV
50 gint64
51 Mono_Posix_Syscall_readv (int dirfd, struct Mono_Posix_Iovec *iov, gint32 iovcnt)
52 {
53         struct iovec* v;
54         gint64 res;
55
56         v = _mph_from_iovec_array (iov, iovcnt);
57         if (!v) {
58                 return -1;
59         }
60
61         res = readv(dirfd, v, iovcnt);
62         free (v);
63         return res;
64 }
65 #endif /* def HAVE_READV */
66
67 #ifdef HAVE_WRITEV
68 gint64
69 Mono_Posix_Syscall_writev (int dirfd, struct Mono_Posix_Iovec *iov, gint32 iovcnt)
70 {
71         struct iovec* v;
72         gint64 res;
73
74         v = _mph_from_iovec_array (iov, iovcnt);
75         if (!v) {
76                 return -1;
77         }
78
79         res = writev (dirfd, v, iovcnt);
80         free (v);
81         return res;
82 }
83 #endif /* def HAVE_WRITEV */
84
85 #ifdef HAVE_PREADV
86 gint64
87 Mono_Posix_Syscall_preadv (int dirfd, struct Mono_Posix_Iovec *iov, gint32 iovcnt, gint64 off)
88 {
89         struct iovec* v;
90         gint64 res;
91
92         mph_return_if_off_t_overflow (off);
93
94         v = _mph_from_iovec_array (iov, iovcnt);
95         if (!v) {
96                 return -1;
97         }
98
99         res = preadv (dirfd, v, iovcnt, (off_t) off);
100         free (v);
101         return res;
102 }
103 #endif /* def HAVE_PREADV */
104
105 #ifdef HAVE_PWRITEV
106 gint64
107 Mono_Posix_Syscall_pwritev (int dirfd, struct Mono_Posix_Iovec *iov, gint32 iovcnt, gint64 off)
108 {
109         struct iovec* v;
110         gint64 res;
111
112         mph_return_if_off_t_overflow (off);
113
114         v = _mph_from_iovec_array (iov, iovcnt);
115         if (!v) {
116                 return -1;
117         }
118
119         res = pwritev (dirfd, v, iovcnt, (off_t) off);
120         free (v);
121         return res;
122 }
123 #endif /* def HAVE_PWRITEV */
124
125
126 /*
127  * vim: noexpandtab
128  */