]>
Commit | Line | Data |
---|---|---|
3bbee82a | 1 | /* Common definitions for pread and pwrite. |
bfff8b1b | 2 | Copyright (C) 2016-2017 Free Software Foundation, Inc. |
3bbee82a L |
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 | <http://www.gnu.org/licenses/>. */ | |
18 | ||
19 | #include <errno.h> | |
20 | #include <error.h> | |
21 | #include <string.h> | |
22 | #include <unistd.h> | |
ceaa9889 | 23 | #include <sys/stat.h> |
3bbee82a L |
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 | /* We might need a bit longer timeout. */ | |
31 | #define TIMEOUT 20 /* sec */ | |
32 | ||
33 | /* This defines the `main' function and some more. */ | |
34 | #include <test-skeleton.c> | |
35 | ||
36 | /* These are for the temporary file we generate. */ | |
37 | static char *name; | |
38 | static int fd; | |
39 | ||
40 | static void | |
41 | do_prepare (void) | |
42 | { | |
43 | fd = create_temp_file ("tst-preadwrite.", &name); | |
44 | if (fd == -1) | |
45 | error (EXIT_FAILURE, errno, "cannot create temporary file"); | |
46 | } | |
47 | ||
48 | ||
49 | static ssize_t | |
50 | do_test_with_offset (off_t offset) | |
51 | { | |
52 | char buf[1000]; | |
53 | char res[1000]; | |
54 | int i; | |
55 | ssize_t ret; | |
56 | ||
57 | memset (buf, '\0', sizeof (buf)); | |
58 | memset (res, '\xff', sizeof (res)); | |
59 | ||
60 | if (write (fd, buf, sizeof (buf)) != sizeof (buf)) | |
61 | error (EXIT_FAILURE, errno, "during write"); | |
62 | ||
63 | for (i = 100; i < 200; ++i) | |
64 | buf[i] = i; | |
65 | ret = pwrite (fd, buf + 100, 100, offset + 100); | |
66 | if (ret == -1) | |
67 | error (EXIT_FAILURE, errno, "during pwrite"); | |
68 | ||
69 | for (i = 450; i < 600; ++i) | |
70 | buf[i] = i; | |
71 | ret = pwrite (fd, buf + 450, 150, offset + 450); | |
72 | if (ret == -1) | |
73 | error (EXIT_FAILURE, errno, "during pwrite"); | |
74 | ||
75 | ret = pread (fd, res, sizeof (buf) - 50, offset + 50); | |
76 | if (ret == -1) | |
77 | error (EXIT_FAILURE, errno, "during pread"); | |
78 | ||
79 | if (memcmp (buf + 50, res, ret) != 0) | |
80 | { | |
81 | printf ("error: read of pread != write of pwrite\n"); | |
82 | return -1; | |
83 | } | |
84 | ||
85 | return ret; | |
86 | } |