blob: ae05b7af0a37964abe041d1891df3e30d921eaf3 [file] [log] [blame]
Dmitry V. Levind4611512016-03-30 03:54:21 +00001/*
2 * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
15 *
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.
26 */
27
28#include "tests.h"
29
30#ifdef HAVE_PWRITEV
31
32# include <fcntl.h>
33# include <stdio.h>
34# include <sys/uio.h>
35# include <unistd.h>
36
37# define LEN 8
38# define LIM (LEN - 1)
39
40static void
41print_iov(const struct iovec *iov)
42{
43 unsigned int i;
44 unsigned char *buf = iov->iov_base;
45
46 fputs("{\"", stdout);
47 for (i = 0; i < iov->iov_len; ++i) {
48 if (i < LIM)
49 printf("\\%d", (int) buf[i]);
50 }
51 printf("\"%s, %u}",
52 i > LIM ? "..." : "", (unsigned) iov->iov_len);
53}
54
55static void
56print_iovec(const struct iovec *iov, unsigned int cnt, unsigned int size)
57{
58 if (!size) {
59 printf("%p", iov);
60 return;
61 }
62 unsigned int i;
63 putchar('[');
64 for (i = 0; i < cnt; ++i) {
65 if (i)
66 fputs(", ", stdout);
Dmitry V. Levind4611512016-03-30 03:54:21 +000067 if (i == size) {
68 printf("%p", &iov[i]);
69 break;
70 }
Dmitry V. Levin4ce30542016-05-07 22:54:04 +000071 if (i == LIM) {
72 fputs("...", stdout);
73 break;
74 }
Dmitry V. Levind4611512016-03-30 03:54:21 +000075 print_iov(&iov[i]);
76 }
77 putchar(']');
78}
79
80int
81main(void)
82{
83 (void) close(0);
84 if (open("/dev/null", O_WRONLY))
85 perror_msg_and_fail("open");
86
87 char *buf = tail_alloc(LEN);
88 unsigned i;
89 for (i = 0; i < LEN; ++i)
90 buf[i] = i;
91
92 struct iovec *iov = tail_alloc(sizeof(*iov) * LEN);
93 for (i = 0; i < LEN; ++i) {
94 buf[i] = i;
95 iov[i].iov_base = &buf[i];
96 iov[i].iov_len = LEN - i;
97 }
Dmitry V. Levind4611512016-03-30 03:54:21 +000098
99 const off_t offset = 0xdefaceddeadbeefLL;
Dmitry V. Levin304689e2016-04-21 21:16:02 +0000100 long rc;
Dmitry V. Levind4611512016-03-30 03:54:21 +0000101 int written = 0;
102 for (i = 0; i < LEN; ++i) {
103 written += iov[i].iov_len;
104 if (pwritev(0, iov, i + 1, offset + i) != written)
105 perror_msg_and_fail("pwritev");
106 fputs("pwritev(0, ", stdout);
107 print_iovec(iov, i + 1, LEN);
108 printf(", %u, %lld) = %d\n",
109 i + 1, (long long) offset + i, written);
110 }
111
112 for (i = 0; i <= LEN; ++i) {
113 unsigned int n = LEN + 1 - i;
Dmitry V. Levind4611512016-03-30 03:54:21 +0000114 fputs("pwritev(0, ", stdout);
115 print_iovec(iov + i, n, LEN - i);
Dmitry V. Levin304689e2016-04-21 21:16:02 +0000116 rc = pwritev(0, iov + i, n, offset + LEN + i);
117 printf(", %u, %lld) = %ld %s (%m)\n",
118 n, (long long) offset + LEN + i, rc, errno2name());
Dmitry V. Levind4611512016-03-30 03:54:21 +0000119 }
120
121 iov->iov_base = iov + LEN * 2;
Dmitry V. Levin304689e2016-04-21 21:16:02 +0000122 rc = pwritev(0, iov, 1, -1);
123 printf("pwritev(0, [{%p, %d}], 1, -1) = %ld %s (%m)\n",
124 iov->iov_base, LEN, rc, errno2name());
Dmitry V. Levind4611512016-03-30 03:54:21 +0000125
126 iov += LEN;
Dmitry V. Levin304689e2016-04-21 21:16:02 +0000127 rc = pwritev(0, iov, 42, -2);
128 printf("pwritev(0, %p, 42, -2) = %ld %s (%m)\n",
129 iov, rc, errno2name());
Dmitry V. Levind4611512016-03-30 03:54:21 +0000130
Dmitry V. Levin304689e2016-04-21 21:16:02 +0000131 rc = pwritev(0, NULL, 1, -3);
132 printf("pwritev(0, NULL, 1, -3) = %ld %s (%m)\n",
133 rc, errno2name());
Dmitry V. Levind4611512016-03-30 03:54:21 +0000134
Dmitry V. Levin4ce30542016-05-07 22:54:04 +0000135 rc = pwritev(0, iov, 0, -4);
Dmitry V. Levin304689e2016-04-21 21:16:02 +0000136 printf("pwritev(0, [], 0, -4) = %ld %s (%m)\n",
137 rc, errno2name());
Dmitry V. Levind4611512016-03-30 03:54:21 +0000138
139 puts("+++ exited with 0 +++");
140 return 0;
141}
142
143#else
144
145SKIP_MAIN_UNDEFINED("HAVE_PWRITEV")
146
147#endif