getopt: remove USE_NONOPTION_FLAGS
[glibc.git] / misc / tst-preadvwritev-common.c
blob29433123a2ec148ee8fc4b8bd20e1536e557216e
1 /* Common definitions for preadv and pwritev.
2 Copyright (C) 2016-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <sys/uio.h>
20 #include <sys/stat.h>
22 static void do_prepare (void);
23 #define PREPARE(argc, argv) do_prepare ()
24 static int do_test (void);
25 #define TEST_FUNCTION do_test ()
26 #include "test-skeleton.c"
28 static char *temp_filename;
29 static int temp_fd;
31 static void
32 do_prepare (void)
34 temp_fd = create_temp_file ("tst-preadvwritev.", &temp_filename);
35 if (temp_fd == -1)
37 printf ("cannot create temporary file: %m\n");
38 exit (1);
42 #define FAIL(str) \
43 do { printf ("error: %s (line %d)\n", str, __LINE__); return 1; } while (0)
45 static int
46 do_test_with_offset (off_t offset)
48 struct iovec iov[2];
49 ssize_t ret;
51 char buf1[32];
52 char buf2[64];
54 memset (buf1, 0xf0, sizeof buf1);
55 memset (buf2, 0x0f, sizeof buf2);
57 /* Write two buffer with 32 and 64 bytes respectively. */
58 memset (iov, 0, sizeof iov);
59 iov[0].iov_base = buf1;
60 iov[0].iov_len = sizeof buf1;
61 iov[1].iov_base = buf2;
62 iov[1].iov_len = sizeof buf2;
64 ret = pwritev (temp_fd, iov, 2, offset);
65 if (ret == -1)
66 FAIL ("first pwritev returned -1");
67 if (ret != (sizeof buf1 + sizeof buf2))
68 FAIL ("first pwritev returned an unexpected value");
70 ret = pwritev (temp_fd, iov, 2, sizeof buf1 + sizeof buf2 + offset);
71 if (ret == -1)
72 FAIL ("second pwritev returned -1");
73 if (ret != (sizeof buf1 + sizeof buf2))
74 FAIL ("second pwritev returned an unexpected value");
76 char buf3[32];
77 char buf4[64];
79 memset (buf3, 0x0f, sizeof buf3);
80 memset (buf4, 0xf0, sizeof buf4);
82 iov[0].iov_base = buf3;
83 iov[0].iov_len = sizeof buf3;
84 iov[1].iov_base = buf4;
85 iov[1].iov_len = sizeof buf4;
87 /* Now read two buffer with 32 and 64 bytes respectively. */
88 ret = preadv (temp_fd, iov, 2, offset);
89 if (ret == -1)
90 FAIL ("first preadv returned -1");
91 if (ret != (sizeof buf3 + sizeof buf4))
92 FAIL ("first preadv returned an unexpected value");
94 if (memcmp (buf1, buf3, sizeof buf1) != 0)
95 FAIL ("first buffer from first preadv different than expected");
96 if (memcmp (buf2, buf4, sizeof buf2) != 0)
97 FAIL ("second buffer from first preadv different than expected");
99 ret = preadv (temp_fd, iov, 2, sizeof buf3 + sizeof buf4 + offset);
100 if (ret == -1)
101 FAIL ("second preadv returned -1");
102 if (ret != (sizeof buf3 + sizeof buf4))
103 FAIL ("second preadv returned an unexpected value");
105 /* And compare the buffers read and written to check if there are equal. */
106 if (memcmp (buf1, buf3, sizeof buf1) != 0)
107 FAIL ("first buffer from second preadv different than expected");
108 if (memcmp (buf2, buf4, sizeof buf2) != 0)
109 FAIL ("second buffer from second preadv different than expected");
111 return 0;