2 * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 # define LIM (LEN - 1)
42 print_iov(const struct iovec *iov)
45 unsigned char *buf = iov->iov_base;
48 for (i = 0; i < iov->iov_len; ++i) {
50 printf("\\%d", (int) buf[i]);
53 i > LIM ? "..." : "", (unsigned) iov->iov_len);
57 print_iovec(const struct iovec *iov, unsigned int cnt, unsigned int size)
65 for (i = 0; i < cnt; ++i) {
73 printf("%p", &iov[i]);
85 if (open("/dev/null", O_WRONLY))
86 perror_msg_and_fail("open");
88 char *buf = tail_alloc(LEN);
90 for (i = 0; i < LEN; ++i)
93 struct iovec *iov = tail_alloc(sizeof(*iov) * LEN);
94 for (i = 0; i < LEN; ++i) {
96 iov[i].iov_base = &buf[i];
97 iov[i].iov_len = LEN - i;
101 const off_t offset = 0xdefaceddeadbeefLL;
104 for (i = 0; i < LEN; ++i) {
105 written += iov[i].iov_len;
106 if (pwritev(0, iov, i + 1, offset + i) != written)
107 perror_msg_and_fail("pwritev");
108 fputs("pwritev(0, ", stdout);
109 print_iovec(iov, i + 1, LEN);
110 printf(", %u, %lld) = %d\n",
111 i + 1, (long long) offset + i, written);
114 for (i = 0; i <= LEN; ++i) {
115 unsigned int n = LEN + 1 - i;
116 fputs("pwritev(0, ", stdout);
117 print_iovec(iov + i, n, LEN - i);
118 rc = pwritev(0, iov + i, n, offset + LEN + i);
119 printf(", %u, %lld) = %ld %s (%m)\n",
120 n, (long long) offset + LEN + i, rc, errno2name());
123 iov->iov_base = iov + LEN * 2;
124 rc = pwritev(0, iov, 1, -1);
125 printf("pwritev(0, [{%p, %d}], 1, -1) = %ld %s (%m)\n",
126 iov->iov_base, LEN, rc, errno2name());
129 rc = pwritev(0, iov, 42, -2);
130 printf("pwritev(0, %p, 42, -2) = %ld %s (%m)\n",
131 iov, rc, errno2name());
133 rc = pwritev(0, NULL, 1, -3);
134 printf("pwritev(0, NULL, 1, -3) = %ld %s (%m)\n",
137 rc = pwritev(0, NULL, 0, -4);
138 printf("pwritev(0, [], 0, -4) = %ld %s (%m)\n",
141 puts("+++ exited with 0 +++");
147 SKIP_MAIN_UNDEFINED("HAVE_PWRITEV")