1 | /* Common definitions for pread and pwrite. |
2 | Copyright (C) 2016-2022 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
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. |
9 | |
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. |
14 | |
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 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <errno.h> |
20 | #include <error.h> |
21 | #include <string.h> |
22 | #include <unistd.h> |
23 | #include <sys/stat.h> |
24 | |
25 | static void do_prepare (void); |
26 | #define PREPARE(argc, argv) do_prepare () |
27 | static int do_test (void); |
28 | #define TEST_FUNCTION do_test () |
29 | |
30 | /* This defines the `main' function and some more. */ |
31 | #include <test-skeleton.c> |
32 | |
33 | /* These are for the temporary file we generate. */ |
34 | static char *name; |
35 | static int fd; |
36 | |
37 | static void |
38 | do_prepare (void) |
39 | { |
40 | fd = create_temp_file (base: "tst-preadwrite." , filename: &name); |
41 | if (fd == -1) |
42 | error (EXIT_FAILURE, errno, format: "cannot create temporary file" ); |
43 | } |
44 | |
45 | |
46 | static ssize_t |
47 | do_test_with_offset (off_t offset) |
48 | { |
49 | char buf[1000]; |
50 | char res[1000]; |
51 | int i; |
52 | ssize_t ret; |
53 | |
54 | memset (buf, '\0', sizeof (buf)); |
55 | memset (res, '\xff', sizeof (res)); |
56 | |
57 | if (write (fd, buf, sizeof (buf)) != sizeof (buf)) |
58 | error (EXIT_FAILURE, errno, format: "during write" ); |
59 | |
60 | for (i = 100; i < 200; ++i) |
61 | buf[i] = i; |
62 | ret = pwrite (fd: fd, buf: buf + 100, n: 100, offset: offset + 100); |
63 | if (ret == -1) |
64 | error (EXIT_FAILURE, errno, format: "during pwrite" ); |
65 | |
66 | for (i = 450; i < 600; ++i) |
67 | buf[i] = i; |
68 | ret = pwrite (fd: fd, buf: buf + 450, n: 150, offset: offset + 450); |
69 | if (ret == -1) |
70 | error (EXIT_FAILURE, errno, format: "during pwrite" ); |
71 | |
72 | ret = pread (fd: fd, buf: res, nbytes: sizeof (buf) - 50, offset: offset + 50); |
73 | if (ret == -1) |
74 | error (EXIT_FAILURE, errno, format: "during pread" ); |
75 | |
76 | if (memcmp (buf + 50, res, ret) != 0) |
77 | { |
78 | printf (format: "error: read of pread != write of pwrite\n" ); |
79 | return -1; |
80 | } |
81 | |
82 | return ret; |
83 | } |
84 | |