xfs/194: actually check if we got the desired block size before proceeding
[xfstests-dev.git] / src / pwrite_mmap_blocked.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*    Copyright (c) 2010 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <time.h>
12 #include <sys/mman.h>
13 #include <signal.h>
14 #include <sys/stat.h>
15
16
17
18 int main(int argc, char *argv[])
19 {
20         int ret;
21         char *cc = "01234";
22         char *progname;
23         loff_t size;
24         loff_t amount = 1;
25         loff_t from = 2;
26         loff_t to = 3;
27         int fd;
28         void *mapped_mem;
29
30         progname = argv[0];
31         size = 5;
32         fd = open(argv[1], O_RDWR|O_TRUNC|O_CREAT, 0666);
33         if (fd < 0) {
34                 fprintf(stderr, "%s: Cannot open `%s': %s\n",
35                         progname, argv[1], strerror(errno));
36                 exit(1);
37         }
38
39         if ((ret = pwrite(fd, (const char *)cc,
40                                 size, 0)) != size) {
41                 perror("pwrite");
42                 exit(1);
43         }
44
45         mapped_mem = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
46         if (mapped_mem == MAP_FAILED) {
47                 perror("mmap");
48                 exit(1);
49         }
50         printf("pwrite %Ld bytes from %Ld to %Ld\n",
51                 (long long) amount, (long long) from, (long long) to);
52
53         ret = pwrite(fd, (char *)mapped_mem + from, amount, to);
54         if (ret != amount) {
55                 perror("pwrite");
56                 exit(1);
57         }
58
59         munmap(mapped_mem,0);
60         close(fd);
61         exit(0);
62 }